Class: FlatMap::Mapping
- Inherits:
-
Object
- Object
- FlatMap::Mapping
- 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
-
#full_name ⇒ Object
readonly
Returns the value of attribute full_name.
-
#mapper ⇒ Object
readonly
Returns the value of attribute mapper.
-
#multiparam ⇒ Object
readonly
Returns the value of attribute multiparam.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
-
#target_attribute ⇒ Object
readonly
Returns the value of attribute target_attribute.
-
#writer ⇒ Object
readonly
Returns the value of attribute writer.
Instance Method Summary collapse
-
#initialize(mapper, name, target_attribute, options = {}) ⇒ Mapping
constructor
Initialize a mapping, passing to it a
mapper
, which is a gateway to actualtarget
,name
, which is an external identifier,target_attribute
, which is used to access actual information of thetarget
, andoptions
. -
#multiparam? ⇒ Boolean
Return
true
if the mapping was created with the:multiparam
option set. -
#read_as_params ⇒ Hash
Return a hash of a single key => value pair, where key corresponds to
full_name
andvalue
to value read fromtarget
. -
#write_from_params(params) ⇒ Object
Lookup the passed hash of params for the key that corresponds to the
full_name
ofself
, and write it if it is present.
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
.
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, = {}) @mapper = mapper @name = name @target_attribute = target_attribute @full_name = mapper.suffixed? ? :"#{@name}_#{mapper.suffix}" : name @multiparam = [:multiparam] fetch_reader() fetch_writer() end |
Instance Attribute Details
#full_name ⇒ Object (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 |
#mapper ⇒ Object (readonly)
Returns the value of attribute mapper.
11 12 13 |
# File 'lib/flat_map/mapping.rb', line 11 def mapper @mapper end |
#multiparam ⇒ Object (readonly)
Returns the value of attribute multiparam.
13 14 15 |
# File 'lib/flat_map/mapping.rb', line 13 def multiparam @multiparam end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/flat_map/mapping.rb', line 11 def name @name end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
12 13 14 |
# File 'lib/flat_map/mapping.rb', line 12 def reader @reader end |
#target_attribute ⇒ Object (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 |
#writer ⇒ Object (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.
54 55 56 |
# File 'lib/flat_map/mapping.rb', line 54 def multiparam? !!@multiparam end |
#read_as_params ⇒ Hash
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.
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.
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 |