Sha256: e537fca0d6ef83377ea029b09790b641897a226a0197d911ca841c132d9ab724

Contents?: true

Size: 957 Bytes

Versions: 2

Compression:

Stored size: 957 Bytes

Contents

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

module Ivy
  module Serializers
    class Mapping
      def initialize(klass)
        @attrs = {}
        @links = {}
        @klass = klass

        attribute(:id)
      end

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

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

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

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

      def links(generator, resource)
        @links.each_value { |link| link.generate(generator, resource) }
      end

      def resource(generator, resource)
        @attrs.each_value { |attr| attr.generate(generator, resource) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ivy-serializers-0.2.0 lib/ivy/serializers/mapping.rb
ivy-serializers-0.1.0 lib/ivy/serializers/mapping.rb