attribute_predicates 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,9 @@
1
1
  == master
2
2
 
3
+ == 0.2.0 / 2008-12-14
4
+
5
+ * Remove the PluginAWeek namespace
6
+
3
7
  == 0.1.2 / 2008-11-29
4
8
 
5
9
  * Fix empty strings in ActiveRecord 2.2+ returning nil instead of false
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
5
5
 
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = 'attribute_predicates'
8
- s.version = '0.1.2'
8
+ s.version = '0.2.0'
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.summary = 'Adds automatic generation of predicate methods for attributes.'
11
11
 
@@ -1,53 +1,51 @@
1
- module PluginAWeek #:nodoc:
2
- module AttributePredicates
3
- module Extensions
4
- # Adds support for automatically defining predicate methods using +attr_predicate+
5
- # when defining attributes using +attr+, +attr_reader+, +attr_reader+, and
6
- # +attr_accessor+. In comparison to normal Ruby attributes, ActiveRecord
7
- # predicates use a different system for defining true/false.
8
- #
9
- # == Examples
10
- #
11
- # The predicate methods for attributes use ActiveRecord's type conversion
12
- # for booleans for determing whether to return true or false. For example,
13
- #
14
- # class Person < ActiveRecord::Base
15
- # attr_accessor :value
16
- # end
17
- #
18
- # p = Person.new
19
- # p.value = false
20
- # p.value? # => false
21
- #
22
- # p.value = 'false'
23
- # p.value? # => false
24
- #
25
- # p.value = 'true'
26
- # p.value? # => true
27
- #
28
- # p.value = 't'
29
- # p.value? # => true
30
- #
31
- # p.value = 1
32
- # p.value? # => true
33
- module ActiveRecord
34
- private
35
- # For Strings, returns true when value is:
36
- # * "true"
37
- # * "t"
38
- #
39
- # For Integers, returns true when value is:
40
- # * 1
41
- def attr_predicate(symbol)
42
- define_method("#{symbol}?") do
43
- ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(instance_variable_get("@#{symbol}")) == true
44
- end
1
+ module AttributePredicates
2
+ module Extensions
3
+ # Adds support for automatically defining predicate methods using +attr_predicate+
4
+ # when defining attributes using +attr+, +attr_reader+, +attr_reader+, and
5
+ # +attr_accessor+. In comparison to normal Ruby attributes, ActiveRecord
6
+ # predicates use a different system for defining true/false.
7
+ #
8
+ # == Examples
9
+ #
10
+ # The predicate methods for attributes use ActiveRecord's type conversion
11
+ # for booleans for determing whether to return true or false. For example,
12
+ #
13
+ # class Person < ActiveRecord::Base
14
+ # attr_accessor :value
15
+ # end
16
+ #
17
+ # p = Person.new
18
+ # p.value = false
19
+ # p.value? # => false
20
+ #
21
+ # p.value = 'false'
22
+ # p.value? # => false
23
+ #
24
+ # p.value = 'true'
25
+ # p.value? # => true
26
+ #
27
+ # p.value = 't'
28
+ # p.value? # => true
29
+ #
30
+ # p.value = 1
31
+ # p.value? # => true
32
+ module ActiveRecord
33
+ private
34
+ # For Strings, returns true when value is:
35
+ # * "true"
36
+ # * "t"
37
+ #
38
+ # For Integers, returns true when value is:
39
+ # * 1
40
+ def attr_predicate(symbol)
41
+ define_method("#{symbol}?") do
42
+ ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(instance_variable_get("@#{symbol}")) == true
45
43
  end
46
- end
44
+ end
47
45
  end
48
46
  end
49
47
  end
50
48
 
51
49
  ActiveRecord::Base.class_eval do
52
- extend PluginAWeek::AttributePredicates::Extensions::ActiveRecord
50
+ extend AttributePredicates::Extensions::ActiveRecord
53
51
  end if defined?(ActiveRecord)
@@ -1,81 +1,79 @@
1
- module PluginAWeek #:nodoc:
2
- module AttributePredicates
3
- module Extensions
4
- # Adds support for automatically defining predicate methods using +attr_predicate+
5
- # when defining attributes using +attr+, +attr_reader+, +attr_reader+, and
6
- # +attr_accessor+.
7
- #
8
- # == Examples
9
- #
10
- # The predicate methods for attributes checks whether the value is blank?
11
- # for determining whether true or false should be returned. For example,
1
+ module AttributePredicates
2
+ module Extensions
3
+ # Adds support for automatically defining predicate methods using +attr_predicate+
4
+ # when defining attributes using +attr+, +attr_reader+, +attr_reader+, and
5
+ # +attr_accessor+.
6
+ #
7
+ # == Examples
8
+ #
9
+ # The predicate methods for attributes checks whether the value is blank?
10
+ # for determining whether true or false should be returned. For example,
11
+ #
12
+ # class Person
13
+ # attr_accessor :value
14
+ # end
15
+ #
16
+ # p = Person.new
17
+ # p.value = false
18
+ # p.value? # => false
19
+ #
20
+ # p.value = true
21
+ # p.value? # => true
22
+ #
23
+ # p.value = []
24
+ # p.value? # => false
25
+ #
26
+ # p.value = [false]
27
+ # p.value? # => true
28
+ module Module
29
+ def self.included(base) #:nodoc:
30
+ base.class_eval do
31
+ %w(attr attr_reader attr_writer attr_accessor).each do |method|
32
+ alias_method "#{method}_without_predicates", method
33
+ alias_method method, "#{method}_with_predicates"
34
+ end
35
+ end
36
+ end
37
+
38
+ # Defines a predicate method, using +attr_predicate+, in addition to the
39
+ # attribute accessors. For example,
12
40
  #
