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(**overrides) ⇒ Settings
Returns (and lazily creates) the task-level Settings object.
-
.type ⇒ String
Returns the cached task type string for this class.
Instance Method Summary collapse
-
#execute(raise: false) ⇒ Result
The execution result.
-
#initialize(context = nil) ⇒ Task
constructor
A new task instance.
-
#logger ⇒ Logger
Returns a logger 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 = nil) ⇒ Task
Returns A new task instance.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cmdx/task.rb', line 88 def initialize(context = nil) Deprecator.restrict(self) @id = Identifier.generate @context = Context.build(context) @errors = Errors.new @result = Result.new(self) @chain = Chain.build(@result, dry_run: @context.delete(:dry_run)) @attributes = {} 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
184 185 186 |
# File 'lib/cmdx/task.rb', line 184 def attributes(...) register(:attribute, Attribute.build(...)) end |
.attributes_schema ⇒ Hash
Returns Hash of attribute names to their configurations.
258 259 260 261 262 |
# File 'lib/cmdx/task.rb', line 258 def attributes_schema Utils::Wrap.array(settings.attributes).to_h do |attr| [attr.method_name, attr.to_h] end end |
.deregister(type, object) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/cmdx/task.rb', line 166 def deregister(type, object, ...) case type when :attribute settings.attributes.undefine_readers_on!(self, object) settings.attributes.deregister(object) when :callback then settings.callbacks.deregister(object, ...) when :middleware then settings.middlewares.deregister(object, ...) when :validator then settings.validators.deregister(object, ...) when :coercion then settings.coercions.deregister(object, ...) else raise "unknown registry type #{type.inspect}" end end |
.execute(*args, **kwargs) ⇒ Result
Returns The execution result.
292 293 294 295 296 |
# File 'lib/cmdx/task.rb', line 292 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.
310 311 312 313 314 |
# File 'lib/cmdx/task.rb', line 310 def execute!(*args, **kwargs) task = new(*args, **kwargs) task.execute(raise: true) block_given? ? yield(task.result) : task.result end |
.optional ⇒ Object
194 195 196 |
# File 'lib/cmdx/task.rb', line 194 def optional(...) register(:attribute, Attribute.optional(...)) end |
.register(type, object) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/cmdx/task.rb', line 143 def register(type, object, ...) case type when :attribute settings.attributes.register(object) settings.attributes.define_readers_on!(self, Utils::Wrap.array(object)) when :callback then settings.callbacks.register(object, ...) when :middleware then settings.middlewares.register(object, ...) when :validator then settings.validators.register(object, ...) when :coercion then settings.coercions.register(object, ...) else raise "unknown registry type #{type.inspect}" end end |
.remove_attributes(*names) ⇒ Object Also known as: remove_attribute
213 214 215 |
# File 'lib/cmdx/task.rb', line 213 def remove_attributes(*names) deregister(:attribute, names) end |
.remove_returns(*names) ⇒ Object Also known as: remove_return
Removes declared returns from the task.
240 241 242 |
# File 'lib/cmdx/task.rb', line 240 def remove_returns(*names) settings.returns -= names.map(&:to_sym) end |
.required ⇒ Object
203 204 205 |
# File 'lib/cmdx/task.rb', line 203 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.
228 229 230 |
# File 'lib/cmdx/task.rb', line 228 def returns(*names) settings.returns |= names.map(&:to_sym) end |
.settings(**overrides) ⇒ Settings
Returns (and lazily creates) the task-level Settings object. On first access, inherits from the superclass settings or the global Configuration. Optional overrides are applied once.
126 127 128 129 130 131 |
# File 'lib/cmdx/task.rb', line 126 def settings(**overrides) @settings ||= begin parent = superclass.settings if superclass.respond_to?(:settings) Settings.new(parent:, **overrides) end end |
.type ⇒ String
Returns the cached task type string for this class.
107 108 109 |
# File 'lib/cmdx/task.rb', line 107 def type @type ||= include?(Workflow) ? "Workflow" : "Task" end |
Instance Method Details
#execute(raise: false) ⇒ Result
Returns The execution result.
327 328 329 330 |
# File 'lib/cmdx/task.rb', line 327 def execute(raise: false) Executor.execute(self, raise:) block_given? ? yield(result) : result end |
#logger ⇒ Logger
Returns a logger for this task. When a custom log_level or log_formatter is configured, the shared logger is duplicated so the original instance is never mutated.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/cmdx/task.rb', line 358 def logger @logger ||= begin settings = self.class.settings log_instance = settings.logger || CMDx.configuration.logger if settings.log_level || settings.log_formatter log_instance = log_instance.dup log_instance.level = settings.log_level if settings.log_level log_instance.formatter = settings.log_formatter if settings.log_formatter end log_instance end end |
#to_h ⇒ Hash
Returns A hash representation of the task.
388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/cmdx/task.rb', line 388 def to_h { index: result.index, chain_id: chain.id, type: self.class.type, class: self.class.name, id:, dry_run: dry_run?, tags: self.class.settings. } end |
#to_s ⇒ String
Returns A string representation of the task.
407 408 409 |
# File 'lib/cmdx/task.rb', line 407 def to_s Utils::Format.to_str(to_h) end |
#work ⇒ Object
343 344 345 |
# File 'lib/cmdx/task.rb', line 343 def work raise UndefinedMethodError, "undefined method #{self.class.name}#work" end |