Class: Hydrangea::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/hydrangea/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state:, value:, previous: nil) ⇒ Node

Returns a new instance of Node.



5
6
7
8
# File 'lib/hydrangea/node.rb', line 5

def initialize(state:, value:, previous: nil)
  @state, @value, @previous = state, value, previous
  @first = previous && previous.first || self
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



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

def first
  @first
end

#previousObject (readonly)

Returns the value of attribute previous.



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

def previous
  @previous
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#history(&transform) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/hydrangea/node.rb', line 10

def history(&transform)
  node, history = self, []
  transform ||= :itself.to_proc
  loop do
    history << transform[node]
    break if node.previous.nil?
    node = node.previous
  end
  history
end

#inspectObject



29
30
31
# File 'lib/hydrangea/node.rb', line 29

def inspect
  "#<#{self.class.name}#{to_s}>"
end

#to_aObject



21
22
23
# File 'lib/hydrangea/node.rb', line 21

def to_a
  [state,value]
end

#to_sObject



25
26
27
# File 'lib/hydrangea/node.rb', line 25

def to_s
  "(#{to_a.map(&:inspect).join(',')})"
end