attr_extras 4.1.0 → 4.2.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 +54 -19
- data/lib/attr_extras.rb +11 -0
- data/lib/attr_extras/version.rb +1 -1
- data/spec/pattr_initialize_spec.rb +9 -0
- data/spec/rattr_initialize_spec.rb +30 -0
- data/spec/vattr_initialize_spec.rb +12 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02abc2f4fe382b84b370d6f777be15a50bf136c7
|
4
|
+
data.tar.gz: bfee4d693acfdb8c1a15f9ed50677be6f21149a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a44ccd28222d3708b7f4b87d74f7f1a605b462ef8980e6b510844e8eca0aa2db9e4f0ae12df38f33b66bf9bbf4a48b08321763ba223ab8bea0abd2eb5c9bfe0
|
7
|
+
data.tar.gz: eff2da3b058ff22016362fec1e5a92e5a9c3cc5c1d88eea67341cc4bfec9b421b572d4296996c63b3baf50dbe168f150aae5111523ac0cee72afaad9f707dcb2
|
data/README.md
CHANGED
@@ -35,11 +35,12 @@ Also provides conveniences for creating value objects, method objects, query met
|
|
35
35
|
|
36
36
|
## Usage
|
37
37
|
|
38
|
-
* [`pattr_initialize`](#pattr_initialize)
|
39
|
-
* [`vattr_initialize`](#vattr_initialize)
|
40
38
|
* [`attr_initialize`](#attr_initialize)
|
41
39
|
* [`attr_private`](#attr_private)
|
42
40
|
* [`attr_value`](#attr_value)
|
41
|
+
* [`pattr_initialize`](#pattr_initialize) / [`attr_private_initialize`](#attr_private_initialize)
|
42
|
+
* [`vattr_initialize`](#vattr_initialize) / [`attr_value_initialize`](#attr_value_initialize)
|
43
|
+
* [`rattr_initialize`](#rattr_initialize) / [`attr_reader_initialize`](#attr_reader_initialize)
|
43
44
|
* [`static_facade`](#static_facade)
|
44
45
|
* [`method_object`](#method_object)
|
45
46
|
* [`attr_implement`](#attr_implement)
|
@@ -47,16 +48,41 @@ Also provides conveniences for creating value objects, method objects, query met
|
|
47
48
|
* [`attr_id_query`](#attr_id_query)
|
48
49
|
|
49
50
|
|
51
|
+
### `attr_initialize`
|
52
|
+
|
53
|
+
`attr_initialize :foo, :bar` defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
|
54
|
+
|
55
|
+
`attr_initialize :foo, [:bar, :baz!]` defines an initializer that takes one regular argument, assigning `@foo`, and one hash argument, assigning `@bar` (optional) and `@baz` (required).
|
56
|
+
|
57
|
+
`attr_initialize [:bar, :baz!]` defines an initializer that takes one hash argument, assigning `@bar` (optional) and `@baz` (required).
|
58
|
+
|
59
|
+
`attr_initialize` can also accept a block which will be invoked after initialization. This is useful for calling `super` appropriately in subclasses or initializing private data as necessary.
|
60
|
+
|
61
|
+
|
62
|
+
### `attr_private`
|
63
|
+
|
64
|
+
`attr_private :foo, :bar` defines private readers for `@foo` and `@bar`.
|
65
|
+
|
66
|
+
|
67
|
+
### `attr_value`
|
68
|
+
|
69
|
+
`attr_value :foo, :bar` defines public readers for `@foo` and `@bar` and also defines object equality: two value objects of the same class with the same values will be considered equal (with `==` and `eql?`, in `Set`s, as `Hash` keys etc).
|
70
|
+
|
71
|
+
It does not define writers, because [value objects](http://en.wikipedia.org/wiki/Value_object) are typically immutable.
|
72
|
+
|
50
73
|
|
51
74
|
### `pattr_initialize`
|
75
|
+
### `attr_private_initialize`
|
52
76
|
|
53
|
-
`pattr_initialize :foo, :bar` defines both initializer and private readers
|
77
|
+
`pattr_initialize :foo, :bar` defines both initializer and private readers. Shortcut for:
|
54
78
|
|
55
79
|
``` ruby
|
56
80
|
attr_initialize :foo, :bar
|
57
81
|
attr_private :foo, :bar
|
58
82
|
```
|
59
83
|
|
84
|
+
`pattr_initialize` is aliased as `attr_private_initialize` if you prefer a longer but clearer name.
|
85
|
+
|
60
86
|
Example:
|
61
87
|
|
62
88
|
``` ruby
|
@@ -73,16 +99,18 @@ Item.new("Pug", 100).price_with_vat # => 125.0
|
|
73
99
|
|
74
100
|
[The `attr_initialize` notation](#attr_initialize) for hash arguments is also supported: `pattr_initialize :foo, [:bar, :baz!]`
|
75
101
|
|
76
|
-
|
77
102
|
### `vattr_initialize`
|
103
|
+
### `attr_value_initialize`
|
78
104
|
|
79
|
-
`vattr_initialize :foo, :bar` defines initializer, public readers and [value object identity](#attr_value)
|
105
|
+
`vattr_initialize :foo, :bar` defines initializer, public readers and [value object identity](#attr_value). Shortcut for:
|
80
106
|
|
81
107
|
``` ruby
|
82
108
|
attr_initialize :foo, :bar
|
83
109
|
attr_value :foo, :bar
|
84
110
|
```
|
85
111
|
|
112
|
+
`vattr_initialize` is aliased as `attr_value_initialize` if you prefer a longer but clearer name.
|
113
|
+
|
86
114
|
Example:
|
87
115
|
|
88
116
|
``` ruby
|
@@ -97,28 +125,34 @@ Country.new("SE").code # => "SE"
|
|
97
125
|
[The `attr_initialize` notation](#attr_initialize) for hash arguments is also supported: `vattr_initialize :foo, [:bar, :baz!]`
|
98
126
|
|
99
127
|
|
100
|
-
### `
|
101
|
-
|
102
|
-
`attr_initialize :foo, :bar` defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
|
103
|
-
|
104
|
-
`attr_initialize :foo, [:bar, :baz!]` defines an initializer that takes one regular argument, assigning `@foo`, and one hash argument, assigning `@bar` (optional) and `@baz` (required).
|
105
|
-
|
106
|
-
`attr_initialize [:bar, :baz!]` defines an initializer that takes one hash argument, assigning `@bar` (optional) and `@baz` (required).
|
107
|
-
|
108
|
-
`attr_initialize` can also accept a block which will be invoked after initialization. This is useful for calling `super` appropriately in subclasses or initializing private data as necessary.
|
128
|
+
### `rattr_initialize`
|
129
|
+
### `attr_reader_initialize`
|
109
130
|
|
131
|
+
`rattr_initialize :foo, :bar` defines both initializer and public readers. Shortcut for:
|
110
132
|
|
111
|
-
|
133
|
+
``` ruby
|
134
|
+
attr_initialize :foo, :bar
|
135
|
+
attr_reader :foo, :bar
|
136
|
+
```
|
112
137
|
|
113
|
-
`
|
138
|
+
`rattr_initialize` is aliased as `attr_reader_initialize` if you prefer a longer but clearer name.
|
114
139
|
|
140
|
+
Example:
|
115
141
|
|
116
|
-
|
142
|
+
``` ruby
|
143
|
+
class PublishBook
|
144
|
+
rattr_initalize :book_name, :publisher_backend
|
117
145
|
|
118
|
-
|
146
|
+
def call
|
147
|
+
publisher_backend.publish book_name
|
148
|
+
end
|
149
|
+
end
|
119
150
|
|
120
|
-
|
151
|
+
service = PublishBook.new("A Novel")
|
152
|
+
service.book_name # => "A Novel"
|
153
|
+
```
|
121
154
|
|
155
|
+
[The `attr_initialize` notation](#attr_initialize) for hash arguments is also supported: `rattr_initialize :foo, [:bar, :baz!]`
|
122
156
|
|
123
157
|
### `static_facade`
|
124
158
|
|
@@ -307,6 +341,7 @@ Or to see warnings (try not to have any):
|
|
307
341
|
* [Teo Ljungberg](https://github.com/teoljungberg)
|
308
342
|
* [Kim Persson](https://github.com/lavinia)
|
309
343
|
* [Joe Ferris](https://github.com/jferris)
|
344
|
+
* [Julien Vanier](https://github.com/monkbroc)
|
310
345
|
|
311
346
|
|
312
347
|
## License
|
data/lib/attr_extras.rb
CHANGED
@@ -41,11 +41,22 @@ module AttrExtras
|
|
41
41
|
attr_private(*Utils.flat_names(names))
|
42
42
|
end
|
43
43
|
|
44
|
+
alias_method :attr_private_initialize, :pattr_initialize
|
45
|
+
|
44
46
|
def vattr_initialize(*names, &block)
|
45
47
|
attr_initialize(*names, &block)
|
46
48
|
attr_value(*Utils.flat_names(names))
|
47
49
|
end
|
48
50
|
|
51
|
+
alias_method :attr_value_initialize, :vattr_initialize
|
52
|
+
|
53
|
+
def rattr_initialize(*names, &block)
|
54
|
+
attr_initialize(*names, &block)
|
55
|
+
attr_reader(*Utils.flat_names(names))
|
56
|
+
end
|
57
|
+
|
58
|
+
alias_method :attr_reader_initialize, :rattr_initialize
|
59
|
+
|
49
60
|
def static_facade(method_name, *names)
|
50
61
|
define_singleton_method(method_name) do |*values|
|
51
62
|
new(*values).public_send(method_name)
|
data/lib/attr_extras/version.rb
CHANGED
@@ -32,4 +32,13 @@ describe Object, ".pattr_initialize" do
|
|
32
32
|
|
33
33
|
example.copy.must_equal "expected"
|
34
34
|
end
|
35
|
+
|
36
|
+
it "accepts the alias attr_private_initializer" do
|
37
|
+
klass = Class.new do
|
38
|
+
attr_private_initialize :foo, :bar
|
39
|
+
end
|
40
|
+
|
41
|
+
example = klass.new("Foo", "Bar")
|
42
|
+
example.send(:foo).must_equal "Foo"
|
43
|
+
end
|
35
44
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
describe Object, ".rattr_initialize" do
|
4
|
+
it "creates both initializer and public readers" do
|
5
|
+
klass = Class.new do
|
6
|
+
rattr_initialize :foo, :bar
|
7
|
+
end
|
8
|
+
|
9
|
+
example = klass.new("Foo", "Bar")
|
10
|
+
example.public_send(:foo).must_equal "Foo"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "works with hash ivars" do
|
14
|
+
klass = Class.new do
|
15
|
+
rattr_initialize :foo, [:bar, :baz!]
|
16
|
+
end
|
17
|
+
|
18
|
+
example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
|
19
|
+
example.public_send(:baz).must_equal "Baz"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "accepts the alias attr_reader_initializer" do
|
23
|
+
klass = Class.new do
|
24
|
+
attr_reader_initialize :foo, :bar
|
25
|
+
end
|
26
|
+
|
27
|
+
example = klass.new("Foo", "Bar")
|
28
|
+
example.public_send(:foo).must_equal "Foo"
|
29
|
+
end
|
30
|
+
end
|
@@ -36,4 +36,16 @@ describe Object, ".vattr_initialize" do
|
|
36
36
|
|
37
37
|
called.must_equal true
|
38
38
|
end
|
39
|
+
|
40
|
+
it "accepts the alias attr_value_initializer" do
|
41
|
+
klass = Class.new do
|
42
|
+
attr_value_initialize :foo, :bar
|
43
|
+
end
|
44
|
+
|
45
|
+
example1 = klass.new("Foo", "Bar")
|
46
|
+
example2 = klass.new("Foo", "Bar")
|
47
|
+
|
48
|
+
example1.foo.must_equal "Foo"
|
49
|
+
example1.must_equal example2
|
50
|
+
end
|
39
51
|
end
|
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: 4.
|
4
|
+
version: 4.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-02-
|
13
|
+
date: 2015-02-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: minitest
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- spec/attr_value_spec.rb
|
71
71
|
- spec/method_object_spec.rb
|
72
72
|
- spec/pattr_initialize_spec.rb
|
73
|
+
- spec/rattr_initialize_spec.rb
|
73
74
|
- spec/spec_helper.rb
|
74
75
|
- spec/static_facade_spec.rb
|
75
76
|
- spec/vattr_initialize_spec.rb
|
@@ -93,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
94
|
version: '0'
|
94
95
|
requirements: []
|
95
96
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.4.5
|
97
98
|
signing_key:
|
98
99
|
specification_version: 4
|
99
100
|
summary: Takes some boilerplate out of Ruby with methods like attr_initialize.
|
@@ -107,6 +108,7 @@ test_files:
|
|
107
108
|
- spec/attr_value_spec.rb
|
108
109
|
- spec/method_object_spec.rb
|
109
110
|
- spec/pattr_initialize_spec.rb
|
111
|
+
- spec/rattr_initialize_spec.rb
|
110
112
|
- spec/spec_helper.rb
|
111
113
|
- spec/static_facade_spec.rb
|
112
114
|
- spec/vattr_initialize_spec.rb
|