public
Description: Adds automatic generation of predicate methods when defining attributes
Homepage: http://www.pluginaweek.org
Clone URL: git://github.com/pluginaweek/attribute_predicates.git
Remove dependency on active_support for non-ActiveRecord predicates
Tag 0.1.1 release
obrie (author)
Sun Jul 06 20:42:58 -0700 2008
commit  a5245091b05c5cfccce6132accb2456985a1d52d
tree    47359fbbc4dd32e948220a2c9ac121786bad0891
parent  03cdb00980a1401f2fae880b623ed3ccf5c09668
...
1
2
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
0
@@ -1,5 +1,9 @@
0
 == master
0
 
0
+== 0.1.1 / 2008-07-06
0
+
0
+* Remove dependency on active_support for non-ActiveRecord predicates
0
+
0
 == 0.1.0 / 2008-07-03
0
 
0
 * Rename to attribute_predicates
...
5
6
7
8
 
9
10
11
...
5
6
7
 
8
9
10
11
0
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
0
 
0
 spec = Gem::Specification.new do |s|
0
   s.name = 'attribute_predicates'
0
- s.version = '0.1.0'
0
+ s.version = '0.1.1'
0
   s.platform = Gem::Platform::RUBY
0
   s.summary = 'Adds automatic generation of predicate methods for attributes.'
0
   
...
29
30
31
32
33
 
 
 
34
35
36
...
59
60
61
62
 
 
 
 
 
 
 
 
 
63
64
65
...
29
30
31
 
 
32
33
34
35
36
37
...
60
61
62
 
63
64
65
66
67
68
69
70
71
72
73
74
0
@@ -29,8 +29,9 @@ module PluginAWeek #:nodoc:
0
       module Module
0
         def self.included(base) #:nodoc:
0
           base.class_eval do
0
- [:attr, :attr_reader, :attr_writer, :attr_accessor].each do |method|
0
- alias_method_chain method, :predicates
0
+ %w(attr attr_reader attr_writer attr_accessor).each do |method|
0
+ alias_method "#{method}_without_predicates", method
0
+ alias_method method, "#{method}_with_predicates"
0
             end
0
           end
0
         end
0
@@ -59,7 +60,15 @@ module PluginAWeek #:nodoc:
0
           # Returns true if the specified variable is not blank, otherwise false
0
           def attr_predicate(symbol)
0
             define_method("#{symbol}?") do
0
- !instance_variable_get("@#{symbol}").blank?
0
+ value = instance_variable_get("@#{symbol}")
0
+ if value.respond_to?(:blank?)
0
+ # Use ActiveSupport's implementation
0
+ !value.blank?
0
+ elsif value.respond_to?(:empty?)
0
+ !value.empty?
0
+ else
0
+ !!value
0
+ end
0
             end
0
           end
0
       end
...
15
16
17
18
 
19
20
21
22
 
23
24
25
26
27
28
29
 
30
31
32
...
39
40
41
42
 
43
44
45
46
 
47
48
49
50
51
52
53
 
54
55
56
57
 
58
59
60
...
67
68
69
70
 
71
72
73
74
75
76
77
 
78
79
80
...
87
88
89
90
 
91
92
93
94
 
95
96
97
98
99
100
101
 
102
103
104
105
 
106
107
108
...
131
132
133
 
134
135
136
...
15
16
17
 
18
19
20
21
 
22
23
24
25
26
27
28
 
29
30
31
32
...
39
40
41
 
42
43
44
45
 
46
47
48
49
50
51
52
 
53
54
55
56
 
57
58
59
60
...
67
68
69
 
70
71
72
73
74
75
76
 
77
78
79
80
...
87
88
89
 
90
91
92
93
 
94
95
96
97
98
99
100
 
101
102
103
104
 
105
106
107
108
...
131
132
133
134
135
136
137
0
@@ -15,18 +15,18 @@ class ModuleAttrTest < Test::Unit::TestCase
0
   
