class Synapse::Mapping::Mapper

Public Class Methods

new(duplicates_allowed) click to toggle source

@param [Boolean] duplicates_allowed @return [undefined]

# File lib/synapse/mapping/mapper.rb, line 6
def initialize(duplicates_allowed)
  @duplicates_allowed = duplicates_allowed
  @mappings = Array.new
end

Public Instance Methods

each_type() { |type| ... } click to toggle source

Yields the type that each mapping is registered for

@yield [Class] @return [undefined]

# File lib/synapse/mapping/mapper.rb, line 15
def each_type
  @mappings.each do |mapping|
    yield mapping.type
  end
end
map(type, *args, &block) click to toggle source

@raise [DuplicateMappingError] If duplicates aren't allowed and another mapping exists that

maps the exact same type as the given mapping

@param [Class] type @param [Object…] args @param [Proc] block @return [undefined]

# File lib/synapse/mapping/mapper.rb, line 27
def map(type, *args, &block)
  options = args.extract_options!
  mapping = create_from type, options, &block

  unless @duplicates_allowed
    if @mappings.include? mapping
      raise DuplicateMappingError
    end
  end

  @mappings.push mapping
  @mappings.sort!
end
mapping_for(target_type) click to toggle source

Retrieves the most specific mapping for a given type, if any

@param [Class] target_type @return [Mapping]

# File lib/synapse/mapping/mapper.rb, line 45
def mapping_for(target_type)
  @mappings.find do |mapping|
    mapping.type >= target_type
  end
end

Private Instance Methods

create_from(type, options, &block) click to toggle source

@param [Class] type @param [Hash] options @param [Proc] block @return [Mapping]

# File lib/synapse/mapping/mapper.rb, line 57
def create_from(type, options, &block)
  to = options.delete :to
  unless to
    unless block
      raise ArgumentError, 'Expected block or option :to'
    end

    to = block
  end

  Mapping.new type, options, to
end