Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug around Array#each

  • Loading branch information...
commit 8799007a613263056c6cc699290f96f90c51ce34 1 parent d5e8121
Tammer Saleh authored
View
9 lib/deep_struct.rb
@@ -10,6 +10,15 @@ def self.from_data(data)
end
end
+ def self.convert_element_if_possible(e)
+ case e
+ when ::Array then DeepStruct::Array.new(e)
+ when ::Hash then DeepStruct::Hash.new(e)
+ else
+ e
+ end
+ end
+
def self.from_file(path)
from_data(FileReader.new(path).data)
end
View
11 lib/deep_struct/array.rb
@@ -1,9 +1,6 @@
class DeepStruct::Array < Array
def [](index)
- v = super
- return DeepStruct::Hash.new(v) if v.is_a?(::Hash)
- return DeepStruct::Array.new(v) if v.is_a?(::Array)
- return v
+ DeepStruct.convert_element_if_possible(super)
end
# The annoyance of subclassing Array is that ruby implements many array
@@ -13,4 +10,10 @@ def [](index)
def first
self[0]
end
+
+ def each(&block)
+ self.to_a.each do |e|
+ DeepStruct.convert_element_if_possible(e)
+ end
+ end
end
View
5 lib/deep_struct/hash.rb
@@ -7,10 +7,7 @@ def initialize(hash)
end
def [](key)
- v = super(key)
- return DeepStruct::Hash.new(v) if v.is_a?(::Hash)
- return DeepStruct::Array.new(v) if v.is_a?(::Array)
- return v
+ DeepStruct.convert_element_if_possible(super)
end
def method_missing(key, *args, &block)
View
2  lib/deep_struct/version.rb
@@ -1,3 +1,3 @@
module DeepStruct
- VERSION = "1.0.1"
+ VERSION = "1.0.2"
end
View
8 spec/deep_struct_spec.rb
@@ -11,6 +11,14 @@
its("first.one") { should == 1 }
its("second.two") { should == 2 }
its("first.unknown") { should == nil }
+
+ describe "#each" do
+ it "returns each element as a DeepStruct::Hash" do
+ deep_struct.each do |ds_hash|
+ ds_hash.should be_a(DeepStruct::Hash)
+ end
+ end
+ end
end
context "instance with a hash of arrays of hashes" do
Please sign in to comment.
Something went wrong with that request. Please try again.