Sha256: fbc9dd9985f9ba40bfd6aa3eaf8a9d2fe63e2f422ca80b6af47b92ee7ee8e224

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'ivy/serializers/attribute'
require 'ivy/serializers/relationships'

module Ivy
  module Serializers
    class Mapping
      def initialize(klass)
        @attributes = {}
        @relationships = {}
        @klass = klass
      end

      def attribute(name, &block)
        @attributes[name] = Attribute.new(name, &block)
      end

      def attributes(*names)
        names.each { |name| attribute(name) }
      end

      def belongs_to(name, options={}, &block)
        @relationships[name] = Relationships::BelongsTo.new(name, options, &block)
      end

      def generate_attributes(generator, resource)
        @attributes.each_value { |attribute| attribute.generate(generator, resource) }
      end

      def has_many(name, options={}, &block)
        @relationships[name] = Relationships::HasMany.new(name, options, &block)
      end

      def relationships(generator, resource)
        @relationships.each_value { |relationship| relationship.generate(generator, resource) }
      end

      def resource(generator, resource)
        generator.attributes(resource) unless @attributes.empty?
        generator.relationships(resource) unless @relationships.empty?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ivy-serializers-0.4.0 lib/ivy/serializers/mapping.rb