Class: Minicron::Hub::ScheduleSerializer

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

Instance Method Summary collapse

Constructor Details

#initialize(schedules) ⇒ ScheduleSerializer

Returns a new instance of ScheduleSerializer.


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

def initialize(schedules)
  @schedules = schedules
end

Instance Method Details

#do_serialization(schedule) ⇒ Object


25
26
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
# File 'lib/minicron/hub/serializers/schedule.rb', line 25

def do_serialization(schedule)
  new_schedule = {}

  # Add all the normal attributes of the 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

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

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

    new_job[key] = value
  end

  # Add the ids of each job schedule to the job
  schedule.job.schedules.each do |s|
    new_job[:schedules].push(s.id)
  end

  # Add the ids of each job execution to the job
  schedule.job.executions.each do |execution|
    new_job[:executions].push(execution.id)
  end

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

  # Append the new schedule to the @responseh
  @response[:schedules].push(new_schedule)
end

#serializeObject


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

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

  if @schedules.respond_to? :each
    @schedules.each do |schedule|
      do_serialization(schedule)
    end
  else
    do_serialization(@schedules)
  end

  @response
end