Class: GraphAgent::Channels::LastValue

Inherits:
BaseChannel show all
Defined in:
lib/graph_agent/channels/last_value.rb

Constant Summary

Constants inherited from BaseChannel

BaseChannel::MISSING

Instance Attribute Summary

Attributes inherited from BaseChannel

#key

Instance Method Summary collapse

Methods inherited from BaseChannel

#consume, #finish

Constructor Details

#initialize(key: "", default: MISSING) ⇒ LastValue

Returns a new instance of LastValue.



6
7
8
9
# File 'lib/graph_agent/channels/last_value.rb', line 6

def initialize(key: "", default: MISSING)
  super(key: key)
  @value = default
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/graph_agent/channels/last_value.rb', line 28

def available?
  !@value.equal?(MISSING)
end

#checkpointObject



32
33
34
# File 'lib/graph_agent/channels/last_value.rb', line 32

def checkpoint
  @value
end

#copyObject



42
43
44
45
46
# File 'lib/graph_agent/channels/last_value.rb', line 42

def copy
  ch = self.class.new(key: key)
  ch.instance_variable_set(:@value, @value)
  ch
end

#from_checkpoint(checkpoint) ⇒ Object



36
37
38
39
40
# File 'lib/graph_agent/channels/last_value.rb', line 36

def from_checkpoint(checkpoint)
  ch = self.class.new(key: key)
  ch.instance_variable_set(:@value, checkpoint.equal?(MISSING) ? MISSING : checkpoint)
  ch
end

#getObject

Raises:



11
12
13
14
15
# File 'lib/graph_agent/channels/last_value.rb', line 11

def get
  raise EmptyChannelError.new("Channel '#{key}' is empty") if @value.equal?(MISSING)

  @value
end

#update(values) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/graph_agent/channels/last_value.rb', line 17

def update(values)
  return false if values.empty?

  if values.length != 1
    raise InvalidUpdateError.new("At key '#{key}': Can receive only one value per step.")
  end

  @value = values.last
  true
end