predicates 0.1.1 → 0.1.2
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/README.md +1 -1
- data/lib/predicates/version.rb +1 -1
- data/lib/predicates.rb +4 -4
- data/test/predicates_test.rb +32 -7
- metadata +1 -1
data/README.md
CHANGED
@@ -32,7 +32,7 @@ Include Predicates in your class or model and define the predicate by supplying
|
|
32
32
|
user.confirmed = 1
|
33
33
|
user.confirmed? #=> true
|
34
34
|
|
35
|
-
Predicates will
|
35
|
+
Predicates will also create an attribute writer for you using `attr_writer` if the attribute you supplied has not been predefined.
|
36
36
|
|
37
37
|
class User
|
38
38
|
extend Predicates
|
data/lib/predicates/version.rb
CHANGED
data/lib/predicates.rb
CHANGED
@@ -3,8 +3,8 @@ require "predicates/version"
|
|
3
3
|
module Predicates
|
4
4
|
class NameError < Exception; end
|
5
5
|
|
6
|
-
# Defines a predicate (truth accessor) for a given attribute. If the attribute has not been
|
7
|
-
#
|
6
|
+
# Defines a predicate (truth accessor) for a given attribute. If the attribute has not been predefined,
|
7
|
+
# an attribute writer will be created along with the predicate.
|
8
8
|
#
|
9
9
|
# == Class Example
|
10
10
|
#
|
@@ -42,10 +42,10 @@ module Predicates
|
|
42
42
|
raise NameError
|
43
43
|
end
|
44
44
|
|
45
|
-
|
45
|
+
attr_writer attribute unless method_defined? attribute
|
46
46
|
|
47
47
|
define_method method do
|
48
|
-
!!
|
48
|
+
!!eval("@#{attribute}")
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
data/test/predicates_test.rb
CHANGED
@@ -21,7 +21,7 @@ class PredicatesTest < MiniTest::Unit::TestCase
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
def
|
24
|
+
def test_define_a_predicate_with_a_predefined_attribute_accessor
|
25
25
|
klass = Class.new WithPredicates do
|
26
26
|
attr_accessor :attribute
|
27
27
|
predicate :attribute?
|
@@ -41,7 +41,7 @@ class PredicatesTest < MiniTest::Unit::TestCase
|
|
41
41
|
refute object.attribute?
|
42
42
|
end
|
43
43
|
|
44
|
-
def
|
44
|
+
def test_define_a_predicate_with_only_a_predefined_attribute_reader
|
45
45
|
klass = Class.new WithPredicates do
|
46
46
|
attr_reader :attribute
|
47
47
|
predicate :attribute?
|
@@ -59,20 +59,45 @@ class PredicatesTest < MiniTest::Unit::TestCase
|
|
59
59
|
assert object.attribute?
|
60
60
|
end
|
61
61
|
|
62
|
-
def
|
62
|
+
def test_define_a_predicate_with_only_a_predefined_attribute_writer
|
63
63
|
klass = Class.new WithPredicates do
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
attr_writer :attribute
|
65
|
+
predicate :attribute?
|
66
|
+
end
|
67
|
+
|
68
|
+
object = klass.new
|
69
|
+
|
70
|
+
assert object.respond_to?(:attribute?)
|
71
|
+
refute object.respond_to?(:attribute)
|
72
|
+
|
73
|
+
object.attribute = 1
|
74
|
+
assert object.attribute?
|
75
|
+
|
76
|
+
object.attribute = true
|
77
|
+
assert object.attribute?
|
67
78
|
|
79
|
+
object.attribute = nil
|
80
|
+
refute object.attribute?
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_define_a_predicate_with_no_predefined_attribute_accessor
|
84
|
+
klass = Class.new WithPredicates do
|
68
85
|
predicate :attribute?
|
69
86
|
end
|
70
87
|
|
71
88
|
object = klass.new
|
72
89
|
|
73
90
|
assert object.respond_to?(:attribute?)
|
74
|
-
|
91
|
+
assert object.respond_to?(:attribute=)
|
92
|
+
refute object.respond_to?(:attribute)
|
75
93
|
|
94
|
+
object.attribute = 1
|
76
95
|
assert object.attribute?
|
96
|
+
|
97
|
+
object.attribute = true
|
98
|
+
assert object.attribute?
|
99
|
+
|
100
|
+
object.attribute = nil
|
101
|
+
refute object.attribute?
|
77
102
|
end
|
78
103
|
end
|