lib/jsonapi_mapper.rb in jsonapi_mapper-0.1.2 vs lib/jsonapi_mapper.rb in jsonapi_mapper-0.1.3

- old
+ new

@@ -16,34 +16,34 @@ Resource = Struct.new(:object, :relationships, :id) Rule = Struct.new(:attributes, :scope) class DocumentMapper attr_accessor :document, :unscoped, :types, :renames, :resources, - :included, :data_mappable + :included, :data def initialize(document, unscoped, rules, renames) self.document = document.deep_symbolize_keys self.renames = renames.deep_symbolize_keys self.unscoped = unscoped.map(&:to_sym) self.resources = {} setup_types(rules) - main = if data_mappable = self.document[:data] - if data_mappable.is_a?(Array) - data_mappable.map{|r| build_resource(r) }.compact + main = if data = self.document[:data] + if data.is_a?(Array) + data.map{|r| build_resource(r) }.compact.collect(&:object) else - [ build_resource(data_mappable) ].compact + build_resource(data).try(:object) end end rest = if included = self.document[:included] included.map{|r| build_resource(r) }.compact end resources.each{|_,r| assign_relationships(r) } - self.data_mappable = main.try(:map, &:object) || [] + self.data = main self.included = rest.try(:map, &:object) || [] end def setup_types(rules) self.types = {} @@ -162,11 +162,11 @@ all.each(&:save) true end def collection? - data_mappable.size > 1 + data.is_a?(Array) end def single? !collection? end @@ -177,10 +177,10 @@ def map_all(cls, &blk) all.select{|o| o.is_a?(cls)}.map(&blk) end - def data - collection? ? data_mappable : data_mappable.first + def data_mappable + collection? ? data : [data].compact end end end