Class: Depth::Enumeration::Node
- Inherits:
-
Struct
- Object
- Struct
- Depth::Enumeration::Node
- Defined in:
- lib/depth/enumeration/node.rb
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#parent_index ⇒ Object
Returns the value of attribute parent_index.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #current_index ⇒ Object
- #enumerable? ⇒ Boolean
- #fragment_type ⇒ Object
- #hash? ⇒ Boolean
- #humanized_route ⇒ Object
- #leaf? ⇒ Boolean
- #next ⇒ Object
- #next? ⇒ Boolean
- #parent_key ⇒ Object
- #root? ⇒ Boolean
- #route ⇒ Object
Instance Attribute Details
#fragment ⇒ Object
Returns the value of attribute fragment
3 4 5 |
# File 'lib/depth/enumeration/node.rb', line 3 def fragment @fragment end |
#parent ⇒ Object
Returns the value of attribute parent
3 4 5 |
# File 'lib/depth/enumeration/node.rb', line 3 def parent @parent end |
#parent_index ⇒ Object
Returns the value of attribute parent_index
3 4 5 |
# File 'lib/depth/enumeration/node.rb', line 3 def parent_index @parent_index end |
Instance Method Details
#array? ⇒ Boolean
42 43 44 |
# File 'lib/depth/enumeration/node.rb', line 42 def array? fragment.is_a?(Array) end |
#current_index ⇒ Object
4 5 6 |
# File 'lib/depth/enumeration/node.rb', line 4 def current_index @current_index ||= 0 end |
#enumerable? ⇒ Boolean
57 58 59 60 |
# File 'lib/depth/enumeration/node.rb', line 57 def enumerable? # ignore other types for the moment array? || hash? end |
#fragment_type ⇒ Object
50 51 52 53 54 55 |
# File 'lib/depth/enumeration/node.rb', line 50 def fragment_type { Array => :array, Hash => :hash }.fetch(fragment.class, :leaf) end |
#hash? ⇒ Boolean
46 47 48 |
# File 'lib/depth/enumeration/node.rb', line 46 def hash? fragment.is_a?(Hash) end |
#humanized_route ⇒ Object
18 19 20 |
# File 'lib/depth/enumeration/node.rb', line 18 def humanized_route route.map(&:key_or_index) end |
#leaf? ⇒ Boolean
62 63 64 |
# File 'lib/depth/enumeration/node.rb', line 62 def leaf? !enumerable? end |
#next ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/depth/enumeration/node.rb', line 22 def next if array? val = fragment[current_index] else val = fragment[fragment.keys[current_index]] end Node.new(self, current_index, val).tap { @current_index += 1 } end |
#next? ⇒ Boolean
37 38 39 40 |
# File 'lib/depth/enumeration/node.rb', line 37 def next? return false if leaf? current_index < fragment.count end |
#parent_key ⇒ Object
31 32 33 34 35 |
# File 'lib/depth/enumeration/node.rb', line 31 def parent_key return nil unless parent.enumerable? # root return parent_index if parent.array? parent.fragment.keys[parent_index] end |
#root? ⇒ Boolean
66 67 68 |
# File 'lib/depth/enumeration/node.rb', line 66 def root? parent.nil? end |
#route ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/depth/enumeration/node.rb', line 8 def route route = [] current = self while(!current.root?) route << RouteElement.new(current.parent_key, type: current.fragment_type) current = current.parent end route.reverse end |