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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cd3524565084916d8e9c402b6cbf5bb8786908b
4
- data.tar.gz: 3c104557a0dddb00aa8086450c6a00446222c2c4
3
+ metadata.gz: 95e3fc1c16fddde09bfe175eed5477a1f423ede9
4
+ data.tar.gz: b5feac492d4dfc65551c623a069bba71bdf75289
5
5
  SHA512:
6
- metadata.gz: 0b5cb9566a446365fc4f60a33271dbcbeacb16078839e7fed947812484fb33b73f863ef4410e364804e28a9aabb6bfc43785fd50f5c77f4d2bb46ef996d4517b
7
- data.tar.gz: ba54ae59b50d67c7fe09aa7b556e0f5054b5af79a01bee242a8f9e42b40164351e383e2dd50c19f5a4422a09235cfa7f57146082290fa159b8711578d10db172
6
+ metadata.gz: cb84e9c77bda70b7a60d436b92b49d5761f1b97814c694f040b3971a00665471907a56aa129a397dc26d75a6829ec9052b69bb166639af8b49eca0c6e6bdef42
7
+ data.tar.gz: 1cd8a5cba1dbbe3c94f5bdbc2061a31a8e6cd5c70b09be944b0d4fbb7ae613743d90b8849bb0f7692e5b377a002599dded805043562bed92b28151457e1e672f
@@ -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.each do |key, value|
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 value
22
- when Hash
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
- value
27
+ value
28
28
  end
29
29
  end
30
30
 
@@ -1,3 +1,3 @@
1
1
  module OpenStructFactory
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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.0
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: 2014-08-10 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler