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)
- .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)
- .required ⇒ Object
-
.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.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cmdx/task.rb', line 88 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) 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
185 186 187 |
# File 'lib/cmdx/task.rb', line 185 def attributes(...) register(:attribute, Attribute.build(...)) end |
.deregister(type, object) ⇒ Object
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/cmdx/task.rb', line 169 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.
250 251 252 253 254 |
# File 'lib/cmdx/task.rb', line 250 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.
269 270 271 272 273 |
# File 'lib/cmdx/task.rb', line 269 def execute!(*args, **kwargs) task = new(*args, **kwargs) task.execute(raise: true) block_given? ? yield(task.result) : task.result end |
.optional ⇒ Object
195 196 197 |
# File 'lib/cmdx/task.rb', line 195 def optional(...) register(:attribute, Attribute.optional(...)) end |
.register(type, object) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cmdx/task.rb', line 148 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
214 215 216 |
# File 'lib/cmdx/task.rb', line 214 def remove_attributes(*names) deregister(:attribute, names) end |
.required ⇒ Object
204 205 206 |
# File 'lib/cmdx/task.rb', line 204 def required(...) register(:attribute, Attribute.required(...)) end |
.settings(**options) ⇒ Hash
Returns The merged settings hash.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cmdx/task.rb', line 113 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[:tags] ||= [] hash.merge!() end end |
Instance Method Details
#execute(raise: false) ⇒ Result
Returns The execution result.
286 287 288 289 |
# File 'lib/cmdx/task.rb', line 286 def execute(raise: false) Executor.execute(self, raise:) block_given? ? yield(result) : result end |
#logger ⇒ Logger
Returns The logger instance for this task.
313 314 315 316 317 318 319 320 |
# File 'lib/cmdx/task.rb', line 313 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.
337 338 339 340 341 342 343 344 345 346 |
# File 'lib/cmdx/task.rb', line 337 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, id: } end |
#to_s ⇒ String
Returns A string representation of the task.
355 356 357 |
# File 'lib/cmdx/task.rb', line 355 def to_s Utils::Format.to_str(to_h) end |
#work ⇒ Object
302 303 304 |
# File 'lib/cmdx/task.rb', line 302 def work raise UndefinedMethodError, "undefined method #{self.class.name}#work" end |