lib/ivy/serializers/formats/json_api.rb in ivy-serializers-0.2.0 vs lib/ivy/serializers/formats/json_api.rb in ivy-serializers-0.3.0

- old
+ new

@@ -2,39 +2,42 @@ module Ivy module Serializers module Formats class JSONAPI < JSON - def attribute(key, value) - value = coerce_id(value) if key == :id - super + def attributes(resource) + @hash_gen.store_object(:attributes) { super } end def belongs_to(name, resource, options={}) - @hash_gen.store_object(name) { linkage(resource) } + if resource + @hash_gen.store_object(name) { linkage(resource) } + else + @hash_gen.store(name, nil) + end end def has_many(name, resources, options={}) @hash_gen.store_object(name) { linkages(resources) } end - def linked(document) + def included(document) @hash_gen.store_object(:included) { super } end - def links(document) - @hash_gen.store_object(:links) { super } - end - def primary_resource(primary_resource_name, primary_resource) super(:data, primary_resource) end def primary_resources(primary_resources_name, primary_resources) super(:data, primary_resources) end + def relationships(document) + @hash_gen.store_object(:relationships) { super } + end + def resource(resource) super type(:type, resource) end @@ -47,19 +50,19 @@ def extract_id(resource) coerce_id(super) end def linkage(resource) - @hash_gen.store_object(:linkage) { linkage_object(resource) } + @hash_gen.store_object(:data) { linkage_object(resource) } end def linkage_object(resource) id(:id, resource) type(:type, resource) end def linkages(resources) - @hash_gen.store_array(:linkage) { linkage_objects(resources) } + @hash_gen.store_array(:data) { linkage_objects(resources) } end def linkage_objects(resources) resources.each { |resource| @hash_gen.push_object { linkage_object(resource) } } end