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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a03ce9220bfff01deb8bd149a84375a38873bc7f
4
- data.tar.gz: d4b97c8e31eaaa0e12a44fc872f7882325ed94d7
3
+ metadata.gz: 36d34fd523db74313e3c0509e90c7a55447577f6
4
+ data.tar.gz: 829cdcf1b0cfbd47beaefcbec7910e96cb518cea
5
5
  SHA512:
6
- metadata.gz: e8a01917e46705498fafd11c0df34424cc0cf35c69d9376c091f7ceb58b08553ee528e1ccf9fdc2fbf2961352745ecfa9ec030c1b9dac513b432cb6231926a32
7
- data.tar.gz: 449c6e92127085a5efb2b8021a6335de05bce8a75008ec51ab523956eb057ce2cdd2d6384206ede335ec368f93a6ed8e3c130cfdbbfcbeb9f76aef609f7bd299
6
+ metadata.gz: bfaa6b07c0878a8f3c3f4d73fbbe44da05964ea4ee2e387e70345580692b25172dc0edd2ff349c324d120707b940b2e416001d39581c35b4806e360a4a8f4c8c
7
+ data.tar.gz: a6cf49dc70ab9cb77ca13d618a985185538d623cc96918a26274bdd4aefdc0fd7ea66234e89845347b32e3e1cea0fd1d4e4edd3f3e53ab2ae4d8a8aac6521edc
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ # 6.2.1
4
+
5
+ * Bugfix with keyword argument defaults. Thanks to [Roman Dubrovsky](https://github.com/barsoom/attr_extras/pull/29)!
6
+
3
7
  # 6.2.0
4
8
 
5
9
  * Another bugfix when passing hash values to positional arguments.
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
 
@@ -1,5 +1,7 @@
1
1
  module AttrExtras::Utils
2
2
  def self.flat_names(names)
3
- names.flatten.map { |x| x.to_s.sub(/!\z/, "") }
3
+ names.flatten.
4
+ flat_map { |x| x.is_a?(Hash) ? x.keys : x }.
5
+ map { |x| x.to_s.sub(/!\z/, "") }
4
6
  end
5
7
  end
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "6.2.0"
2
+ VERSION = "6.2.1"
3
3
  end
@@ -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
@@ -112,6 +112,7 @@ describe Object, ".attr_initialize" do
112
112
 
113
113
  # Should not raise.
114
114
  example = klass.new({ "invalid.ivar.name" => 123 })
115
+
115
116
  example.foo.must_equal({ "invalid.ivar.name" => 123 })
116
117
  end
117
118
 
@@ -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.0
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-01-31 00:00:00.000000000 Z
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