Class: FlatMap::Mapping

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/flat_map/mapping.rb

Overview

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

Defined Under Namespace

Modules: Reader, Writer Classes: Factory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapper, name, target_attribute, options = {}) ⇒ Mapping

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.

Parameters:

  • mapper (FlatMap::Mapper)
  • name (Symbol)
  • target_attribute (Symbol)
  • options (Hash) (defaults to: {})
  • [Symbol, (Hash)

    a customizable set of options

  • [Symbol] (Hash)

    a customizable set of options

  • [Class] (Hash)

    a customizable set of options



37
38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#full_nameObject (readonly)

Returns the value of attribute full_name.



11
12
13
# File 'lib/flat_map/mapping.rb', line 11

def full_name
  @full_name
end

#mapperObject (readonly)

Returns the value of attribute mapper.



11
12
13
# File 'lib/flat_map/mapping.rb', line 11

def mapper
  @mapper
end

#multiparamObject (readonly)

Returns the value of attribute multiparam.



13
14
15
# File 'lib/flat_map/mapping.rb', line 13

def multiparam
  @multiparam
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/flat_map/mapping.rb', line 11

def name
  @name
end

#readerObject (readonly)

Returns the value of attribute reader.



12
13
14
# File 'lib/flat_map/mapping.rb', line 12

def reader
  @reader
end

#target_attributeObject (readonly)

Returns the value of attribute target_attribute.



11
12
13
# File 'lib/flat_map/mapping.rb', line 11

def target_attribute
  @target_attribute
end

#writerObject (readonly)

Returns the value of attribute writer.



12
13
14
# File 'lib/flat_map/mapping.rb', line 12

def writer
  @writer
end

Instance Method Details

#multiparam?Boolean

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

Returns:

  • (Boolean)


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

def multiparam?
  !!@multiparam
end

#read_as_paramsHash

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.

Returns:

  • (Hash)


72
73
74
# File 'lib/flat_map/mapping.rb', line 72

def read_as_params
  reader ? {full_name => read} : {}
end

#write_from_params(params) ⇒ Object

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

Parameters:

  • params (Hash)

Returns:

  • (Object)

    value assigned



63
64
65
# 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