Sha256: ef17276200470f217109242805ad00ee4cc2cef9f910f936b985151a2183d6c3

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

module Yaks
  class DefaultPolicy
    include Util

    DEFAULTS = {
      rel_template: "rel:src={src}&dest={dest}",
      namespace: Kernel
    }

    attr_reader :options

    def initialize(options = {})
      @options = DEFAULTS.merge(options)
    end

    def derive_mapper_from_object(model)
      if model.respond_to? :to_ary
        if m = model.first
          name = m.class.name.split('::').last + 'CollectionMapper'
          begin
            return @options[:namespace].const_get(name)
          rescue NameError
          end
        end
        begin
          return @options[:namespace].const_get(:CollectionMapper)
        rescue NameError
        end
        Yaks::CollectionMapper
      else
        name = model.class.name.split('::').last
        @options[:namespace].const_get(name + 'Mapper')
      end
    end

    def derive_type_from_mapper_class(mapper_class)
      underscore(mapper_class.name.split('::').last.sub(/Mapper$/, ''))
    end

    def derive_mapper_from_association(association)
      @options[:namespace].const_get("#{camelize(association.singular_name)}Mapper")
    end

    def derive_rel_from_association(mapper, association)
      expand_rel( derive_type_from_mapper_class(mapper.class), association.name )
    end

    def expand_rel(src_name, dest_name)
      URITemplate.new(@options[:rel_template]).expand(
        src: src_name,
        dest: dest_name
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yaks-0.4.2 lib/yaks/default_policy.rb
yaks-0.4.1 lib/yaks/default_policy.rb