Class: CMDx::Task
Overview
Represents a task that can be executed within the CMDx framework. Tasks define attributes, callbacks, and execution logic that can be chained together to form workflows.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash{Symbol => Object}
readonly
Returns the hash of processed attribute values for this task.
-
#chain ⇒ Chain
readonly
Returns the execution chain containing all task results.
-
#context ⇒ Context
(also: #ctx)
readonly
Returns the execution context for this task.
-
#errors ⇒ Errors
readonly
Returns the collection of validation and execution errors.
-
#id ⇒ String
readonly
Returns the unique identifier for this task instance.
-
#result ⇒ Result
(also: #res)
readonly
Returns the execution result for this task.
Class Method Summary collapse
- .attributes ⇒ Object (also: attribute)
-
.attributes_schema ⇒ Hash
Hash of attribute names to their configurations.
- .deregister(type, object) ⇒ Object
-
.execute(*args, **kwargs) ⇒ Result
The execution result.
-
.execute!(*args, **kwargs) ⇒ Result
The execution result.
- .optional ⇒ Object
- .register(type, object) ⇒ Object
- .remove_attributes(*names) ⇒ Object (also: remove_attribute)
-
.remove_returns(*names) ⇒ Object
(also: remove_return)
Removes declared returns from the task.
- .required ⇒ Object
-
.returns(*names) ⇒ Object
Declares expected context returns that must be set after task execution.
-
.settings(**options) ⇒ Hash
The merged settings hash.
Instance Method Summary collapse
-
#execute(raise: false) ⇒ Result
The execution result.
-
#initialize(context = {}) ⇒ Task
constructor
A new task instance.
-
#logger ⇒ Logger
The logger instance for this task.
-
#to_h ⇒ Hash
A hash representation of the task.
-
#to_s ⇒ String
A string representation of the task.
- #work ⇒ Object
Constructor Details
#initialize(context = {}) ⇒ Task
Returns A new task instance.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/cmdx/task.rb', line 89 def initialize(context = {}) Deprecator.restrict(self) @attributes = {} @errors = Errors.new @id = Identifier.generate @context = Context.build(context) @result = Result.new(self) @chain = Chain.build(@result, dry_run: @context.delete(:dry_run)) end |
Instance Attribute Details
#attributes ⇒ Hash{Symbol => Object} (readonly)
Returns the hash of processed attribute values for this task.
19 20 21 |
# File 'lib/cmdx/task.rb', line 19 def attributes @attributes end |
#chain ⇒ Chain (readonly)
Returns the execution chain containing all task results.
71 72 73 |
# File 'lib/cmdx/task.rb', line 71 def chain @chain end |
#context ⇒ Context (readonly) Also known as: ctx
Returns the execution context for this task.
49 50 51 |
# File 'lib/cmdx/task.rb', line 49 def context @context end |
#errors ⇒ Errors (readonly)
Returns the collection of validation and execution errors.
29 30 31 |
# File 'lib/cmdx/task.rb', line 29 def errors @errors end |
#id ⇒ String (readonly)
Returns the unique identifier for this task instance.
39 40 41 |
# File 'lib/cmdx/task.rb', line 39 def id @id end |
#result ⇒ Result (readonly) Also known as: res
Returns the execution result for this task.
60 61 62 |
# File 'lib/cmdx/task.rb', line 60 def result @result end |
Class Method Details
.attributes ⇒ Object Also known as: attribute
187 188 189 |
# File 'lib/cmdx/task.rb', line 187 def attributes(...) register(:attribute, Attribute.build(...)) end |
.attributes_schema ⇒ Hash
Returns Hash of attribute names to their configurations.
261 262 263 264 265 |
# File 'lib/cmdx/task.rb', line 261 def attributes_schema Array(settings[:attributes]).each_with_object({}) do |attr, schema| schema[attr.method_name] = attr.to_h end end |
.deregister(type, object) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/cmdx/task.rb', line 171 def deregister(type, object, ...) case type when :attribute then settings[:attributes].deregister(object, ...) when :callback then settings[:callbacks].deregister(object, ...) when :coercion then settings[:coercions].deregister(object, ...) when :middleware then settings[:middlewares].deregister(object, ...) when :validator then settings[:validators].deregister(object, ...) else raise "unknown registry type #{type.inspect}" end end |
.execute(*args, **kwargs) ⇒ Result
Returns The execution result.
298 299 300 301 302 |
# File 'lib/cmdx/task.rb', line 298 def execute(*args, **kwargs) task = new(*args, **kwargs) task.execute(raise: false) block_given? ? yield(task.result) : task.result end |
.execute!(*args, **kwargs) ⇒ Result
Returns The execution result.
317 318 319 320 321 |
# File 'lib/cmdx/task.rb', line 317 def execute!(*args, **kwargs) task = new(*args, **kwargs) task.execute(raise: true) block_given? ? yield(task.result) : task.result end |
.optional ⇒ Object
197 198 199 |
# File 'lib/cmdx/task.rb', line 197 def optional(...) register(:attribute, Attribute.optional(...)) end |
.register(type, object) ⇒ Object
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/cmdx/task.rb', line 150 def register(type, object, ...) case type when :attribute then settings[:attributes].register(object, ...) when :callback then settings[:callbacks].register(object, ...) when :coercion then settings[:coercions].register(object, ...) when :middleware then settings[:middlewares].register(object, ...) when :validator then settings[:validators].register(object, ...) else raise "unknown registry type #{type.inspect}" end end |
.remove_attributes(*names) ⇒ Object Also known as: remove_attribute
216 217 218 |
# File 'lib/cmdx/task.rb', line 216 def remove_attributes(*names) deregister(:attribute, names) end |
.remove_returns(*names) ⇒ Object Also known as: remove_return
Removes declared returns from the task.
243 244 245 |
# File 'lib/cmdx/task.rb', line 243 def remove_returns(*names) settings[:returns] -= names.map(&:to_sym) end |
.required ⇒ Object
206 207 208 |
# File 'lib/cmdx/task.rb', line 206 def required(...) register(:attribute, Attribute.required(...)) end |
.returns(*names) ⇒ Object
Declares expected context returns that must be set after task execution. If any declared return is missing from the context after #work completes successfully, the task will fail with a validation error.
231 232 233 |
# File 'lib/cmdx/task.rb', line 231 def returns(*names) settings[:returns] |= names.map(&:to_sym) end |
.settings(**options) ⇒ Hash
Returns The merged settings hash.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cmdx/task.rb', line 114 def settings(**) @settings ||= begin hash = if superclass.respond_to?(:settings) parent = superclass.settings parent .except(:backtrace_cleaner, :exception_handler, :logger, :deprecate) .transform_values!(&:dup) .merge!( backtrace_cleaner: parent[:backtrace_cleaner] || CMDx.configuration.backtrace_cleaner, exception_handler: parent[:exception_handler] || CMDx.configuration.exception_handler, logger: parent[:logger] || CMDx.configuration.logger, deprecate: parent[:deprecate] ) else CMDx.configuration.to_h end hash[:attributes] ||= AttributeRegistry.new hash[:returns] ||= [] hash[:tags] ||= [] hash.merge!() end end |
Instance Method Details
#execute(raise: false) ⇒ Result
Returns The execution result.
334 335 336 337 |
# File 'lib/cmdx/task.rb', line 334 def execute(raise: false) Executor.execute(self, raise:) block_given? ? yield(result) : result end |
#logger ⇒ Logger
Returns The logger instance for this task.
361 362 363 364 365 366 367 368 |
# File 'lib/cmdx/task.rb', line 361 def logger @logger ||= begin logger = self.class.settings[:logger] || CMDx.configuration.logger logger.level = self.class.settings[:log_level] || logger.level logger.formatter = self.class.settings[:log_formatter] || logger.formatter logger end end |
#to_h ⇒ Hash
Returns A hash representation of the task.
385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/cmdx/task.rb', line 385 def to_h { index: result.index, chain_id: chain.id, type: self.class.include?(Workflow) ? "Workflow" : "Task", tags: self.class.settings[:tags], class: self.class.name, dry_run: dry_run?, id: } end |
#to_s ⇒ String
Returns A string representation of the task.
404 405 406 |
# File 'lib/cmdx/task.rb', line 404 def to_s Utils::Format.to_str(to_h) end |
#work ⇒ Object
350 351 352 |
# File 'lib/cmdx/task.rb', line 350 def work raise UndefinedMethodError, "undefined method #{self.class.name}#work" end |