Class: Nomad::Stanza::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/nomad/stanza/serializer.rb

Class Method Summary collapse

Class Method Details

.build_proc(obj, key, id = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nomad/stanza/serializer.rb', line 13

def build_proc(obj, key, id = nil)
  proc do
    add *[key, id].compact do
      obj.class.schema.each do |key|
        k = key.name
        v = obj.attributes[k]
        if k == :id or v.nil?
          next
        elsif v.is_a? Nomad::Stanza::Base
          if v.class.has_attribute?(:id)
            instance_eval &build_proc(v, k, v.id)
          else
            instance_eval &build_proc(v, k)
          end
        elsif v.is_a? Array
          member = obj.class.schema.key(k).type.type.member
          if member.is_a? Class and member.ancestors.include? Nomad::Stanza::Base
            v.each do |i|
              if i.class.has_attribute?(:id)
                instance_eval &build_proc(i, k, i.id)
              else
                instance_eval &build_proc(i, k)
              end
            end
          else
            add k, v
          end
        else
          add k, v
        end
      end
    end
  end
end

.serialize(obj, indent: 2) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
# File 'lib/nomad/stanza/serializer.rb', line 5

def serialize(obj, indent: 2)
  raise ArgumentError, 'Nomad::Stanza::Job is required.' unless obj.is_a? Nomad::Stanza::Job

  builder = Hydrochlorb.build do
    instance_eval &build_proc(obj, 'job', obj.id)
  end.to_hcl(indent: indent)
end