class Kuroko2::Workflow::Node

Constants

PATH_REGEXP
TASK_REGISTRY

Attributes

children[R]
option[R]
parent[RW]
type[R]

Public Class Methods

deregister(key) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 40
def self.deregister(key)
  TASK_REGISTRY.delete(key)
end
new(type, option = nil) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 44
def initialize(type, option = nil)
  @type       = type.to_sym
  @task_klass = TASK_REGISTRY.fetch(@type, nil)
  @option     = option.try(:strip)
  @parent     = nil
  @children   = []

  raise AssertionError, "`#{@type}` is not registered in task repository." unless @task_klass
end
register(key: nil, klass:) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 30
def self.register(key: nil, klass:)
  key ||= klass.to_s.demodulize.underscore.to_sym

  unless TASK_REGISTRY.has_key?(key)
    TASK_REGISTRY.store(key, klass)
  else
    Kuroko2.logger.warn("Unable to add '#{klass}' to task registry. '#{TASK_REGISTRY[key]}' is already registered.")
  end
end

Public Instance Methods

append_child(child) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 54
def append_child(child)
  child.parent = self
  @children << child
end
execute(token) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 59
def execute(token)
  Kuroko2.logger.debug { "(token #{token.uuid}) Execute #{@type} with option '#{@option}'." }
  @task_klass.new(self, token).execute.tap do |result|
    Kuroko2.logger.debug("(token #{token.uuid}) Result is '#{result}'.")
  end
end
find(path) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 66
def find(path)
  raise AssertionError, "path query('#{path}') is invalid." unless PATH_REGEXP === path

  query = path.split('/')
  query.shift # drop first empty string.

  traverse(query)
end
next(index = 0) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 75
def next(index = 0)
  if (child = children[index])
    child
  else
    next_sibling
  end
end
next_sibling() click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 83
def next_sibling
  if parent
    parent.next(current_index + 1)
  else
    nil
  end
end
path() click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 91
def path
  if parent
    parent.path + "/#{current_index}-#{type}"
  else
    ''
  end
end
to_script(indent = 0) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 99
def to_script(indent = 0)
  "#{'  ' * indent}#{type}: #{option}\n" + children.map { |child| child.to_script(indent + 1) }.join
end
validate_all() click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 103
def validate_all
  @task_klass.new(self, nil).validate
  @children.each do |child|
    child.validate_all
  end
end

Protected Instance Methods

current_index() click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 112
def current_index
  @_current_index = parent.children.index(self)
end
traverse(query) click to toggle source
# File lib/autoload/kuroko2/workflow/node.rb, line 116
def traverse(query)
  return self if query.empty?

  first    = query.shift
  index, _ = first.split('-')

  @children[index.to_i].traverse(query)
end