Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6cfd16be5a
Fetching contributors…

Cannot retrieve contributors at this time

63 lines (50 sloc) 1.382 kb
module Ixtlan
module Babel
class Deserializer
def initialize(model_class)
@model_class = model_class
end
private
def self.filter
@filter ||= HashFilter.new
end
def filter
@filter ||= self.class.filter.dup
end
protected
def self.default_context_key(default)
filter.default_context_key(default)
end
def self.add_context(key, options = {})
filter[key] = options
end
public
def use(context_or_options)
filter.use(context_or_options)
self
end
def from_hash(data, options = nil)
filter.use( options ) if options
if data.is_a? Array
if filter.root
root = filter.root.to_s
data.collect{ |d| @model_class.new( filter.filter( d[ root ] ) ) }
else
data.collect{ |d| @model_class.new( filter.filter( d ) ) }
end
else
data = data[ filter.root.to_s ] if filter.root
@model_class.new( filter.filter( data ) )
end
end
def from_json(json, options = nil)
data = JSON.parse(json)
from_hash(data, options)
end
def from_yaml(yaml, options = nil)
data = YAML.load_stream(StringIO.new(yaml)).documents
from_hash(data, options)
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.