Class: Minicron::Hub::ExecutionSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/minicron/hub/serializers/execution.rb

Instance Method Summary collapse

Constructor Details

#initialize(executions) ⇒ ExecutionSerializer

Returns a new instance of ExecutionSerializer.


4
5
6
# File 'lib/minicron/hub/serializers/execution.rb', line 4

def initialize(executions)
  @executions = executions
end

Instance Method Details

#do_serialization(execution) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/minicron/hub/serializers/execution.rb', line 27

def do_serialization(execution)
  new_execution = {}

  # Add all the normal attributes of the execution
  execution.attributes.each do |key, value|
    # Remove _id from keys
    key = key[-3, 3] == '_id' ? key[0..-4] : key

    new_execution[key] = value
  end

  # Set up the job execution output ids array
  new_execution[:job_execution_outputs] = []

  # Add the job to the sideloaded data
  new_job = {}
  execution.job.attributes.each do |key, value|
    # To make our name method in the model work :/
    value = execution.job.name if key == 'name'

    # Remove _id from keys
    key = key[-3, 3] == '_id' ? key[0..-4] : key

    new_job[key] = value
  end

  # Append the new job to the @response
  @response[:jobs].push(new_job)

  # Append the job host to the @response
  @response[:hosts].push(execution.job.host)

  # Add the job execution outputs to the sideloaded data and the ids to
  # the execution
  execution.job_execution_outputs.each do |job_execution_output|
    new_job_execution_output = {}

    job_execution_output.attributes.each do |key, value|
      # Remove _id from keys
      key = key[-3, 3] == '_id' ? key[0..-4] : key
      new_job_execution_output[key] = value
    end

    @response[:job_execution_outputs].push(new_job_execution_output)
    new_execution[:job_execution_outputs].push(job_execution_output.id)
  end

  # Append the new execution to the @response
  @response[:executions].push(new_execution)
end

#serializeObject


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/minicron/hub/serializers/execution.rb', line 8

def serialize
  @response = {
    :executions => [],
    :jobs => [],
    :job_execution_outputs => [],
    :hosts => []
  }

  if @executions.respond_to? :each
    @executions.each do |execution|
      do_serialization(execution)
    end
  else
    do_serialization(@executions)
  end

  @response
end