attr_init 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/attr_init.rb +11 -3
- data/lib/attr_init/version.rb +1 -1
- data/spec/attr_init_spec.rb +19 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8c2f28874937543c9524fbc537b4f5c82ec1a05
|
4
|
+
data.tar.gz: b15e3bde7a91aa499416fd0c2d802106338321d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c86cc3859143e98a0040a19edfee4f0e779db335da6982d2f54517b78150f04f9d3a473470b1fb01974c7e12eec27eeb54386893c24a0b0d21a654a30633b1f1
|
7
|
+
data.tar.gz: 48c19c8851295ed93c75197b750868e153d0f0b101bddfeb217e90abaffdc8e6874eb55d120127594bdea8f4a14c2cf0f4ffebd151f099883971a82017821fa8
|
data/lib/attr_init.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
require "attr_init/version"
|
2
2
|
|
3
3
|
def attr_init(*attrs)
|
4
|
+
begin
|
5
|
+
hidden_attrs = superclass.class_variable_get :@@_hidden_attrs
|
6
|
+
class_variable_set :@@_hidden_attrs, hidden_attrs + attrs
|
7
|
+
rescue NameError => e
|
8
|
+
class_variable_set :@@_hidden_attrs, attrs
|
9
|
+
end
|
10
|
+
|
4
11
|
define_method(:attr_init) do |params|
|
5
|
-
|
12
|
+
self.class.class_variable_get(:@@_hidden_attrs).each do |a|
|
6
13
|
instance_variable_set "@#{a}", params[a]
|
7
14
|
end
|
8
15
|
end
|
@@ -16,8 +23,9 @@ end
|
|
16
23
|
|
17
24
|
def attr_hash(*attrs)
|
18
25
|
define_method(:to_h) do
|
19
|
-
|
20
|
-
|
26
|
+
self.class.class_variable_get(:@@_hidden_attrs)
|
27
|
+
.each_with_object({}) do |attr, hash|
|
28
|
+
hash[attr] = public_send(attr)
|
21
29
|
end
|
22
30
|
end
|
23
31
|
end
|
data/lib/attr_init/version.rb
CHANGED
data/spec/attr_init_spec.rb
CHANGED
@@ -25,6 +25,25 @@ describe AttrInit do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe '::new' do
|
28
|
+
it 'allows super class' do
|
29
|
+
class A
|
30
|
+
reader_struct :a
|
31
|
+
end
|
32
|
+
|
33
|
+
class B < A
|
34
|
+
reader_struct :b
|
35
|
+
def initialize(params)
|
36
|
+
super
|
37
|
+
attr_init(params)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
object = B.new(a: 0, b: 1)
|
42
|
+
|
43
|
+
expect(object.b).to eq 1
|
44
|
+
expect(object.a).to eq 0
|
45
|
+
end
|
46
|
+
|
28
47
|
it 'defines #to_h' do
|
29
48
|
object = reader_struct.new(hash_params)
|
30
49
|
|