lib/ivy/serializers/mapping.rb in ivy-serializers-0.2.0 vs lib/ivy/serializers/mapping.rb in ivy-serializers-0.3.0
- old
+ new
@@ -4,14 +4,12 @@
module Ivy
module Serializers
class Mapping
def initialize(klass)
@attrs = {}
- @links = {}
+ @relationships = {}
@klass = klass
-
- attribute(:id)
end
def attribute(name, &block)
@attrs[name] = Attribute.new(name, &block)
end
@@ -19,22 +17,27 @@
def attributes(*names)
names.each { |name| attribute(name) }
end
def belongs_to(name, options={}, &block)
- @links[name] = Relationships::BelongsTo.new(name, options, &block)
+ @relationships[name] = Relationships::BelongsTo.new(name, options, &block)
end
+ def generate_attributes(generator, resource)
+ @attrs.each_value { |attr| attr.generate(generator, resource) }
+ end
+
def has_many(name, options={}, &block)
- @links[name] = Relationships::HasMany.new(name, options, &block)
+ @relationships[name] = Relationships::HasMany.new(name, options, &block)
end
- def links(generator, resource)
- @links.each_value { |link| link.generate(generator, resource) }
+ def relationships(generator, resource)
+ @relationships.each_value { |relationship| relationship.generate(generator, resource) }
end
def resource(generator, resource)
- @attrs.each_value { |attr| attr.generate(generator, resource) }
+ generator.attributes(resource) unless @attrs.empty?
+ generator.relationships(resource) unless @relationships.empty?
end
end
end
end