attr_init 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/lib/attr_init.rb +19 -9
- data/lib/attr_init/version.rb +1 -1
- data/spec/attr_init_spec.rb +1 -1
- 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: 9da3988b53195faca639b398ebe6dbe700f5b693
|
4
|
+
data.tar.gz: 48c0be1f2c8e5eb5a054229c7c43e5b7dad8ffd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
|
data/lib/attr_init.rb
CHANGED
@@ -1,15 +1,10 @@
|
|
1
1
|
require "attr_init/version"
|
2
2
|
|
3
3
|
def attr_init(*attrs)
|
4
|
-
|
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
|
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.
|
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
|
data/lib/attr_init/version.rb
CHANGED
data/spec/attr_init_spec.rb
CHANGED