attr_extras 4.6.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/attr_extras/attr_initialize.rb +6 -0
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_extras/attr_initialize_spec.rb +12 -0
- 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: fd15c690db7cddf4aa28a7bd1c461d01b07883e5
|
4
|
+
data.tar.gz: 02a2356ba8d7a3ebd8a5dc944a92cd3a337df577
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7fe4495e99a5bfde538d23bb1d39e48add881babe4da8c9b3d819bb3803db465c7a8452070a4e02c1ca772ac1a8572c1b5b83c36df9ad3bf38dd5b218a5ff6c
|
7
|
+
data.tar.gz: 0c3f59b421f30e9ac25702851ce9fae3c56f4286b4d9e27fef241a2f0753162bdbd3cb067704999fb28f34e29661c2d62d83293030d57a48664ce834d6b47942
|
data/README.md
CHANGED
@@ -58,6 +58,8 @@ Also provides conveniences for creating value objects, method objects, query met
|
|
58
58
|
|
59
59
|
`attr_initialize [:bar, :baz!]` defines an initializer that takes one hash argument, assigning `@bar` (optional) and `@baz` (required).
|
60
60
|
|
61
|
+
If you pass unknown hash arguments, you will get an `ArgumentError`.
|
62
|
+
|
61
63
|
`attr_initialize` can also accept a block which will be invoked after initialization. This is useful for e.g. initializing private data as necessary.
|
62
64
|
|
63
65
|
|
@@ -21,6 +21,12 @@ class AttrExtras::AttrInitialize
|
|
21
21
|
if name_or_names.is_a?(Array)
|
22
22
|
hash = value || {}
|
23
23
|
|
24
|
+
known_keys = name_or_names.map { |name| name.to_s.sub(/!\z/, "").to_sym }
|
25
|
+
unknown_keys = hash.keys - known_keys
|
26
|
+
if unknown_keys.any?
|
27
|
+
raise ArgumentError, "Got unknown keys: #{unknown_keys.inspect}; allowed keys: #{known_keys.inspect}"
|
28
|
+
end
|
29
|
+
|
24
30
|
name_or_names.each do |name|
|
25
31
|
set_ivar_from_hash.call(self, name, hash)
|
26
32
|
end
|
data/lib/attr_extras/version.rb
CHANGED
@@ -56,6 +56,18 @@ describe Object, ".attr_initialize" do
|
|
56
56
|
lambda { klass.new(:optional => "X") }.must_raise KeyError
|
57
57
|
end
|
58
58
|
|
59
|
+
it "complains about unknown hash values" do
|
60
|
+
klass = Class.new do
|
61
|
+
attr_initialize :foo, [:bar]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Should not raise.
|
65
|
+
klass.new("Foo", :bar => "Bar")
|
66
|
+
|
67
|
+
exception = lambda { klass.new("Foo", :bar => "Bar", :baz => "Baz") }.must_raise ArgumentError
|
68
|
+
exception.message.must_include "baz"
|
69
|
+
end
|
70
|
+
|
59
71
|
it "accepts a block for initialization" do
|
60
72
|
klass = Class.new do
|
61
73
|
attr_initialize :value 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:
|
4
|
+
version: 5.0.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: 2016-06-
|
13
|
+
date: 2016-06-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: minitest
|