recursive_struct 0.0.5 → 0.0.6
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 +8 -8
- data/lib/recursive_struct.rb +14 -22
- data/lib/recursive_struct/data.rb +35 -0
- data/lib/recursive_struct/version.rb +1 -1
- data/spec/recursive_struct_spec.rb +10 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZDZjODIwNzIwODg3YjQ1ZmM2OGM4Zjg4M2E5ZDZmZGVjZjdhMjFkMg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWRmNWUwMWZlMTkyNWZiM2ZiMGI3ODZiMjFhNjQzNjkxNzJmNjMyOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGI0NWFkMzc2NWM4NDRmMTkzYjcyZmYzMzRjODZhMjA4MDY1YzEzNzYyYWVk
|
10
|
+
YWJmY2IwMmY0MjBhZmI0ODY1N2QyMDFjNDVjMzJiZWZkMTI2MDIxMTI0YmNm
|
11
|
+
ZmM1OWQzNTgxNDM1NmExYzFjNzBjODIzM2UwMTc2ZWU2YmExZDY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzY5NmE3ZWJhZTFjZDljNDM1MTIwMTQyN2U5NDdiYWIwODJmNmQ2NmQ5ZjVi
|
14
|
+
MzU4OWM1MjFiNzQ4MGJlODk5YzRkOTgyYjJmYWEzY2EyYzhlZWVkMDRjMDI0
|
15
|
+
NWVjMzc2MWVhOWVjYTA2NTFmNzU5ZGFhYTE5ZDU5N2ZiMjM0MDU=
|
data/lib/recursive_struct.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
+
require 'recursive_struct/data'
|
2
|
+
|
1
3
|
class RecursiveStruct
|
4
|
+
include RecursiveStruct::Data
|
5
|
+
|
2
6
|
def initialize(hash = nil)
|
3
7
|
if hash
|
4
8
|
hash.each do |key, value|
|
5
|
-
|
9
|
+
set_data(key, value)
|
6
10
|
end
|
7
11
|
end
|
8
12
|
end
|
@@ -10,8 +14,10 @@ class RecursiveStruct
|
|
10
14
|
def method_missing(name, *args)
|
11
15
|
key = name.to_s
|
12
16
|
|
13
|
-
if key.
|
14
|
-
|
17
|
+
if key.include?('=')
|
18
|
+
add_setter(key.chomp('='), *args)
|
19
|
+
elsif data.respond_to?(key)
|
20
|
+
send_data(key, *args)
|
15
21
|
elsif args.length == 0
|
16
22
|
get_data(key)
|
17
23
|
else
|
@@ -25,25 +31,11 @@ class RecursiveStruct
|
|
25
31
|
value.is_a?(Hash) ? self.class.new(value) : value
|
26
32
|
end
|
27
33
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
key = key.to_sym
|
34
|
-
|
35
|
-
data[key] = value
|
36
|
-
define_methods(key)
|
37
|
-
end
|
38
|
-
|
39
|
-
def get_data(key)
|
40
|
-
data[key.to_sym]
|
41
|
-
end
|
42
|
-
|
43
|
-
def define_methods(key)
|
44
|
-
unless respond_to?(key)
|
45
|
-
define_singleton_method(key) { data[key] }
|
46
|
-
define_singleton_method("#{key}=") { |value| data[key] = value }
|
34
|
+
def add_setter(key, *args)
|
35
|
+
if key == '[]' && args.length == 2
|
36
|
+
set_data(args.first, args.last)
|
37
|
+
elsif args.length == 1
|
38
|
+
set_data(key, args.first)
|
47
39
|
end
|
48
40
|
end
|
49
41
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class RecursiveStruct
|
2
|
+
module Data
|
3
|
+
def data
|
4
|
+
@data ||= {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def set_data(key, value)
|
8
|
+
key = key.to_sym
|
9
|
+
|
10
|
+
data[key] = process(value)
|
11
|
+
define_methods(key)
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_data(key)
|
15
|
+
data[key.to_sym]
|
16
|
+
end
|
17
|
+
|
18
|
+
def send_data(key, *args)
|
19
|
+
data.send(key, *args)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def process(value)
|
25
|
+
value.is_a?(Hash) ? RecursiveStruct.new(value) : value
|
26
|
+
end
|
27
|
+
|
28
|
+
def define_methods(key)
|
29
|
+
unless respond_to?(key)
|
30
|
+
define_singleton_method(key) { data[key] }
|
31
|
+
define_singleton_method("#{key}=") { |value| data[key] = value }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -54,6 +54,12 @@ describe RecursiveStruct do
|
|
54
54
|
it { expect(subject.one).to be true }
|
55
55
|
end
|
56
56
|
|
57
|
+
describe 'hash setter' do
|
58
|
+
before { subject[:one] = true }
|
59
|
+
it { expect(subject[:one]).to be true }
|
60
|
+
it { expect(subject.one).to be true }
|
61
|
+
end
|
62
|
+
|
57
63
|
describe 'array value' do
|
58
64
|
before { subject.one = [1, 2] }
|
59
65
|
it { expect(subject.one).not_to be_empty }
|
@@ -72,5 +78,9 @@ describe RecursiveStruct do
|
|
72
78
|
describe 'invalid method' do
|
73
79
|
it { expect { subject.one(1) }.to raise_error NoMethodError }
|
74
80
|
end
|
81
|
+
|
82
|
+
describe 'responds to hash methods' do
|
83
|
+
it { expect { subject.keys }.not_to raise_error }
|
84
|
+
end
|
75
85
|
end
|
76
86
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: recursive_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Phillip Boksz
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
83
|
- lib/recursive_struct.rb
|
84
|
+
- lib/recursive_struct/data.rb
|
84
85
|
- lib/recursive_struct/version.rb
|
85
86
|
- recursive_struct.gemspec
|
86
87
|
- spec/recursive_struct_spec.rb
|