class Mockingjay::Deserialize

Attributes

data[R]

Public Class Methods

new(raw_data) click to toggle source

Takes in a JSON string, and creates a new hash full of data

# File lib/mockingjay/deserialize.rb, line 6
def initialize(raw_data)
  @data = reduce_hash(JSON.parse(raw_data))
end

Private Instance Methods

generator_for(type, args = nil) click to toggle source

When given a generator, if it exists it will send data to it. If not, an unknown generator will be called.

# File lib/mockingjay/deserialize.rb, line 36
def generator_for(type, args = nil)
  Generator.respond_to?(type) ? Generator.send(type, args) : Generator.unknown(type)
end
interpret(value) click to toggle source
# File lib/mockingjay/deserialize.rb, line 27
def interpret(value)
  case
  when value.class == Hash  then reduce_hash(value)
  when value.class == Array then reduce_array(value)
  end
end
reduce_array(data) click to toggle source
# File lib/mockingjay/deserialize.rb, line 23
def reduce_array(data)
  data.reduce([]) { |array, value| array << interpret(value) }
end
reduce_hash(data) click to toggle source

Reduces a hash into generated values by looking for Generator hooks

# File lib/mockingjay/deserialize.rb, line 13
def reduce_hash(data)
  data.each_pair.reduce({}) { |data, (key, value)|
    if (match = key.match(/Generator\.(?<f>.+)/))
      generator_for(match[:f], value)
    else
      data.merge!({ key.to_sym => interpret(value) })
    end
  }
end