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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38b2b1b3e247d7f1dbc20e44110939f9ff6366e0
4
- data.tar.gz: df85b55e2c60037a153f6127dc9758ce329b9b2b
3
+ metadata.gz: fd15c690db7cddf4aa28a7bd1c461d01b07883e5
4
+ data.tar.gz: 02a2356ba8d7a3ebd8a5dc944a92cd3a337df577
5
5
  SHA512:
6
- metadata.gz: c1a61d4507ca8ce8f60e0def7521cde84e6918025125f0573add4b3271b1edcd0cf831d7c9f34a37970951abe8051339ebe83b1e33aa9fcf9efa4b86e4f46731
7
- data.tar.gz: 9c17ccb154eda8c4790654c80ab32a5ccd34e4dbf06dc31717e3b6511b8e8ee19d2370f50496b04beb17af582689c930e2eb77d1a753de9223a09deb753782f0
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
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "4.6.0"
2
+ VERSION = "5.0.0"
3
3
  end
@@ -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.6.0
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-16 00:00:00.000000000 Z
13
+ date: 2016-06-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest