Class: Minicron::Hub::HostSerializer

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

Instance Method Summary collapse

Constructor Details

#initialize(hosts) ⇒ HostSerializer

Returns a new instance of HostSerializer.


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

def initialize(hosts)
  @hosts = hosts
end

Instance Method Details

#do_serialization(host) ⇒ 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
# File 'lib/minicron/hub/serializers/host.rb', line 25

def do_serialization(host)
  new_host = {}

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

    new_host[key] = value
  end

  # Set up the job host output ids array
  new_host[:jobs] = []

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

      # Append the job
      new_job[key] = value
    end

    @response[:jobs].push(new_job)
    new_host[:jobs].push(job.id)
  end

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

#serializeObject


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

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

  if @hosts.respond_to? :each
    @hosts.each do |host|
      do_serialization(host)
    end
  else
    do_serialization(@hosts)
  end

  @response
end