Sha256: 6a921f482f5ffd0137643c9922d9754b252bf98d28e58d75a4dbea4bb1b3d60c

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

module Yaks
  class Mapper
    class Config
      include Equalizer.new(:attributes)

      def initialize(attributes = Hamster.list, links = Hamster.list, associations = Hamster.list, profile = nil)
        @attributes   = attributes
        @links        = links
        @associations = associations
        @profile      = profile
        freeze
      end

      def new(updates)
        self.class.new(
          updates.fetch(:attributes)   { attributes   },
          updates.fetch(:links)        { links        },
          updates.fetch(:associations) { associations },
          updates.fetch(:profile)      { profile      },
        )
      end

      def attributes(*attrs)
        return @attributes if attrs.empty?
        new(
          attributes: @attributes + attrs.to_list
        )
      end

      def link(rel, template, options = {})
        new(
          links: @links.cons(Link.new(rel, template, options))
        )
      end

      def profile(type = Undefined)
        return @profile if type == Undefined
        new(
          profile: type
        )
      end

      # key
      # embed_style
      # rel
      # (profile)

      def has_one(name, options = {})
        add_association(HasOne, name, options)
      end

      def has_many(name, options = {})
        add_association(HasMany, name, options)
      end

      def add_association(type, name, options = {})
        new(
          associations: @associations.cons(
            type.new(
              name,
              options.fetch(:as)     { name },
              options.fetch(:mapper) { Undefined },
              options.fetch(:links)  { Yaks::List() },
              options.reject         {|k,v| [:as, :mapper, :links].include?(k) }
            )
          )
        )
      end

      def links
        @links
      end

      def associations
        @associations
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yaks-0.3.1 lib/yaks/mapper/config.rb
yaks-0.3.0 lib/yaks/mapper/config.rb
yaks-0.2.0 lib/yaks/mapper/config.rb
yaks-0.1.0 lib/yaks/mapper/config.rb