attr_extras 6.2.0 → 6.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +24 -3
- data/lib/attr_extras/utils.rb +3 -1
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_extras/aattr_initialize_spec.rb +11 -1
- data/spec/attr_extras/attr_initialize_spec.rb +1 -0
- data/spec/attr_extras/pattr_initialize_spec.rb +9 -0
- data/spec/attr_extras/rattr_initialize_spec.rb +10 -1
- data/spec/attr_extras/utils_spec.rb +19 -0
- data/spec/attr_extras/vattr_initialize_spec.rb +11 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36d34fd523db74313e3c0509e90c7a55447577f6
|
4
|
+
data.tar.gz: 829cdcf1b0cfbd47beaefcbec7910e96cb518cea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfaa6b07c0878a8f3c3f4d73fbbe44da05964ea4ee2e387e70345580692b25172dc0edd2ff349c324d120707b940b2e416001d39581c35b4806e360a4a8f4c8c
|
7
|
+
data.tar.gz: a6cf49dc70ab9cb77ca13d618a985185538d623cc96918a26274bdd4aefdc0fd7ea66234e89845347b32e3e1cea0fd1d4e4edd3f3e53ab2ae4d8a8aac6521edc
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -59,14 +59,35 @@ Also provides conveniences for creating value objects, method objects, query met
|
|
59
59
|
|
60
60
|
`attr_initialize [:bar, :baz!]` defines an initializer that takes two keyword arguments, assigning `@bar` (optional) and `@baz` (required).
|
61
61
|
|
62
|
-
Keyword arguments can have default values:
|
63
|
-
`attr_initialize [:bar, baz: "default value"]` defines an initializer that takes two keyword arguments, assigning `@bar` (optional) and `@baz` (optional with default value `"default value"`).
|
64
|
-
|
65
62
|
If you pass unknown keyword arguments, you will get an `ArgumentError`.
|
66
63
|
If you don't pass required arguments and don't define default value for them, you will get a `KeyError`.
|
67
64
|
|
68
65
|
`attr_initialize` can also accept a block which will be invoked after initialization. This is useful for e.g. initializing private data as necessary.
|
69
66
|
|
67
|
+
#### Default values
|
68
|
+
|
69
|
+
Keyword arguments can have default values:
|
70
|
+
|
71
|
+
`attr_initialize [:bar, baz: "default value"]` defines an initializer that takes two keyword arguments, assigning `@bar` (optional) and `@baz` (optional with default value `"default value"`).
|
72
|
+
|
73
|
+
Note that default values are evaluated *when the class is loaded* and not on every instantition. So `attr_initialize [time: Time.now]` might not do what you expect.
|
74
|
+
|
75
|
+
You can always use regular Ruby methods to achieve this:
|
76
|
+
|
77
|
+
```
|
78
|
+
class Foo
|
79
|
+
attr_initialize [:time]
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def time
|
84
|
+
@time || Time.now
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
Or just use a regular initializer with default values.
|
90
|
+
|
70
91
|
|
71
92
|
### `attr_private`
|
72
93
|
|
data/lib/attr_extras/utils.rb
CHANGED
data/lib/attr_extras/version.rb
CHANGED
@@ -24,7 +24,7 @@ describe Object, ".aattr_initialize" do
|
|
24
24
|
|
25
25
|
it "works with hash ivars" do
|
26
26
|
klass = Class.new do
|
27
|
-
aattr_initialize :foo, [:bar, :baz!]
|
27
|
+
aattr_initialize :foo, [ :bar, :baz! ]
|
28
28
|
end
|
29
29
|
|
30
30
|
example = klass.new("Foo", bar: "Bar", baz: "Baz")
|
@@ -32,6 +32,16 @@ describe Object, ".aattr_initialize" do
|
|
32
32
|
example.baz.must_equal "Baz"
|
33
33
|
end
|
34
34
|
|
35
|
+
it "works with hash ivars and default values" do
|
36
|
+
klass = Class.new do
|
37
|
+
aattr_initialize :foo, [ bar: "Bar", baz!: "Baz" ]
|
38
|
+
end
|
39
|
+
|
40
|
+
example = klass.new("Foo")
|
41
|
+
|
42
|
+
example.baz.must_equal "Baz"
|
43
|
+
end
|
44
|
+
|
35
45
|
it "accepts a block for initialization" do
|
36
46
|
klass = Class.new do
|
37
47
|
aattr_initialize :value do
|
@@ -19,6 +19,15 @@ describe Object, ".pattr_initialize" do
|
|
19
19
|
example.send(:baz).must_equal "Baz"
|
20
20
|
end
|
21
21
|
|
22
|
+
it "works with hash ivars and default values" do
|
23
|
+
klass = Class.new do
|
24
|
+
pattr_initialize :foo, [ bar: "Bar", baz!: "Baz" ]
|
25
|
+
end
|
26
|
+
|
27
|
+
example = klass.new("Foo")
|
28
|
+
example.send(:baz).must_equal "Baz"
|
29
|
+
end
|
30
|
+
|
22
31
|
it "can reference private initializer methods in an initializer block" do
|
23
32
|
klass = Class.new do
|
24
33
|
pattr_initialize :value do
|
@@ -12,13 +12,22 @@ describe Object, ".rattr_initialize" do
|
|
12
12
|
|
13
13
|
it "works with hash ivars" do
|
14
14
|
klass = Class.new do
|
15
|
-
rattr_initialize :foo, [:bar, :baz!]
|
15
|
+
rattr_initialize :foo, [ :bar, :baz! ]
|
16
16
|
end
|
17
17
|
|
18
18
|
example = klass.new("Foo", bar: "Bar", baz: "Baz")
|
19
19
|
example.public_send(:baz).must_equal "Baz"
|
20
20
|
end
|
21
21
|
|
22
|
+
it "works with hash ivars and default values" do
|
23
|
+
klass = Class.new do
|
24
|
+
rattr_initialize :foo, [ bar: "Bar", baz!: "Baz" ]
|
25
|
+
end
|
26
|
+
|
27
|
+
example = klass.new("Foo")
|
28
|
+
example.send(:baz).must_equal "Baz"
|
29
|
+
end
|
30
|
+
|
22
31
|
it "accepts the alias attr_reader_initialize" do
|
23
32
|
klass = Class.new do
|
24
33
|
attr_reader_initialize :foo, :bar
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe AttrExtras::Utils do
|
4
|
+
describe ".flat_names" do
|
5
|
+
subject { AttrExtras::Utils.flat_names(names) }
|
6
|
+
|
7
|
+
it "strips any bangs from a flat list of arguments" do
|
8
|
+
AttrExtras::Utils.flat_names([ :foo, :bar! ]).must_equal [ "foo", "bar" ]
|
9
|
+
end
|
10
|
+
|
11
|
+
it "flattens hash arguments and strips any bangs" do
|
12
|
+
AttrExtras::Utils.flat_names([ :foo, [ :bar, :baz! ] ]).must_equal [ "foo", "bar", "baz" ]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "flattens hash arguments with defaults and strips any bangs" do
|
16
|
+
AttrExtras::Utils.flat_names([ :foo, [ bar: "Bar", baz!: "Baz"] ]).must_equal [ "foo", "bar", "baz" ]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -24,6 +24,17 @@ describe Object, ".vattr_initialize" do
|
|
24
24
|
example1.must_equal example2
|
25
25
|
end
|
26
26
|
|
27
|
+
it "works with hash ivars and default values" do
|
28
|
+
klass = Class.new do
|
29
|
+
vattr_initialize :foo, [ bar: "Bar", baz!: "Baz" ]
|
30
|
+
end
|
31
|
+
|
32
|
+
example1 = klass.new("Foo")
|
33
|
+
example2 = klass.new("Foo")
|
34
|
+
example1.baz.must_equal "Baz"
|
35
|
+
example1.must_equal example2
|
36
|
+
end
|
37
|
+
|
27
38
|
it "can accept an initializer block" do
|
28
39
|
called = false
|
29
40
|
klass = Class.new 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: 6.2.
|
4
|
+
version: 6.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2019-
|
14
|
+
date: 2019-03-25 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: minitest
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- spec/attr_extras/pattr_initialize_spec.rb
|
94
94
|
- spec/attr_extras/rattr_initialize_spec.rb
|
95
95
|
- spec/attr_extras/static_facade_spec.rb
|
96
|
+
- spec/attr_extras/utils_spec.rb
|
96
97
|
- spec/attr_extras/vattr_initialize_spec.rb
|
97
98
|
- spec/attr_extras_spec.rb
|
98
99
|
- spec/spec_helper.rb
|
@@ -135,6 +136,7 @@ test_files:
|
|
135
136
|
- spec/attr_extras/pattr_initialize_spec.rb
|
136
137
|
- spec/attr_extras/rattr_initialize_spec.rb
|
137
138
|
- spec/attr_extras/static_facade_spec.rb
|
139
|
+
- spec/attr_extras/utils_spec.rb
|
138
140
|
- spec/attr_extras/vattr_initialize_spec.rb
|
139
141
|
- spec/attr_extras_spec.rb
|
140
142
|
- spec/spec_helper.rb
|