Class: JobWorkflow::TaskOutput

Inherits:
Object
  • Object
show all
Defined in:
lib/job_workflow/task_output.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name:, each_index:, data: {}) ⇒ TaskOutput

: (task_name: Symbol, each_index: Integer, ?data: Hash[Symbol, untyped]) -> void



28
29
30
31
32
# File 'lib/job_workflow/task_output.rb', line 28

def initialize(task_name:, each_index:, data: {})
  @task_name = task_name
  @each_index = each_index
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object

: …



40
41
42
43
44
# File 'lib/job_workflow/task_output.rb', line 40

def method_missing(name, *args, **kwargs, &block)
  return data[name.to_sym] if data.key?(name.to_sym) && args.empty? && kwargs.empty? && block.nil?

  super
end

Instance Attribute Details

#dataObject (readonly)

: Hash[Symbol, untyped]



7
8
9
# File 'lib/job_workflow/task_output.rb', line 7

def data
  @data
end

#each_indexObject (readonly)

: Integer



6
7
8
# File 'lib/job_workflow/task_output.rb', line 6

def each_index
  @each_index
end

#task_nameObject (readonly)

: Symbol



5
6
7
# File 'lib/job_workflow/task_output.rb', line 5

def task_name
  @task_name
end

Class Method Details

.deserialize(hash) ⇒ Object

: (Hash[String, untyped]) -> TaskOutput



18
19
20
21
22
23
24
# File 'lib/job_workflow/task_output.rb', line 18

def deserialize(hash)
  new(
    task_name: hash["task_name"].to_sym,
    each_index: hash["each_index"],
    data: ActiveJob::Arguments.deserialize([hash["data"]]).first
  )
end

.from_task(task:, data:, each_index:) ⇒ Object

: (task: Task, each_index: Integer, data: Hash[Symbol, untyped]) -> TaskOutput



11
12
13
14
15
# File 'lib/job_workflow/task_output.rb', line 11

def from_task(task:, data:, each_index:)
  normalized_data = task.output.to_h { |output_def| [output_def.name, nil] }
  normalized_data.merge!(data.slice(*normalized_data.keys))
  new(task_name: task.task_name, each_index:, data: normalized_data)
end

Instance Method Details

#respond_to_missing?(sym, include_private) ⇒ Boolean

: (Symbol, bool) -> bool

Returns:

  • (Boolean)


47
48
49
# File 'lib/job_workflow/task_output.rb', line 47

def respond_to_missing?(sym, include_private)
  data.key?(sym) || super
end

#serializeObject

: () -> Hash[String, untyped]



35
36
37
# File 'lib/job_workflow/task_output.rb', line 35

def serialize
  { "task_name" => task_name.to_s, "each_index" => each_index, "data" => ActiveJob::Arguments.serialize([data]).first }
end