Class: StatModule::JSONable

Inherits:
Object
  • Object
show all
Defined in:
lib/JSONable.rb

Direct Known Subclasses

Detail, Finding, Fix, Location, Process, Stat

Constant Summary collapse

FORMATTING_STAR =
''
FORMATTING_CHECKMARK =
''
FORMATTING_BALL =
''
FORMATTING_WARNING =
''

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ JSONable

Initialize object extending JSONable

Params:

hash

Hash



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
47
48
49
50
51
52
# File 'lib/JSONable.rb', line 17

def initialize(hash)
  if hash.is_a? Hash
    hash.each do |k, v|
      if v.is_a? Array
        items = []
        v.each { |i|
          case k
            when 'findings'
              item = StatModule::Finding.new(nil, nil, nil, i)
            when 'fixes'
              item = StatModule::Fix.new(nil, i)
            when 'traces'
              item = StatModule::Location.new(nil, i)
            else
              v = item
          end
          items.push(item)
        }
        v = items
      end
      if v.is_a? Hash
        case k
          when 'process'
            v = StatModule::Process.new(nil, v)
          when 'location'
            v = StatModule::Location.new(nil, v)
          when 'detail'
            v = StatModule::Detail.new(nil, v)
        end
      end
      self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
      self.class.send(:define_method, k, proc { self.instance_variable_get("@#{k}") }) ## create the getter that returns the instance variable
      self.class.send(:define_method, "#{k}=", proc { |v| self.instance_variable_set("@#{k}", v) }) ## create the setter that sets the instance variable
    end
  end
end

Class Method Details

.from_json!(string) ⇒ Object

Generate Hash from json string



69
70
71
72
73
# File 'lib/JSONable.rb', line 69

def self.from_json!(string)
  JSON.load(string).each do |var, val|
    self.instance_variable_set '@' + var, val
  end
end

Instance Method Details

#to_json(excluded_fields = []) ⇒ Object

Get object in pretty json format

Params:

excluded_fields

array of String - attributes to exclude



59
60
61
62
63
64
65
# File 'lib/JSONable.rb', line 59

def to_json(excluded_fields = [])
  hash = {}
  self.instance_variables.each do |var|
    hash[var.to_s.delete "@"] = self.instance_variable_get var unless excluded_fields.is_a?(Array) && excluded_fields.include?(var.to_s.delete "@")
  end
  JSON.pretty_generate(hash)
end