Class: Synapse::Mapping::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/mapping/mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(duplicates_allowed) ⇒ undefined

Parameters:

  • duplicates_allowed (Boolean)


6
7
8
9
# File 'lib/synapse/mapping/mapper.rb', line 6

def initialize(duplicates_allowed)
  @duplicates_allowed = duplicates_allowed
  @mappings = Array.new
end

Instance Method Details

#each_type {|Class| ... } ⇒ undefined

Yields the type that each mapping is registered for

Yields:

  • (Class)

Returns:

  • (undefined)


15
16
17
18
19
# File 'lib/synapse/mapping/mapper.rb', line 15

def each_type
  @mappings.each do |mapping|
    yield mapping.type
  end
end

#map(type, *args, &block) ⇒ undefined

Parameters:

  • type (Class)
  • args (Object...)
  • block (Proc)

Returns:

  • (undefined)

Raises:

  • (DuplicateMappingError)

    If duplicates aren’t allowed and another mapping exists that maps the exact same type as the given mapping



27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Mapping

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

Parameters:

  • target_type (Class)

Returns:



45
46
47
48
49
# File 'lib/synapse/mapping/mapper.rb', line 45

def mapping_for(target_type)
  @mappings.find do |mapping|
    mapping.type >= target_type
  end
end