0
   def test_should_create_predicate_for_readonly_attr
0
     @module.attr(:foo)
0
- ['foo', 'foo?'].each do |method|
0
+ %w(foo foo?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
     
0
- ['foo='].each do |method|
0
+ %w(foo=).each do |method|
0
       assert !@module.instance_methods.include?(method), "#{method} exists"
0
     end
0
   end
0
   
0
   def test_should_create_predicate_for_readwrite_attr
0
     @module.attr(:foo, true)
0
- ['foo', 'foo=', 'foo?'].each do |method|
0
+ %w(foo foo= foo?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
   end
0
@@ -39,22 +39,22 @@ class ModuleAttrReaderTest < Test::Unit::TestCase
0
   
0
   def test_should_create_predicate
0
     @module.attr_reader(:foo)
0
- ['foo', 'foo?'].each do |method|
0
+ %w(foo foo?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
     
0
- ['foo='].each do |method|
0
+ %w(foo=).each do |method|
0
       assert !@module.instance_methods.include?(method), "#{method} exists"
0
     end
0
   end
0
   
0
   def test_should_create_predicate_for_multiple_attributes
0
     @module.attr_reader(:foo, :bar)
0
- ['foo', 'foo?', 'bar', 'bar?'].each do |method|
0
+ %w(foo foo? bar bar?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
     
0
- ['foo=', 'bar='].each do |method|
0
+ %w(foo= bar=).each do |method|
0
       assert !@module.instance_methods.include?(method), "#{method} exists"
0
     end
0
   end
0
@@ -67,14 +67,14 @@ class ModuleAttrAccessorTest < Test::Unit::TestCase
0
   
0
   def test_should_create_predicate
0
     @module.attr_accessor(:foo)
0
- ['foo', 'foo=', 'foo?'].each do |method|
0
+ %w(foo foo= foo?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
   end
0
   
0
   def test_should_create_predicate_for_multiple_attributes
0
     @module.attr_accessor(:foo, :bar)
0
- ['foo', 'foo=', 'foo?', 'bar', 'bar=', 'bar?'].each do |method|
0
+ %w(foo foo= foo? bar bar= bar?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
   end
0
@@ -87,22 +87,22 @@ class ModuleAttrWriterTest < Test::Unit::TestCase
0
   
0
   def test_should_create_predicate
0
     @module.attr_writer(:foo)
0
- ['foo=', 'foo?'].each do |method|
0
+ %w(foo= foo?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
     
0
- ['foo'].each do |method|
0
+ %w(foo).each do |method|
0
       assert !@module.instance_methods.include?(method), "#{method} exists"
0
     end
0
   end
0
   
0
   def test_should_create_predicate_for_multiple_attributes
0
     @module.attr_writer(:foo, :bar)
0
- ['foo=', 'foo?', 'bar=', 'bar?'].each do |method|
0
+ %w(foo= foo? bar= bar?).each do |method|
0
       assert @module.instance_methods.include?(method), "#{method} does not exist"
0
     end
0
     
0
- ['foo', 'bar'].each do |method|
0
+ %w(foo bar).each do |method|
0
       assert !@module.instance_methods.include?(method), "#{method} exists"
0
     end
0
   end
0
@@ -131,6 +131,7 @@ class ModuleTest < Test::Unit::TestCase
0
   def test_should_evaluate_false_values_for_predicate
0
     @klass.attr_accessor(:foo)
0
     
0
+ # *Note* ' ' is only false when ActiveSupport is being used
0
     [nil, '', ' ', {}, []].each do |value|
0
       assert_equal false, @klass.new(value).foo?, "#{value.inspect} is true"
0
     end
...
1
2
 
 
3
4
...
1
2
3
4
5
6
0
@@ -1,4 +1,6 @@
0
 # Load the plugin testing framework
0
 $:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
0
+
0
+# Use the test helper for testing ActiveRecord
0
 require 'rubygems'
0
 require 'plugin_test_helper'

Comments

    No one has commented yet.