attr_extras 1.8.1 → 1.9.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.
- checksums.yaml +4 -4
- data/README.md +11 -5
- data/lib/attr_extras.rb +9 -1
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_extras_spec.rb +46 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d151eb4e76dda68fdeef01c651351ddc24bd55a
|
4
|
+
data.tar.gz: 87c5fad6b639817d4c2884ef24c319924b1437dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b65f8a8e1b9ed0cd46f4628f76ddde7459c3a2c848263b560af9e3a6b012816d62bf8ba2973f68c1dfa0bbd5e73fcf5a2070b1e7e155946b3c5077509753e8f
|
7
|
+
data.tar.gz: c49227cae57dbb9f2178bee8eee1ce9a344ba5a6918093011396e6462051e8d7b3fdc8326d104c188f639144e33eff2d29cd21eb9fedb87c508da091cd20f23d
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# attr\_extras
|
4
4
|
|
5
|
-
Takes some boilerplate out of Ruby, lowering the barrier to extracting small focused classes, without the downsides of using `Struct
|
5
|
+
Takes some boilerplate out of Ruby, lowering the barrier to extracting small focused classes, without [the downsides of using `Struct`](http://thepugautomatic.com/2013/08/struct-inheritance-is-overused/).
|
6
6
|
|
7
7
|
Instead of
|
8
8
|
|
@@ -34,20 +34,26 @@ This nicely complements Ruby's built-in `attr_accessor`, `attr_reader` and `attr
|
|
34
34
|
`attr_initialize :foo, :bar`<br>
|
35
35
|
Defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
|
36
36
|
|
37
|
-
`attr_initialize :foo, [:bar, :baz!]
|
37
|
+
`attr_initialize :foo, [:bar, :baz!]`,
|
38
38
|
Defines an initializer that takes one regular argument, assigning `@foo`, and one hash argument, assigning `@bar` (optional) and `@baz` (required).
|
39
39
|
|
40
|
+
`attr_initialize [:bar, :baz!]`,
|
41
|
+
Defines an initializer that takes one hash argument, assigning `@bar` (optional) and `@baz` (required).
|
42
|
+
|
40
43
|
`attr_private :foo, :bar`<br>
|
41
44
|
Defines private readers for `@foo` and `@bar`.
|
42
45
|
|
43
46
|
`pattr_initialize :foo, :bar`<br>
|
44
|
-
Defines both initializer and private readers.
|
47
|
+
Defines both initializer and private readers. The `[]` notation for hash arguments is also supported.
|
45
48
|
|
46
49
|
`attr_value :foo, :bar`<br>
|
47
|
-
|
50
|
+
NOTE: experimental. Likely to be renamed, modified or removed soon.<br>
|
51
|
+
Defines both initializer and public readers. Does not define writers as value objects are typically immutable. Defines object equality: two value objects of the same class with the same values are equal.
|
48
52
|
|
49
53
|
`method_object :fooable?, :foo`<br>
|
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.
|
54
|
+
Defines a `.fooable?` class method that takes one argument (`:foo`) and delegates to an instance method that can access `foo` as a private reader, useful for [method objects](http://refactoring.com/catalog/replaceMethodWithMethodObject.html). The `[]` notation for hash arguments is also supported.
|
55
|
+
|
56
|
+
You don't have to specify readers if you don't want them: `method_object :fooable?` is also valid.
|
51
57
|
|
52
58
|
`attr_id_query :foo?, :bar?`<br>
|
53
59
|
Defines query methods like `foo?`, which is true iff `foo_id` is truthy. Goes well with Active Record.
|
data/lib/attr_extras.rb
CHANGED
@@ -49,7 +49,15 @@ module AttrExtras
|
|
49
49
|
|
50
50
|
def attr_value(*names)
|
51
51
|
attr_initialize(*names)
|
52
|
-
|
52
|
+
|
53
|
+
flat_names = attr_flat_names(names)
|
54
|
+
attr_reader *flat_names
|
55
|
+
|
56
|
+
define_method(:==) do |other|
|
57
|
+
return false unless other.is_a?(self.class)
|
58
|
+
|
59
|
+
flat_names.all? { |attr| self.public_send(attr) == other.public_send(attr) }
|
60
|
+
end
|
53
61
|
end
|
54
62
|
|
55
63
|
def method_object(method_name, *names)
|
data/lib/attr_extras/version.rb
CHANGED
data/spec/attr_extras_spec.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require "minitest/autorun"
|
2
4
|
require "minitest/pride"
|
3
5
|
require "attr_extras"
|
4
6
|
|
5
7
|
describe Object, ".method_object" do
|
6
|
-
|
7
|
-
Class.new do
|
8
|
+
it "creates a class method that instantiates and runs that instance method" do
|
9
|
+
klass = Class.new do
|
8
10
|
method_object :fooable?,
|
9
11
|
:foo
|
10
12
|
|
@@ -12,12 +14,22 @@ describe Object, ".method_object" do
|
|
12
14
|
foo
|
13
15
|
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
it "creates a class method that instantiates and runs that instance method" do
|
18
18
|
assert klass.fooable?(true)
|
19
19
|
refute klass.fooable?(false)
|
20
20
|
end
|
21
|
+
|
22
|
+
it "doesn't require attributes" do
|
23
|
+
klass = Class.new do
|
24
|
+
method_object :fooable?
|
25
|
+
|
26
|
+
def fooable?
|
27
|
+
true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
assert klass.fooable?
|
32
|
+
end
|
21
33
|
end
|
22
34
|
|
23
35
|
describe Object, ".attr_initialize" do
|
@@ -130,6 +142,36 @@ describe Object, ".attr_value" do
|
|
130
142
|
example.bar.must_equal "Bar"
|
131
143
|
example.baz.must_equal "Baz"
|
132
144
|
end
|
145
|
+
|
146
|
+
it "defines equality based on the attributes" do
|
147
|
+
klass = Class.new do
|
148
|
+
attr_value :foo, :bar
|
149
|
+
end
|
150
|
+
|
151
|
+
example1 = klass.new("Foo", "Bar")
|
152
|
+
example2 = klass.new("Foo", "Bar")
|
153
|
+
example3 = klass.new("Arroz", "Feijão")
|
154
|
+
|
155
|
+
assert example1 == example2, "Examples should be equal"
|
156
|
+
refute example1 != example2, "Examples should be equal"
|
157
|
+
|
158
|
+
assert example1 != example3, "Examples should not be equal"
|
159
|
+
end
|
160
|
+
|
161
|
+
it "defines equality based on the actual type" do
|
162
|
+
klass1 = Class.new do
|
163
|
+
attr_value :foo
|
164
|
+
end
|
165
|
+
klass2 = Class.new do
|
166
|
+
attr_value :foo
|
167
|
+
end
|
168
|
+
|
169
|
+
example1 = klass1.new("Foo")
|
170
|
+
example2 = klass2.new("Foo")
|
171
|
+
|
172
|
+
assert example1 != example2, "Examples should not be equal"
|
173
|
+
refute example1 == example2, "Examples should not be equal"
|
174
|
+
end
|
133
175
|
end
|
134
176
|
|
135
177
|
describe Object, ".attr_id_query" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|