Class: Depth::Enumeration::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/depth/enumeration/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fragmentObject

Returns the value of attribute fragment

Returns:

  • (Object)

    the current value of fragment



3
4
5
# File 'lib/depth/enumeration/node.rb', line 3

def fragment
  @fragment
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



3
4
5
# File 'lib/depth/enumeration/node.rb', line 3

def parent
  @parent
end

#parent_indexObject

Returns the value of attribute parent_index

Returns:

  • (Object)

    the current value of parent_index



3
4
5
# File 'lib/depth/enumeration/node.rb', line 3

def parent_index
  @parent_index
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/depth/enumeration/node.rb', line 42

def array?
  fragment.is_a?(Array)
end

#current_indexObject



4
5
6
# File 'lib/depth/enumeration/node.rb', line 4

def current_index
  @current_index ||= 0
end

#enumerable?Boolean

Returns:

  • (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_typeObject



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

Returns:

  • (Boolean)


46
47
48
# File 'lib/depth/enumeration/node.rb', line 46

def hash?
  fragment.is_a?(Hash)
end

#humanized_routeObject



18
19
20
# File 'lib/depth/enumeration/node.rb', line 18

def humanized_route
  route.map(&:key_or_index)
end

#leaf?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/depth/enumeration/node.rb', line 62

def leaf?
  !enumerable?
end

#nextObject



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

Returns:

  • (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_keyObject



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

Returns:

  • (Boolean)


66
67
68
# File 'lib/depth/enumeration/node.rb', line 66

def root?
  parent.nil?
end

#routeObject



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