Class: Minicron::Hub::JobSerializer

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

Instance Method Summary collapse

Constructor Details

#initialize(jobs) ⇒ JobSerializer

Returns a new instance of JobSerializer.


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

def initialize(jobs)
  @jobs = jobs
end

Instance Method Details

#do_serialization(job) ⇒ 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/minicron/hub/serializers/job.rb', line 27

def do_serialization(job)
  new_job = {}

  # Add all the normal attributes of the job
  job.attributes.each do |key, value|
    # To make our name method in the model work :/
    value = job.name if key == 'name'

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

    new_job[key] = value
  end

  # Set up the execution ids array
  new_job[:executions] = []

  # Set up the schedules ids array
  new_job[:schedules] = []

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

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

    new_host[key] = value
  end

  # Append the new host to the @response
  @response[:hosts].push(new_host)

  # Add the schedules to the sideloaded data and the ids to
  # the job
  job.schedules.each do |schedule|
    new_schedule = {}

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

      new_schedule[key] = value
    end

    # Add the formatted version of the schedule
    new_schedule['formatted'] = schedule.formatted

    @response[:schedules].push(new_schedule)
    new_job[:schedules].push(schedule.id)
  end

  # Add the executions to the sideloaded data and the ids to
  # the job
  job.executions.each do |execution|
    new_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

    # Also we need to add the job execution output ids
    new_execution[:job_execution_outputs] = []
    execution.job_execution_outputs.each do |job_execution_output|
      new_execution[:job_execution_outputs].push(job_execution_output.id)
    end

    @response[:executions].push(new_execution)
    new_job[:executions].push(execution.id)
  end

  # Append the new job to the @responseh
  @response[:jobs].push(new_job)
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/job.rb', line 8

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

  if @jobs.respond_to? :each
    @jobs.each do |job|
      do_serialization(job)
    end
  else
    do_serialization(@jobs)
  end

  @response
end