Class: MappedRecord::Mapping
- Inherits:
-
Object
- Object
- MappedRecord::Mapping
- Defined in:
- lib/mapped-record/mapping.rb
Class Attribute Summary collapse
-
.named_mappings ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.[](key) ⇒ Object
Access named mappings straight from the class.
-
.[]=(key, values) ⇒ Object
Assign mappings straight from the class.
-
.all ⇒ Object
Returns all defined mappings.
-
.blank? ⇒ Boolean
Returns true of no mappings are assigned.
-
.create(mapping_name, *map_attrs) ⇒ Object
Assigns a named mapping accessible throughout the runtime environment.
-
.empty? ⇒ Boolean
Returns true of no mappings are assigned.
-
.has?(mapping_name) ⇒ Boolean
Returns true if mapping of
mapping_name
is assigned. -
.reset ⇒ Object
Clear all mappings.
-
.update_mapping(mapping_name, key, value, type) ⇒ Object
:nodoc:.
Class Attribute Details
.named_mappings ⇒ Object (readonly)
:nodoc:
104 105 106 |
# File 'lib/mapped-record/mapping.rb', line 104 def named_mappings @named_mappings end |
Class Method Details
.[](key) ⇒ Object
Access named mappings straight from the class.
95 96 97 |
# File 'lib/mapped-record/mapping.rb', line 95 def [](key) named_mappings[key] end |
.[]=(key, values) ⇒ Object
Assign mappings straight from the class.
100 101 102 |
# File 'lib/mapped-record/mapping.rb', line 100 def []=(key, values) create(key, *values) end |
.all ⇒ Object
Returns all defined mappings.
111 112 113 |
# File 'lib/mapped-record/mapping.rb', line 111 def all named_mappings end |
.blank? ⇒ Boolean
Returns true of no mappings are assigned.
85 86 87 |
# File 'lib/mapped-record/mapping.rb', line 85 def blank? named_mappings.blank? end |
.create(mapping_name, *map_attrs) ⇒ Object
Assigns a named mapping accessible throughout the runtime environment.
Mapping.create :my_mapping, 'Ready', 'Set', 'Go'
The mapping is then accessible from Mapping[:my_mapping]
Given a list of strings, those keys will be mapped automatically to downcase and underscored attributes. (Specify these first).
Configuration options:
:filter
-
Specify a hash of keys and procs to call before assigning to attributes.
attr_mapped 'Date', { :after => { 'Date' => Proc.new { ... } } }
:namespace
-
A prefix string to remove before automatically mapping.
attr_mapped 'PBOne', 'PBTwo', { :namespace => 'PB' }
'key' => :target, 'key2' => :target2, ...
-
As many manual mappings as needed.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mapped-record/mapping.rb', line 22 def create(mapping_name, *map_attrs) raise NameError, "Not creating mapping with nil name" if mapping_name.nil? named_mappings[mapping_name] ||= HashWithIndifferentAccess.new = map_attrs. namespace = nil type = IMPLICIT .each_pair do |key, value| case key when :namespace namespace = value.to_s unless value.to_s.blank? when :filter value.each_pair do |attr, proc| named_mappings[mapping_name][attr.to_s] ||= HashWithIndifferentAccess.new named_mappings[mapping_name][attr.to_s][:filter] = proc end when String, Symbol # deals with explicit mappings raise TargetError.new(mapping_name, key, value, [String, Symbol]) unless value.kind_of?(Symbol) update_mapping(mapping_name, key, value.to_sym, EXPLICIT) end end mapping_temp = HashWithIndifferentAccess.new map_attrs.each do |attr| raise TargetError.new(mapping_name, attr, nil, [Symbol]) unless attr.kind_of?(String) or attr.kind_of?(Symbol) mapped_attr = attr.to_s if namespace raise NamespaceError, "Causes mapping to be ''" if mapped_attr == namespace if mapped_attr.match(/^#{namespace}/) mapped_attr = mapped_attr.sub(/^#{namespace}/, '') type = NAMESPACE end end mapped_attr = mapped_attr.underscore.gsub(' ', '_') update_mapping(mapping_name, attr, mapped_attr.to_sym, type) end named_mappings[mapping_name] end |
.empty? ⇒ Boolean
Returns true of no mappings are assigned.
80 81 82 |
# File 'lib/mapped-record/mapping.rb', line 80 def empty? named_mappings.empty? end |
.has?(mapping_name) ⇒ Boolean
Returns true if mapping of mapping_name
is assigned.
75 76 77 |
# File 'lib/mapped-record/mapping.rb', line 75 def has?(mapping_name) named_mappings.include?(mapping_name) end |
.reset ⇒ Object
Clear all mappings.
90 91 92 |
# File 'lib/mapped-record/mapping.rb', line 90 def reset named_mappings.clear end |
.update_mapping(mapping_name, key, value, type) ⇒ Object
:nodoc:
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mapped-record/mapping.rb', line 115 def update_mapping(mapping_name, key, value, type) # :nodoc: named_mapping = named_mappings[mapping_name] named_mapping[key] ||= HashWithIndifferentAccess.new if named_mapping[key][:to].nil? or type >= named_mapping[key][:type] # check collision collisions = named_mapping.select { |key_name, mapping| key_name.to_s != key.to_s && mapping[:to] == value } raise CollisionError.new(mapping_name, key, value, collisions) unless collisions.blank? # assign mapping named_mapping[key][:to] = value and named_mapping[key][:type] = type end end |