Class: Any2Tmx::Traversable
- Inherits:
-
Object
- Object
- Any2Tmx::Traversable
- Defined in:
- lib/any2tmx/traversable.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Instance Method Summary collapse
- #dig(path) ⇒ Object
- #each_entry(&block) ⇒ Object
-
#initialize(collection) ⇒ Traversable
constructor
A new instance of Traversable.
- #size ⇒ Object
- #zip(other_traversable) ⇒ Object
Constructor Details
#initialize(collection) ⇒ Traversable
Returns a new instance of Traversable.
5 6 7 |
# File 'lib/any2tmx/traversable.rb', line 5 def initialize(collection) @collection = collection end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
3 4 5 |
# File 'lib/any2tmx/traversable.rb', line 3 def collection @collection end |
Instance Method Details
#dig(path) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/any2tmx/traversable.rb', line 21 def dig(path) path.inject(:start) do |ret, seg| if ret == :start collection[seg] elsif ret if seg.is_a?(Numeric) && ret.is_a?(Array) ret[seg] # array index case elsif seg.is_a?(String) && ret.is_a?(Hash) ret[seg] # hash key case end end end end |
#each_entry(&block) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/any2tmx/traversable.rb', line 9 def each_entry(&block) if block_given? each_entry_helper(collection, [], &block) else to_enum(__method__) end end |
#size ⇒ Object
17 18 19 |
# File 'lib/any2tmx/traversable.rb', line 17 def size each_entry.inject(0) { |ret, _| ret + 1 } end |
#zip(other_traversable) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/any2tmx/traversable.rb', line 35 def zip(other_traversable) each_entry.each_with_object({}) do |(entry, path), ret| other_entry = other_traversable.dig(path) ret[entry] = other_entry if other_entry yield if block_given? && other_entry end end |