recursive-open-struct 0.4.4 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/recursive_open_struct.rb +6 -6
- data/spec/recursive_open_struct_spec.rb +8 -0
- metadata +2 -2
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
|
3
3
|
class RecursiveOpenStruct < OpenStruct
|
4
|
-
VERSION = "0.4.
|
4
|
+
VERSION = "0.4.5"
|
5
5
|
|
6
6
|
def initialize(h=nil, args={})
|
7
7
|
@recurse_over_arrays = args.fetch(:recurse_over_arrays,false)
|
@@ -28,7 +28,7 @@ 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] ||=
|
31
|
+
@sub_elements[name] ||= self.class.new(v, :recurse_over_arrays => @recurse_over_arrays)
|
32
32
|
elsif v.is_a?(Array) and @recurse_over_arrays
|
33
33
|
@sub_elements[name] ||= recurse_over_array v
|
34
34
|
else
|
@@ -45,7 +45,7 @@ class RecursiveOpenStruct < OpenStruct
|
|
45
45
|
def recurse_over_array array
|
46
46
|
array.map do |a|
|
47
47
|
if a.is_a? Hash
|
48
|
-
|
48
|
+
self.class.new(a, :recurse_over_arrays => true)
|
49
49
|
elsif a.is_a? Array
|
50
50
|
recurse_over_array a
|
51
51
|
else
|
@@ -65,7 +65,7 @@ class RecursiveOpenStruct < OpenStruct
|
|
65
65
|
io.puts ' '*indent_level + '(recursion limit reached)'
|
66
66
|
else
|
67
67
|
#puts ostrct_or_hash.inspect
|
68
|
-
if ostrct_or_hash.is_a?(
|
68
|
+
if ostrct_or_hash.is_a?(self.class) then
|
69
69
|
ostrct_or_hash = ostrct_or_hash.marshal_dump
|
70
70
|
end
|
71
71
|
|
@@ -73,12 +73,12 @@ class RecursiveOpenStruct < OpenStruct
|
|
73
73
|
# to align display, we look for the maximum key length of the data that will be displayed
|
74
74
|
# (everything except hashes)
|
75
75
|
data_indent = ostrct_or_hash \
|
76
|
-
.reject { |k, v| v.is_a?(
|
76
|
+
.reject { |k, v| v.is_a?(self.class) || v.is_a?(Hash) } \
|
77
77
|
.max {|a,b| a[0].to_s.length <=> b[0].to_s.length}[0].to_s.length
|
78
78
|
# puts "max length = #{data_indent}"
|
79
79
|
|
80
80
|
ostrct_or_hash.each do |key, value|
|
81
|
-
if (value.is_a?(
|
81
|
+
if (value.is_a?(self.class) || value.is_a?(Hash)) then
|
82
82
|
io.puts ' '*indent_level + key.to_s + '.'
|
83
83
|
display_recursive_open_struct(io, value, indent_level + 1, recursion_limit - 1)
|
84
84
|
else
|
@@ -234,6 +234,14 @@ QUOTE
|
|
234
234
|
@io.string.should match /^ h2\.$/
|
235
235
|
@io.string.should match /^ \(recursion limit reached\)$/
|
236
236
|
end
|
237
|
+
|
238
|
+
it "creates nested objects via subclass" do
|
239
|
+
RecursiveOpenStructSubClass = Class.new(RecursiveOpenStruct)
|
240
|
+
|
241
|
+
rossc = RecursiveOpenStructSubClass.new({ :one => [{:two => :three}] }, recurse_over_arrays: true)
|
242
|
+
|
243
|
+
rossc.one.first.class.should == RecursiveOpenStructSubClass
|
244
|
+
end
|
237
245
|
end # additionnel features
|
238
246
|
|
239
247
|
end # describe RecursiveOpenStruct
|
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.
|
4
|
+
version: 0.4.5
|
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-
|
12
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|