Module: FlatMap::OpenMapper::Mapping::ClassMethods

Defined in:
lib/flat_map/open_mapper/mapping.rb

Overview

Mapping class macros

Constant Summary collapse

MAPPING_OPTIONS =

Mapping-modifier options to distinguish options from mappings themselves:

[:reader, :writer, :format, :multiparam].freeze

Instance Method Summary collapse

Instance Method Details

#map(*args) ⇒ Object

Define single or multiple mappings at a time. Usually, a Hash is passed in a form {mapping_name => target_attribute}. All keys that are listed under MAPPING_OPTIONS will be extracted and used as modifiers for new mappings.

Also, mapping names may be listed as an array preceding the hash. In that case, its elements are treated as {mapping_name => mapping_name} mapping elements.

Example:

map :brand, :account_source => :source, :format => :enum
# is equivalent to:
map :brand => :brand, :format => :enum
map :account_source => :source, :format => :enum


28
29
30
31
32
33
34
35
# File 'lib/flat_map/open_mapper/mapping.rb', line 28

def map(*args)
  mapping_options     = args.extract_options!
  mappings            = mapping_options.slice!(*MAPPING_OPTIONS)
  mappings_from_array = args.zip(args).flatten
  mappings.merge!(Hash[*mappings_from_array]) unless mappings_from_array.empty?

  define_mappings(mappings, mapping_options)
end

#mappingsArray<FlatMap::Mapping::Factory>

List of class mappings (mapping factories).

Returns:



55
56
57
# File 'lib/flat_map/open_mapper/mapping.rb', line 55

def mappings
  @mappings ||= []
end

#mappings=(val) ⇒ Object

Writer for mappings.



60
61
62
# File 'lib/flat_map/open_mapper/mapping.rb', line 60

def mappings=(val)
  @mappings = val
end