Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed bug around Array#each

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

0 comments on commit 8799007

Please sign in to comment.
Something went wrong with that request. Please try again.