Class: Kuroko2::Workflow::Node
- Inherits:
-
Object
- Object
- Kuroko2::Workflow::Node
- Defined in:
- lib/autoload/kuroko2/workflow/node.rb
Constant Summary collapse
- PATH_REGEXP =
%r(\A(?:/|(?:/\d+-[a-z0-9_]+)+)\z)
- TASK_REGISTRY =
{ root: Task::Sequence, noop: Task::Noop, sequence: Task::Sequence, auto_skip_error: Task::AutoSkipError, fork: Task::Fork, parallel_fork: Task::ParallelFork, env: Task::Env, execute: Task::Execute, queue: Task::Queue, sub_process: Task::SubProcess, subprocess: Task::SubProcess, timeout: Task::Timeout, expected_time: Task::ExpectedTime, wait: Task::Wait, sleep: Task::Sleep, rails_env: Task::RailsEnv, kuroko_runner: Task::KurokoRunner, echo: Task::Echo, retry: Task::Retry, }
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#option ⇒ Object
readonly
Returns the value of attribute option.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #append_child(child) ⇒ Object
- #execute(token) ⇒ Object
- #find(path) ⇒ Object
-
#initialize(type, option = nil) ⇒ Node
constructor
A new instance of Node.
- #next(index = 0) ⇒ Object
- #next_sibling ⇒ Object
- #path ⇒ Object
- #to_script(indent = 0) ⇒ Object
- #validate_all ⇒ Object
Constructor Details
#initialize(type, option = nil) ⇒ Node
Returns a new instance of Node.
44 45 46 47 48 49 50 51 52 |
# 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 |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
27 28 29 |
# File 'lib/autoload/kuroko2/workflow/node.rb', line 27 def children @children end |
#option ⇒ Object (readonly)
Returns the value of attribute option.
27 28 29 |
# File 'lib/autoload/kuroko2/workflow/node.rb', line 27 def option @option end |
#parent ⇒ Object
Returns the value of attribute parent.
28 29 30 |
# File 'lib/autoload/kuroko2/workflow/node.rb', line 28 def parent @parent end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
27 28 29 |
# File 'lib/autoload/kuroko2/workflow/node.rb', line 27 def type @type end |
Class Method Details
.deregister(key) ⇒ Object
40 41 42 |
# File 'lib/autoload/kuroko2/workflow/node.rb', line 40 def self.deregister(key) TASK_REGISTRY.delete(key) end |
.register(key: nil, klass:) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# 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 |
Instance Method Details
#append_child(child) ⇒ Object
54 55 56 57 |
# File 'lib/autoload/kuroko2/workflow/node.rb', line 54 def append_child(child) child.parent = self @children << child end |
#execute(token) ⇒ Object
59 60 61 62 63 64 |
# 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) ⇒ Object
66 67 68 69 70 71 72 73 |
# 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) ⇒ Object
75 76 77 78 79 80 81 |
# 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 ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/autoload/kuroko2/workflow/node.rb', line 83 def next_sibling if parent parent.next(current_index + 1) else nil end end |
#path ⇒ Object
91 92 93 94 95 96 97 |
# 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) ⇒ Object
99 100 101 |
# 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 ⇒ Object
103 104 105 106 107 108 |
# 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 |