class FlatMap::Mapping

Encapsulates mapping concept used by mappers. Each mapping belongs to a particular mapper and has its own reader and writer objects.

Attributes

full_name[R]
mapper[R]
multiparam[R]
name[R]
reader[R]
target_attribute[R]
writer[R]

Public Class Methods

new(mapper, name, target_attribute, options = {}) click to toggle source

Initialize a mapping, passing to it a mapper, which is a gateway to actual target, name, which is an external identifier, target_attribute, which is used to access actual information of the target, and options.

@param [FlatMap::Mapper] mapper @param [Symbol] name @param [Symbol] target_attribute @param [Hash] options @option [Symbol, Proc] :reader specifies how value will

be read from the +target+

@option [Symbol] :format specifies additional processing

of the value on reading

@option [Symbol, Proc] :writer specifies how value will

be written to the +target+

@option [Class] :multiparam specifies multiparam Class,

object of which will be instantiated on writing
multiparam attribute passed from the Rails form
# File lib/flat_map/mapping.rb, line 37
def initialize(mapper, name, target_attribute, options = {})
  @mapper           = mapper
  @name             = name
  @target_attribute = target_attribute

  @full_name = mapper.suffixed? ? :"#{@name}_#{mapper.suffix}" : name

  @multiparam = options[:multiparam]

  fetch_reader(options)
  fetch_writer(options)
end

Public Instance Methods

multiparam?() click to toggle source

Return true if the mapping was created with the :multiparam option set.

@return [Boolean]

# File lib/flat_map/mapping.rb, line 54
def multiparam?
  !!@multiparam
end
read_as_params() click to toggle source

Return a hash of a single key => value pair, where key corresponds to full_name and value to value read from target. If reader is not set, return an empty hash.

@return [Hash]

# File lib/flat_map/mapping.rb, line 72
def read_as_params
  reader ? {full_name => read} : {}
end
write_from_params(params) click to toggle source

Lookup the passed hash of params for the key that corresponds to the full_name of self, and write it if it is present.

@param [Hash] params @return [Object] value assigned

# File lib/flat_map/mapping.rb, line 63
def write_from_params(params)
  write(params[full_name]) if params.key?(full_name) && writer.present?
end

Private Instance Methods

fetch_reader(options) click to toggle source

Instantiate a reader object based on the :reader and :format values of options.

@param [Hash] options @return [FlatMap::Mapping::Reader::Basic]

# File lib/flat_map/mapping.rb, line 81
def fetch_reader(options)
  options_reader = options[:reader]

  @reader =
    case options_reader
    when Symbol, String
      Reader::Method.new(self, options_reader)
    when Proc
      Reader::Proc.new(self, options_reader)
    when false
      nil
    else
      if options.key?(:format) then
        Reader::Formatted.new(self, options[:format])
      else
        Reader::Basic.new(self)
      end
    end
end
fetch_writer(options) click to toggle source

Instantiate a writer object based on the :writer value of options.

@param [Hash] options @return [FlatMap::Mapping::Writer::Basic]

# File lib/flat_map/mapping.rb, line 107
def fetch_writer(options)
  options_writer = options[:writer]

  @writer =
    case options_writer
    when Symbol, String
      Writer::Method.new(self, options_writer)
    when Proc
      Writer::Proc.new(self, options_writer)
    when false
      nil
    else
      Writer::Basic.new(self)
    end
end