Class: Vangrail::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/actions.rb

Overview

Ruby callables a Colang flow can execute.

An action receives (arguments_hash, context) and returns whatever the flow will branch on: usually true or false, sometimes a rewritten string. Nothing about an action is special, which is the point. A team's own check is a lambda, registered by name, and a flow calls it exactly like a built-in.

actions = Vangrail::Actions.new
actions.register('check_ticket_id') { |_args, ctx| ctx[:user_input].match?(/EINF-\d+/) }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handlers = {}) ⇒ Actions

Returns a new instance of Actions.



16
17
18
19
# File 'lib/vangrail/actions.rb', line 16

def initialize(handlers = {})
  @handlers = {}
  handlers.each { |name, fn| register(name, &fn) }
end

Class Method Details

.from_rails(input: nil, output: nil, facts: nil) ⇒ Object

Wraps rails as the actions the toolkit's own flows call by name, so a config folder written for the Python runtime finds what it expects. self_check_input returns true when the text is allowed, which is the polarity those flows branch on.



53
54
55
56
57
58
59
# File 'lib/vangrail/actions.rb', line 53

def self.from_rails(input: nil, output: nil, facts: nil)
  actions = new
  actions.register('self_check_input') { |_args, ctx| input&.call(ctx[:text], ctx)&.allowed? }
  actions.register('self_check_output') { |_args, ctx| output&.call(ctx[:text], ctx)&.allowed? }
  actions.register('self_check_facts') { |_args, ctx| facts&.call(ctx[:text], ctx)&.allowed? }
  actions
end

Instance Method Details

#[](name) ⇒ Object



29
30
31
# File 'lib/vangrail/actions.rb', line 29

def [](name)
  @handlers[name.to_s]
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/vangrail/actions.rb', line 33

def key?(name)
  @handlers.key?(name.to_s)
end

#merge(other) ⇒ Object



41
42
43
# File 'lib/vangrail/actions.rb', line 41

def merge(other)
  self.class.new(to_h.merge(other.respond_to?(:to_h) ? other.to_h : other))
end

#namesObject



37
38
39
# File 'lib/vangrail/actions.rb', line 37

def names
  @handlers.keys.sort
end

#register(name, callable = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
# File 'lib/vangrail/actions.rb', line 21

def register(name, callable = nil, &block)
  fn = callable || block
  raise ArgumentError, "action #{name} needs a callable" unless fn.respond_to?(:call)

  @handlers[name.to_s] = fn
  self
end

#to_hObject



45
46
47
# File 'lib/vangrail/actions.rb', line 45

def to_h
  @handlers.dup
end