13
- # class Person
14
- # attr_accessor :value
41
+ # module Mod
42
+ # attr :is_okay
15
43
  # end
16
44
  #
17
- # p = Person.new
18
- # p.value = false
19
- # p.value? # => false
20
- #
21
- # p.value = true
22
- # p.value? # => true
23
- #
24
- # p.value = []
25
- # p.value? # => false
26
- #
27
- # p.value = [false]
28
- # p.value? # => true
29
- module Module
30
- def self.included(base) #:nodoc:
31
- base.class_eval do
32
- %w(attr attr_reader attr_writer attr_accessor).each do |method|
33
- alias_method "#{method}_without_predicates", method
34
- alias_method method, "#{method}_with_predicates"
35
- end
36
- end
37
- end
38
-
39
- # Defines a predicate method, using +attr_predicate+, in addition to the
40
- # attribute accessors. For example,
41
- #
42
- # module Mod
43
- # attr :is_okay
44
- # end
45
- #
46
- # Mod.instance-methods.sort # => ["is_okay", "is_okay?"]
47
- def attr_with_predicates(*args)
48
- attr_without_predicates(*args)
49
- attr_predicate(args.first)
50
- end
51
-
52
- [:attr_reader, :attr_writer, :attr_accessor].each do |method|
53
- define_method("#{method}_with_predicates") do |*symbols|
54
- send("#{method}_without_predicates", *symbols)
55
- symbols.each {|symbol| attr_predicate(symbol)}
56
- end
45
+ # Mod.instance-methods.sort # => ["is_okay", "is_okay?"]
46
+ def attr_with_predicates(*args)
47
+ attr_without_predicates(*args)
48
+ attr_predicate(args.first)
49
+ end
50
+
51
+ [:attr_reader, :attr_writer, :attr_accessor].each do |method|
52
+ define_method("#{method}_with_predicates") do |*symbols|
53
+ send("#{method}_without_predicates", *symbols)
54
+ symbols.each {|symbol| attr_predicate(symbol)}
57
55
  end
58
-
59
- private
60
- # Returns true if the specified variable is not blank, otherwise false
61
- def attr_predicate(symbol)
62
- define_method("#{symbol}?") do
63
- value = instance_variable_get("@#{symbol}")
64
- if value.respond_to?(:blank?)
65
- # Use ActiveSupport's implementation
66
- !value.blank?
67
- elsif value.respond_to?(:empty?)
68
- !value.empty?
69
- else
70
- !!value
71
- end
56
+ end
57
+
58
+ private
59
+ # Returns true if the specified variable is not blank, otherwise false
60
+ def attr_predicate(symbol)
61
+ define_method("#{symbol}?") do
62
+ value = instance_variable_get("@#{symbol}")
63
+ if value.respond_to?(:blank?)
64
+ # Use ActiveSupport's implementation
65
+ !value.blank?
66
+ elsif value.respond_to?(:empty?)
67
+ !value.empty?
68
+ else
69
+ !!value
72
70
  end
73
71
  end
74
- end
72
+ end
75
73
  end
76
74
  end
77
75
  end
78
76
 
79
77
  ::Module.class_eval do
80
- include PluginAWeek::AttributePredicates::Extensions::Module
78
+ include AttributePredicates::Extensions::Module
81
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_predicates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Pfeifer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-29 00:00:00 -05:00
12
+ date: 2008-12-14 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,13 +22,13 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
 
24
24
  files:
25
+ - lib/attribute_predicates.rb
25
26
  - lib/attribute_predicates
26
27
  - lib/attribute_predicates/extensions
27
- - lib/attribute_predicates/extensions/active_record.rb
28
28
  - lib/attribute_predicates/extensions/module.rb
29
- - lib/attribute_predicates.rb
30
- - test/module_test.rb
29
+ - lib/attribute_predicates/extensions/active_record.rb
31
30
  - test/test_helper.rb
31
+ - test/module_test.rb
32
32
  - test/active_record_test.rb
33
33
  - CHANGELOG.rdoc
34
34
  - init.rb