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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc859365c8ea9d1d78cf83feaed062c4f843822c
4
- data.tar.gz: e432f2b7cbf1199b03169e82ce9a04bd6726f765
3
+ metadata.gz: d8c2f28874937543c9524fbc537b4f5c82ec1a05
4
+ data.tar.gz: b15e3bde7a91aa499416fd0c2d802106338321d8
5
5
  SHA512:
6
- metadata.gz: d406ab5e4409f4419090a8525e5bbff4c0be0797c5e74dc31a7f1b64624e2394033c1df0a7de2883eac495426755c5d6aef982c6ad5d4d2e681ef8b8b4befb8a
7
- data.tar.gz: fc6414ba838611d57a8d9df5c8e8b85b596e2b2e16f52e1f4893bab96bd4cba94ee1625c3cacceb61e914e4c5ad8761f4c1247a7004332ed8bd542d2dd864d12
6
+ metadata.gz: c86cc3859143e98a0040a19edfee4f0e779db335da6982d2f54517b78150f04f9d3a473470b1fb01974c7e12eec27eeb54386893c24a0b0d21a654a30633b1f1
7
+ data.tar.gz: 48c19c8851295ed93c75197b750868e153d0f0b101bddfeb217e90abaffdc8e6874eb55d120127594bdea8f4a14c2cf0f4ffebd151f099883971a82017821fa8
@@ -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
- attrs.each do |a|
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
- attrs.each_with_object({}) do |attr, hash|
20
- hash[attr] = send(attr)
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
@@ -1,3 +1,3 @@
1
1
  module AttrInit
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_init
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - johnmcconnell