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 +4 -4
- data/README.md +2 -2
- data/attr_extras.gemspec +1 -1
- data/lib/attr_extras.rb +15 -4
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_extras_spec.rb +16 -4
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0749835eb5235d2f2171a3da61eb87953d574cb9
|
4
|
+
data.tar.gz: f52712ce7b9e9d15f0e0e1efe9208b13ce5931ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 |
|
22
|
-
|
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
|
-
|
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
|
-
|
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)
|
data/lib/attr_extras/version.rb
CHANGED
data/spec/attr_extras_spec.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
|