attr_extras 1.8.0 → 1.8.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.
- checksums.yaml +4 -4
- data/README.md +10 -7
- data/lib/attr_extras.rb +6 -4
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_extras_spec.rb +3 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17e3dfb363304609f6981fe839b36c318f9726ea
|
4
|
+
data.tar.gz: d32ee310679a5135dd5433e288b561451418a88f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c24ba0b62181e236814657c9e06fdff80263696d77f3d824b62eeec038e20b7ff0cd4979783d16aec71c884fd2f7711429c7983e3cdc7321b301c52e8f33e958
|
7
|
+
data.tar.gz: 7846ad4e0bd1808dccdec81857477a8e7372cf56378e6e1e95659fe4a19339ba2c4759f713fa18577aa4705b4a939fd165bf26cc83f3e122e4fe532b37c8b0c4
|
data/README.md
CHANGED
@@ -47,7 +47,7 @@ Defines both initializer and private readers.
|
|
47
47
|
Defines both initializer and public readers.
|
48
48
|
|
49
49
|
`method_object :fooable?, :foo`<br>
|
50
|
-
Defines a `.fooable?` class method that delegates to an instance method.
|
50
|
+
Defines a `.fooable?` class method that takes one argument (`:foo`) and delegates to an instance method that can access `foo` as a private reader.
|
51
51
|
|
52
52
|
`attr_id_query :foo?, :bar?`<br>
|
53
53
|
Defines query methods like `foo?`, which is true iff `foo_id` is truthy. Goes well with Active Record.
|
@@ -64,8 +64,7 @@ and the enforced questionmarks with `attr_id_query :foo?`, so you can search for
|
|
64
64
|
|
65
65
|
``` ruby
|
66
66
|
class MyClass
|
67
|
-
|
68
|
-
attr_private :foo
|
67
|
+
pattr_initialize :foo, :bar
|
69
68
|
attr_id_query :item?
|
70
69
|
attr_query :oof?
|
71
70
|
|
@@ -84,6 +83,7 @@ x.foo # NoMethodError: private method `foo' called.
|
|
84
83
|
x.item? # => true
|
85
84
|
x.oof? # => true
|
86
85
|
|
86
|
+
|
87
87
|
class MyMethodObject
|
88
88
|
method_object :fooable?,
|
89
89
|
:foo
|
@@ -96,6 +96,7 @@ end
|
|
96
96
|
MyMethodObject.fooable?(:some_value) # => true
|
97
97
|
MyMethodObject.fooable?(:another_value) # => false
|
98
98
|
|
99
|
+
|
99
100
|
class MyHashyObject
|
100
101
|
attr_initialize :foo, [:bar, :baz]
|
101
102
|
attr_reader :bar
|
@@ -104,14 +105,15 @@ end
|
|
104
105
|
x = MyHashyObject.new("Foo!", bar: "Bar!", baz: "Baz!")
|
105
106
|
x.bar # => "Bar!"
|
106
107
|
|
108
|
+
|
107
109
|
class MyValueObject
|
108
110
|
attr_value :foo, :bar
|
109
111
|
end
|
110
112
|
|
111
113
|
x = MyValueObject.new(5, 10)
|
112
|
-
x.foo
|
113
|
-
x.bar
|
114
|
-
x.foo = 20
|
114
|
+
x.foo # => 5
|
115
|
+
x.bar # => 10
|
116
|
+
x.foo = 20 # NoMethodError: undefined method `foo=''`
|
115
117
|
```
|
116
118
|
|
117
119
|
## Why not use `Struct`?
|
@@ -125,7 +127,7 @@ Instead of `attr_private :foo`, you could do `private; attr_reader :foo`.
|
|
125
127
|
|
126
128
|
Other than being more to type, declaring `attr_reader` after `private` will actually give you a warning (deserved or not) if you run Ruby with warnings turned on.
|
127
129
|
|
128
|
-
If you don't want the dependency on `attr_extras`, you can get rid of the warnings with `attr_reader :foo; private :foo
|
130
|
+
If you don't want the dependency on `attr_extras`, you can get rid of the warnings with `attr_reader :foo; private :foo`. Or just define a regular private method.
|
129
131
|
|
130
132
|
|
131
133
|
## Installation
|
@@ -142,6 +144,7 @@ Or install it yourself as:
|
|
142
144
|
|
143
145
|
gem install attr_extras
|
144
146
|
|
147
|
+
|
145
148
|
## License
|
146
149
|
|
147
150
|
Copyright (c) 2012 [Barsoom AB](http://barsoom.se)
|
data/lib/attr_extras.rb
CHANGED
@@ -44,14 +44,12 @@ module AttrExtras
|
|
44
44
|
|
45
45
|
def pattr_initialize(*names)
|
46
46
|
attr_initialize(*names)
|
47
|
-
|
48
|
-
flat_names = names.flatten.map { |x| x.to_s.sub(/!\z/, "") }
|
49
|
-
attr_private(*flat_names)
|
47
|
+
attr_private *attr_flat_names(names)
|
50
48
|
end
|
51
49
|
|
52
50
|
def attr_value(*names)
|
53
51
|
attr_initialize(*names)
|
54
|
-
attr_reader(
|
52
|
+
attr_reader *attr_flat_names(names)
|
55
53
|
end
|
56
54
|
|
57
55
|
def method_object(method_name, *names)
|
@@ -72,6 +70,10 @@ module AttrExtras
|
|
72
70
|
|
73
71
|
private
|
74
72
|
|
73
|
+
def attr_flat_names(names)
|
74
|
+
names.flatten.map { |x| x.to_s.sub(/!\z/, "") }
|
75
|
+
end
|
76
|
+
|
75
77
|
def attr_query_with_suffix(*names, suffix)
|
76
78
|
names.each do |name|
|
77
79
|
name = name.to_s
|
data/lib/attr_extras/version.rb
CHANGED
data/spec/attr_extras_spec.rb
CHANGED
@@ -123,11 +123,12 @@ describe Object, ".attr_value" do
|
|
123
123
|
|
124
124
|
it "works with hash ivars" do
|
125
125
|
klass = Class.new do
|
126
|
-
attr_value :foo, [:bar, :baz]
|
126
|
+
attr_value :foo, [:bar, :baz!]
|
127
127
|
end
|
128
128
|
|
129
|
-
example = klass.new("Foo", :bar => "Bar")
|
129
|
+
example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
130
130
|
example.bar.must_equal "Bar"
|
131
|
+
example.baz.must_equal "Baz"
|
131
132
|
end
|
132
133
|
end
|
133
134
|
|