Module: Marty::Diagnostic::Packer

Included in:
Base
Defined in:
lib/marty/diagnostic/packer.rb

Instance Method Summary collapse

Instance Method Details

#create_info(description, status = true, consistent = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/marty/diagnostic/packer.rb', line 26

def create_info description, status = true, consistent = nil
  {
    'description' => description.to_s,
    'status'      => status,
    'consistent'  => consistent
  }
end

#error(description) ⇒ Object



38
39
40
# File 'lib/marty/diagnostic/packer.rb', line 38

def error description
  create_info(description, false)
end

#is_valid_info?(info) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/marty/diagnostic/packer.rb', line 34

def is_valid_info? info
  info.keys.to_set == Set['description', 'status', 'consistent']
end

#pack(include_ip = true) ⇒ Object



2
3
4
5
# File 'lib/marty/diagnostic/packer.rb', line 2

def pack include_ip = true
  info = process(yield)
  include_ip ? { Node.my_ip => info } : info
end

#process(obj) ⇒ Object



7
8
9
10
# File 'lib/marty/diagnostic/packer.rb', line 7

def process obj
  obj.is_a?(Hash) ? process_hash(obj) :
    { name.demodulize => create_info(obj.to_s) }
end

#process_hash(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/marty/diagnostic/packer.rb', line 12

def process_hash data
  return { name.demodulize => data } if is_valid_info?(data)

  data.each_with_object({}) do |(k, v), h|
    if v.is_a?(Hash)
      raise "Invalid Diagnostic Info #{v}" unless is_valid_info?(v)

      h[k] = v
    else
      h[k] = create_info(v)
    end
  end
end