class Mockingjay::Serialize

Attributes

json[R]

Public Class Methods

new(raw_data) click to toggle source

Parses raw data to JSON

# File lib/mockingjay/serialize.rb, line 6
def initialize(raw_data)
  @json = reduce_hash(raw_data.is_a?(Hash) ? raw_data : JSON.parse(raw_data)).to_json
end

Public Instance Methods

save_as(name) click to toggle source

A bit of sugar for quicker saves

# File lib/mockingjay/serialize.rb, line 11
def save_as(name)
  File.open("#{name}.json", 'w') { |file| file.write @json }
end

Private Instance Methods

generator_for(value) click to toggle source

Finds the rule to substitute in place of a value

# File lib/mockingjay/serialize.rb, line 39
def generator_for(value)
  type = value_type_of(value)
  Generator.respond_to?(type) ? Rules.send(type) : Rules.unknown(type)
end
interpret(value) click to toggle source
# File lib/mockingjay/serialize.rb, line 25
def interpret(value)
  case
  when value.class == Hash  then reduce_hash(value)
  when value.class == Array then reduce_array(value)
  else generator_for(value)
  end
end
reduce_array(data) click to toggle source
# File lib/mockingjay/serialize.rb, line 21
def reduce_array(data)
  data.reduce([]) { |array, value| array << interpret(value) }
end
reduce_hash(data) click to toggle source
# File lib/mockingjay/serialize.rb, line 17
def reduce_hash(data)
  data.each_pair.reduce({}) { |data, (key, value)| data.merge!({ key => interpret(value) }) }
end
value_type_of(value) click to toggle source

Finds the type of a value, and turns into a symbol to hook rules

# File lib/mockingjay/serialize.rb, line 34
def value_type_of(value)
  value.class.to_s.downcase.to_sym
end