Class: StatModule::JSONable
- Inherits:
-
Object
- Object
- StatModule::JSONable
- Defined in:
- lib/JSONable.rb
Constant Summary collapse
- FORMATTING_STAR =
'⭐'- FORMATTING_CHECKMARK =
'✅'- FORMATTING_BALL =
'⚫'- FORMATTING_WARNING =
'⚠'
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(hash) ⇒ JSONable
constructor
A new instance of JSONable.
- #to_json(excluded_fields = []) ⇒ Object
Constructor Details
#initialize(hash) ⇒ JSONable
Returns a new instance of JSONable.
12 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 47 |
# File 'lib/JSONable.rb', line 12 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
57 58 59 60 61 |
# File 'lib/JSONable.rb', line 57 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
49 50 51 52 53 54 55 |
# File 'lib/JSONable.rb', line 49 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 hash.to_json end |