attr_extras 1.7.0 → 1.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ffd8abe1675a431f0b748464aae94e5e45ff1305
4
- data.tar.gz: eb248dbd2a6e75dec4e9fe44c9a999087bc9fcb9
3
+ metadata.gz: 0749835eb5235d2f2171a3da61eb87953d574cb9
4
+ data.tar.gz: f52712ce7b9e9d15f0e0e1efe9208b13ce5931ab
5
5
  SHA512:
6
- metadata.gz: cb1c750b44e30aa2f387175e57a259a48c2b1b5bafab3acd937da929cb62fa18519ed2b664d55fd05fbdf8d685d93ac192ca3b56c53cdf63a168cc5477c83e84
7
- data.tar.gz: 62e1c97fd22b160a960a25532a62a36ad59604a208f45a66be0174490f56c33ff21466815fc6a17b53567bfe683919a2c3f6e0c4bdcf2bf02fc07b4c3b93a8e6
6
+ metadata.gz: 4bccfdba5b88e7944b199dcf07d214b33b082f22c1128df429cb8fd80f274fab86e21024ec43a4be7154391f95ca078584994ed29828fe8018d8ab9454be3dbd
7
+ data.tar.gz: 71b0501a29618a80d824ab1eb69b07753eb3f4ff324d103b2c35c02190172e2465bbee5810707193ab1cd2b503937fb1032fcdc455bb9e08a6f6ff982357e5a1
data/README.md CHANGED
@@ -34,8 +34,8 @@ This nicely complements Ruby's built-in `attr_accessor`, `attr_reader` and `attr
34
34
  `attr_initialize :foo, :bar`<br>
35
35
  Defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
36
36
 
37
- `attr_initialize :foo, [:bar, :baz]`<br>
38
- Defines an initializer that takes one regular argument, assigning `@foo`, and one hash argument, assigning `@bar` and `@baz`.
37
+ `attr_initialize :foo, [:bar, :baz!]`<br>
38
+ Defines an initializer that takes one regular argument, assigning `@foo`, and one hash argument, assigning `@bar` (optional) and `@baz` (required).
39
39
 
40
40
  `attr_private :foo, :bar`<br>
41
41
  Defines private readers for `@foo` and `@bar`.
data/attr_extras.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
  require File.expand_path('../lib/attr_extras/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Henrik Nyh"]
5
+ gem.authors = ["Henrik Nyh", "Joakim Kolsjö"]
6
6
  gem.email = ["henrik@nyh.se"]
7
7
  gem.summary = %q{Takes some boilerplate out of Ruby with methods like attr_initialize.}
8
8
  gem.homepage = "https://github.com/barsoom/attr_extras"
data/lib/attr_extras.rb CHANGED
@@ -18,11 +18,20 @@ module AttrExtras
18
18
  if name_or_names.is_a?(Array)
19
19
  value ||= {}
20
20
 
21
- name_or_names.each do |key|
22
- instance_variable_set("@#{key}", value[key])
21
+ name_or_names.each do |name|
22
+ if name.to_s.end_with?("!")
23
+ actual_name = name.to_s.chop.to_sym
24
+ actual_value = value.fetch(actual_name)
25
+ else
26
+ actual_name = name
27
+ actual_value = value[name]
28
+ end
29
+
30
+ instance_variable_set("@#{actual_name}", actual_value)
23
31
  end
24
32
  else
25
- instance_variable_set("@#{name_or_names}", value)
33
+ name = name_or_names
34
+ instance_variable_set("@#{name}", value)
26
35
  end
27
36
  end
28
37
  end
@@ -35,7 +44,9 @@ module AttrExtras
35
44
 
36
45
  def pattr_initialize(*names)
37
46
  attr_initialize(*names)
38
- attr_private(*names.flatten)
47
+
48
+ flat_names = names.flatten.map { |x| x.to_s.sub(/!\z/, "") }
49
+ attr_private(*flat_names)
39
50
  end
40
51
 
41
52
  def attr_value(*names)
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "1.7.0"
2
+ VERSION = "1.8.0"
3
3
  end
@@ -39,7 +39,7 @@ describe Object, ".attr_initialize" do
39
39
 
40
40
  it "can set ivars from a hash" do
41
41
  klass = Class.new do
42
- pattr_initialize :foo, [:bar, :baz]
42
+ attr_initialize :foo, [:bar, :baz]
43
43
  end
44
44
 
45
45
  example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
@@ -50,7 +50,7 @@ describe Object, ".attr_initialize" do
50
50
 
51
51
  it "treats hash values as optional" do
52
52
  klass = Class.new do
53
- pattr_initialize :foo, [:bar, :baz]
53
+ attr_initialize :foo, [:bar, :baz]
54
54
  end
55
55
 
56
56
  example = klass.new("Foo", :bar => "Bar")
@@ -59,6 +59,17 @@ describe Object, ".attr_initialize" do
59
59
  example = klass.new("Foo")
60
60
  example.instance_variable_get("@bar").must_equal nil
61
61
  end
62
+
63
+ it "can require hash values" do
64
+ klass = Class.new do
65
+ attr_initialize [:optional, :required!]
66
+ end
67
+
68
+ example = klass.new(:required => "X")
69
+ example.instance_variable_get("@required").must_equal "X"
70
+
71
+ lambda { klass.new(:optional => "X") }.must_raise KeyError
72
+ end
62
73
  end
63
74
 
64
75
  describe Object, ".attr_private" do
@@ -90,11 +101,12 @@ describe Object, ".pattr_initialize" do
90
101
 
91
102
  it "works with hash ivars" do
92
103
  klass = Class.new do
93
- pattr_initialize :foo, [:bar, :baz]
104
+ pattr_initialize :foo, [:bar, :baz!]
94
105
  end
95
106
 
96
- example = klass.new("Foo", :bar => "Bar")
107
+ example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
97
108
  example.send(:bar).must_equal "Bar"
109
+ example.send(:baz).must_equal "Baz"
98
110
  end
99
111
  end
100
112
 
metadata CHANGED
@@ -1,10 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
8
+ - Joakim Kolsjö
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []