recursive-open-struct 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  require 'ostruct'
2
2
 
3
3
  class RecursiveOpenStruct < OpenStruct
4
- VERSION = "0.4.2"
4
+ VERSION = "0.4.3"
5
5
 
6
6
  def initialize(h=nil, args={})
7
7
  @recurse_over_arrays = args.fetch(:recurse_over_arrays,false)
@@ -28,9 +28,9 @@ class RecursiveOpenStruct < OpenStruct
28
28
  define_method(name) do
29
29
  v = @table[name]
30
30
  if v.is_a?(Hash)
31
- @sub_elements[name] ||= RecursiveOpenStruct.new(v)
31
+ @sub_elements[name] ||= RecursiveOpenStruct.new(v, :recurse_over_arrays => @recurse_over_arrays)
32
32
  elsif v.is_a?(Array) and @recurse_over_arrays
33
- @sub_elements[name] ||= v.map { |a| (a.is_a? Hash) ? RecursiveOpenStruct.new(a, :recurse_over_arrays => true) : a }
33
+ @sub_elements[name] ||= recurse_over_array v
34
34
  else
35
35
  v
36
36
  end
@@ -42,6 +42,18 @@ class RecursiveOpenStruct < OpenStruct
42
42
  name
43
43
  end
44
44
 
45
+ def recurse_over_array array
46
+ array.map do |a|
47
+ if a.is_a? Hash
48
+ RecursiveOpenStruct.new(a, :recurse_over_arrays => true)
49
+ elsif a.is_a? Array
50
+ recurse_over_array a
51
+ else
52
+ a
53
+ end
54
+ end
55
+ end
56
+
45
57
  def debug_inspect(io = STDOUT, indent_level = 0, recursion_limit = 12)
46
58
  display_recursive_open_struct(io, @table, indent_level, recursion_limit)
47
59
  end
@@ -131,6 +131,31 @@ describe RecursiveOpenStruct do
131
131
  }
132
132
  end
133
133
 
134
+ context "when array is nested deeper" do
135
+ let(:deep_hash) { { :foo => { :blah => blah_list } } }
136
+ subject { RecursiveOpenStruct.new(deep_hash, :recurse_over_arrays => true) }
137
+
138
+ it { subject.foo.blah.length.should == 3 }
139
+ it "Retains changes across Array lookups" do
140
+ subject.foo.blah[1].foo = "Dr Scott"
141
+ subject.foo.blah[1].foo.should == "Dr Scott"
142
+ end
143
+
144
+ end
145
+
146
+ context "when array is in an array" do
147
+ let(:haah) { { :blah => [ blah_list ] } }
148
+ subject { RecursiveOpenStruct.new(haah, :recurse_over_arrays => true) }
149
+
150
+ it { subject.blah.length.should == 1 }
151
+ it { subject.blah[0].length.should == 3 }
152
+ it "Retains changes across Array lookups" do
153
+ subject.blah[0][1].foo = "Dr Scott"
154
+ subject.blah[0][1].foo.should == "Dr Scott"
155
+ end
156
+
157
+ end
158
+
134
159
  end # when recursing over arrays is enabled
135
160
 
136
161
  context "when recursing over arrays is disabled" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recursive-open-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-30 00:00:00.000000000 Z
12
+ date: 2013-05-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec