class Arpa::DataMappers::Base
Private Class Methods
_attributes_to_map()
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 103 def self._attributes_to_map @attributes_to_map end
_attrs_to_entity()
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 111 def self._attrs_to_entity @attrs_to_entity end
_attrs_to_record()
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 107 def self._attrs_to_record @attrs_to_record end
_entity_class()
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 79 def self._entity_class @entity_class.constantize end
_repository_class()
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 87 def self._repository_class @repository_class.constantize end
attributes_to_map(*attrs)
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 91 def self.attributes_to_map(*attrs) @attributes_to_map = attrs end
attrs_to_entity(*attrs)
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 99 def self.attrs_to_entity(*attrs) @attrs_to_entity = attrs end
attrs_to_record(*attrs)
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 95 def self.attrs_to_record(*attrs) @attrs_to_record = attrs end
entity_class(entity_class)
click to toggle source
— Classes Methods —
# File lib/arpa/data_mappers/base.rb, line 75 def self.entity_class(entity_class) @entity_class = entity_class end
repository_class(repository_class)
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 83 def self.repository_class(repository_class) @repository_class = repository_class end
Public Instance Methods
map_to_entity(record, options = {})
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 13 def map_to_entity(record, options = {}) options[:map_association_to] ||= :map_to_entity attrs_to_entity = self.class._attrs_to_entity attributes = attributes_from(record, attrs_to_entity, options) self.class._entity_class.new(attributes) end
map_to_record(entity, options = {})
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 6 def map_to_record(entity, options = {}) options[:map_association_to] ||= :map_to_record attrs_to_record = self.class._attrs_to_record attributes = attributes_from(entity, attrs_to_record, options) self.class._repository_class.new(attributes) end
Private Instance Methods
association_value(object, attr_key, options)
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 56 def association_value(object, attr_key, options) key = attr_key.keys.first object_value = object.send(key) return object_value unless options[:map_association] mapper = attr_key[key][:mapper].constantize.instance if object_value.try(:size) object_value.collect do |obj| mapper.send(options[:map_association_to], obj, map_association: false) end else mapper.send(options[:map_association_to], object_value, map_association: false) end end
attributes_from(object, attrs_to_map, options = {})
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 22 def attributes_from(object, attrs_to_map, options = {}) options[:map_association_to] = options.fetch(:map_association_to, :map_to_entity) options[:map_association] = options.fetch(:map_association, true) attrs_to_map ||= self.class._attributes_to_map begin build_hash_attributes(attrs_to_map, object, options) rescue StandardError => e raise StandardError, "#{self.class} -> #{e.message}" end end
build_hash_attribute()
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 43 def build_hash_attribute lambda { |object, options, attr_key| if attr_key.is_a?(Hash) key = attr_key.keys.first value = association_value(object, attr_key, options) else key = attr_key value = object.send(attr_key) if object.respond_to?(attr_key) end { :"#{key}" => value } } end
build_hash_attributes(attributes_to_map, object, options)
click to toggle source
# File lib/arpa/data_mappers/base.rb, line 35 def build_hash_attributes(attributes_to_map, object, options) builder_attribute = build_hash_attribute.curry.call(object, options) attributes_to_map.collect do |attr_key| builder_attribute.call(attr_key) end.reduce({}, :merge) end