open_struct_factory 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/open_struct_factory.rb +5 -5
- data/lib/open_struct_factory/version.rb +1 -1
- data/spec/open_struct_factory_spec.rb +30 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95e3fc1c16fddde09bfe175eed5477a1f423ede9
|
4
|
+
data.tar.gz: b5feac492d4dfc65551c623a069bba71bdf75289
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb84e9c77bda70b7a60d436b92b49d5761f1b97814c694f040b3971a00665471907a56aa129a397dc26d75a6829ec9052b69bb166639af8b49eca0c6e6bdef42
|
7
|
+
data.tar.gz: 1cd8a5cba1dbbe3c94f5bdbc2061a31a8e6cd5c70b09be944b0d4fbb7ae613743d90b8849bb0f7692e5b377a002599dded805043562bed92b28151457e1e672f
|
data/lib/open_struct_factory.rb
CHANGED
@@ -8,7 +8,7 @@ module OpenStructFactory
|
|
8
8
|
# * An optional block can be passed to process the hash keys
|
9
9
|
def self.create(hash, &block) # :yields: key
|
10
10
|
properties = {}
|
11
|
-
hash.
|
11
|
+
hash.each_pair do |key, value|
|
12
12
|
property_name = block_given? ? (yield key) : key
|
13
13
|
properties[property_name] = process_value(value, &block)
|
14
14
|
end
|
@@ -18,13 +18,13 @@ module OpenStructFactory
|
|
18
18
|
private
|
19
19
|
|
20
20
|
def self.process_value(value, &block)
|
21
|
-
case
|
22
|
-
when
|
21
|
+
case
|
22
|
+
when value.respond_to?(:each_pair)
|
23
23
|
create(value, &block)
|
24
|
-
when Array
|
24
|
+
when value.is_a?(Array)
|
25
25
|
value.map { |v| process_value(v, &block) }
|
26
26
|
else
|
27
|
-
|
27
|
+
value
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -93,6 +93,35 @@ describe OpenStructFactory do
|
|
93
93
|
|
94
94
|
end
|
95
95
|
|
96
|
+
context "when given an non-Hash object responding to each_pair" do
|
97
|
+
|
98
|
+
class ActionControllerParameters
|
99
|
+
|
100
|
+
def initialize(bar, baz)
|
101
|
+
@bar = bar
|
102
|
+
@baz = baz
|
103
|
+
end
|
104
|
+
|
105
|
+
def each_pair(&block)
|
106
|
+
[[:bar, @bar], [:baz, @baz]].each(&block)
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
let(:hash) do
|
112
|
+
{
|
113
|
+
:foo => ActionControllerParameters.new(123, ActionControllerParameters.new("abc", "xyz")),
|
114
|
+
}
|
115
|
+
end
|
116
|
+
|
117
|
+
it "treats it like a hash" do
|
118
|
+
os = OpenStructFactory.create(hash)
|
119
|
+
expect(os.foo.bar).to eq(123)
|
120
|
+
expect(os.foo.baz.bar).to eq("abc")
|
121
|
+
expect(os.foo.baz.baz).to eq("xyz")
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
96
125
|
context "when given a block" do
|
97
126
|
|
98
127
|
let(:hash) do
|
@@ -115,4 +144,4 @@ describe OpenStructFactory do
|
|
115
144
|
|
116
145
|
end # .create
|
117
146
|
|
118
|
-
end
|
147
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_struct_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xavier Defrang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|