attr_init 0.0.3 → 0.0.4

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: d8c2f28874937543c9524fbc537b4f5c82ec1a05
4
- data.tar.gz: b15e3bde7a91aa499416fd0c2d802106338321d8
3
+ metadata.gz: 9da3988b53195faca639b398ebe6dbe700f5b693
4
+ data.tar.gz: 48c0be1f2c8e5eb5a054229c7c43e5b7dad8ffd2
5
5
  SHA512:
6
- metadata.gz: c86cc3859143e98a0040a19edfee4f0e779db335da6982d2f54517b78150f04f9d3a473470b1fb01974c7e12eec27eeb54386893c24a0b0d21a654a30633b1f1
7
- data.tar.gz: 48c19c8851295ed93c75197b750868e153d0f0b101bddfeb217e90abaffdc8e6874eb55d120127594bdea8f4a14c2cf0f4ffebd151f099883971a82017821fa8
6
+ metadata.gz: 8874ee0a9e363a96b2869cf682f2f12c377eb665a0017d29e4f34a083bc23513df902b119e9d9054ffca06b2b6c702fc3bbcdf16c5030b4618cc575362071053
7
+ data.tar.gz: 3fe9769685289086376a14f59c0db1bb8cab2ca353e8e7603eb6b40fd41b5e9f142f233931047d8651d48bdb6bc348adf53f3939140acfd51e14858cbe1d03ce
data/README.md CHANGED
@@ -8,8 +8,9 @@ Status](https://coveralls.io/repos/johnmcconnell/attr_init/badge.png)](https://c
8
8
 
9
9
  So ruby has `Struct` but I never use it because:
10
10
  1. I have to extend the class with `Struct`
11
- 2. It makes your instance_variables public
11
+ 2. It makes your `instance_variables` public
12
12
  3. It does not use hash initialization
13
+ 4. It does not support inheritance!!!
13
14
 
14
15
  ## Installation
15
16
 
@@ -1,15 +1,10 @@
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
4
+ AttrInit.add_new_attrs(self, attrs)
10
5
 
11
6
  define_method(:attr_init) do |params|
12
- self.class.class_variable_get(:@@_hidden_attrs).each do |a|
7
+ AttrInit.get_attrs(self.class).each do |a|
13
8
  instance_variable_set "@#{a}", params[a]
14
9
  end
15
10
  end
@@ -23,8 +18,7 @@ end
23
18
 
24
19
  def attr_hash(*attrs)
25
20
  define_method(:to_h) do
26
- self.class.class_variable_get(:@@_hidden_attrs)
27
- .each_with_object({}) do |attr, hash|
21
+ AttrInit.get_attrs(self.class).each_with_object({}) do |attr, hash|
28
22
  hash[attr] = public_send(attr)
29
23
  end
30
24
  end
@@ -43,4 +37,20 @@ def accessor_struct(*attrs)
43
37
  end
44
38
 
45
39
  module AttrInit
40
+ def self.add_new_attrs(scope, attrs)
41
+ begin
42
+ hidden_attrs = get_attrs(scope.superclass)
43
+ scope.class_variable_set hidden_attrs_variable_name, hidden_attrs + attrs
44
+ rescue NameError => e
45
+ scope.class_variable_set hidden_attrs_variable_name, attrs
46
+ end
47
+ end
48
+
49
+ def self.get_attrs(scope)
50
+ scope.class_variable_get hidden_attrs_variable_name
51
+ end
52
+
53
+ def self.hidden_attrs_variable_name
54
+ :@@_hidden_attrs
55
+ end
46
56
  end
@@ -1,3 +1,3 @@
1
1
  module AttrInit
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,5 +1,5 @@
1
- require 'spec_helper'
2
1
  require 'attr_init'
2
+ require 'spec_helper'
3
3
 
4
4
  describe AttrInit do
5
5
  let(:symbols) do
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - johnmcconnell