attribute_predicates 0.1.0 → 0.1.1
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 +4 -0
- data/README.rdoc +1 -1
- data/Rakefile +1 -1
- data/lib/attribute_predicates/extensions/active_record.rb +44 -13
- data/lib/attribute_predicates/extensions/module.rb +68 -24
- data/test/module_test.rb +14 -13
- data/test/test_helper.rb +2 -0
- metadata +2 -2
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
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.
|
8
|
+
s.version = '0.1.1'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.summary = 'Adds automatic generation of predicate methods for attributes.'
|
11
11
|
|
@@ -1,22 +1,53 @@
|
|
1
1
|
module PluginAWeek #:nodoc:
|
2
2
|
module AttributePredicates
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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}"))
|
44
|
+
end
|
14
45
|
end
|
15
|
-
|
46
|
+
end
|
16
47
|
end
|
17
48
|
end
|
18
49
|
end
|
19
50
|
|
20
51
|
ActiveRecord::Base.class_eval do
|
21
|
-
extend PluginAWeek::AttributePredicates::ActiveRecord
|
52
|
+
extend PluginAWeek::AttributePredicates::Extensions::ActiveRecord
|
22
53
|
end if defined?(ActiveRecord)
|
@@ -1,37 +1,81 @@
|
|
1
1
|
module PluginAWeek #:nodoc:
|
2
2
|
module AttributePredicates
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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,
|
12
|
+
#
|
13
|
+
# class Person
|
14
|
+
# attr_accessor :value
|
15
|
+
# end
|
16
|
+
#
|
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
|
8
36
|
end
|
9
37
|
end
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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)
|
21
50
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
define_method("#{symbol}?") do
|
28
|
-
!instance_variable_get("@#{symbol}").blank?
|
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)}
|
29
56
|
end
|
30
57
|
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
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
31
75
|
end
|
32
76
|
end
|
33
77
|
end
|
34
78
|
|
35
79
|
::Module.class_eval do
|
36
|
-
include PluginAWeek::AttributePredicates::Module
|
80
|
+
include PluginAWeek::AttributePredicates::Extensions::Module
|
37
81
|
end
|
data/test/module_test.rb
CHANGED
@@ -15,18 +15,18 @@ class ModuleAttrTest < Test::Unit::TestCase
|
|
15
15
|
|
16
16
|
def test_should_create_predicate_for_readonly_attr
|
17
17
|
@module.attr(:foo)
|
18
|
-
|
18
|
+
%w(foo foo?).each do |method|
|
19
19
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
%w(foo=).each do |method|
|
23
23
|
assert !@module.instance_methods.include?(method), "#{method} exists"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_should_create_predicate_for_readwrite_attr
|
28
28
|
@module.attr(:foo, true)
|
29
|
-
|
29
|
+
%w(foo foo= foo?).each do |method|
|
30
30
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
31
31
|
end
|
32
32
|
end
|
@@ -39,22 +39,22 @@ class ModuleAttrReaderTest < Test::Unit::TestCase
|
|
39
39
|
|
40
40
|
def test_should_create_predicate
|
41
41
|
@module.attr_reader(:foo)
|
42
|
-
|
42
|
+
%w(foo foo?).each do |method|
|
43
43
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
44
44
|
end
|
45
45
|
|
46
|
-
|
46
|
+
%w(foo=).each do |method|
|
47
47
|
assert !@module.instance_methods.include?(method), "#{method} exists"
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
def test_should_create_predicate_for_multiple_attributes
|
52
52
|
@module.attr_reader(:foo, :bar)
|
53
|
-
|
53
|
+
%w(foo foo? bar bar?).each do |method|
|
54
54
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
55
55
|
end
|
56
56
|
|
57
|
-
|
57
|
+
%w(foo= bar=).each do |method|
|
58
58
|
assert !@module.instance_methods.include?(method), "#{method} exists"
|
59
59
|
end
|
60
60
|
end
|
@@ -67,14 +67,14 @@ class ModuleAttrAccessorTest < Test::Unit::TestCase
|
|
67
67
|
|
68
68
|
def test_should_create_predicate
|
69
69
|
@module.attr_accessor(:foo)
|
70
|
-
|
70
|
+
%w(foo foo= foo?).each do |method|
|
71
71
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
def test_should_create_predicate_for_multiple_attributes
|
76
76
|
@module.attr_accessor(:foo, :bar)
|
77
|
-
|
77
|
+
%w(foo foo= foo? bar bar= bar?).each do |method|
|
78
78
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
79
79
|
end
|
80
80
|
end
|
@@ -87,22 +87,22 @@ class ModuleAttrWriterTest < Test::Unit::TestCase
|
|
87
87
|
|
88
88
|
def test_should_create_predicate
|
89
89
|
@module.attr_writer(:foo)
|
90
|
-
|
90
|
+
%w(foo= foo?).each do |method|
|
91
91
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
92
92
|
end
|
93
93
|
|
94
|
-
|
94
|
+
%w(foo).each do |method|
|
95
95
|
assert !@module.instance_methods.include?(method), "#{method} exists"
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
99
|
def test_should_create_predicate_for_multiple_attributes
|
100
100
|
@module.attr_writer(:foo, :bar)
|
101
|
-
|
101
|
+
%w(foo= foo? bar= bar?).each do |method|
|
102
102
|
assert @module.instance_methods.include?(method), "#{method} does not exist"
|
103
103
|
end
|
104
104
|
|
105
|
-
|
105
|
+
%w(foo bar).each do |method|
|
106
106
|
assert !@module.instance_methods.include?(method), "#{method} exists"
|
107
107
|
end
|
108
108
|
end
|
@@ -131,6 +131,7 @@ class ModuleTest < Test::Unit::TestCase
|
|
131
131
|
def test_should_evaluate_false_values_for_predicate
|
132
132
|
@klass.attr_accessor(:foo)
|
133
133
|
|
134
|
+
# *Note* ' ' is only false when ActiveSupport is being used
|
134
135
|
[nil, '', ' ', {}, []].each do |value|
|
135
136
|
assert_equal false, @klass.new(value).foo?, "#{value.inspect} is true"
|
136
137
|
end
|
data/test/test_helper.rb
CHANGED
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.
|
4
|
+
version: 0.1.1
|
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-07-
|
12
|
+
date: 2008-07-06 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|