lib/appfuel/repository/mapper.rb in appfuel-0.2.3 vs lib/appfuel/repository/mapper.rb in appfuel-0.2.4
- old
+ new
@@ -116,40 +116,74 @@
# @raise [Dry::Contriner::Error] when db_class is not registered
#
# @param entity [String] name of the entity
# @param attr [String] name of the attribute
# @return [Object]
- def storage_class(domain_name, domain_attr, type)
- entry = find(domain_name, attr)
+ def storage_class(entity_name, entity_attr, type)
+ entry = find(entity_name, entity_attr)
+ storage_class_from_entry(entry, type)
+ end
+ def storage_class_from_entry(entry, type)
unless entry.storage?(type)
fail "No (#{type}) storage has been mapped"
end
container_name = entry.container_name
unless container_root_name == container_name
fail "You can not access a mapping outside of this container " +
- "(#{container_root_name}, #{container_name})"
+ "(mapper: #{container_root_name}, entry: #{container_name})"
end
- app_container = Appfuel.app_container(entry.container)
+ app_container = Appfuel.app_container(entry.container_name)
key = entry.storage(type)
app_container[key]
end
- def to_entity_hash(domain_name, data, opts = {})
+ def to_entity_hash(domain_name, data)
entity_attrs = {}
each_entity_attr(domain_name) do |entry|
attr_name = entry.storage_attr
domain_attr = entry.domain_attr
next unless data.key?(attr_name)
-
update_entity_hash(domain_attr, data[attr_name], entity_attrs)
end
entity_attrs
end
+ # Convert the domain into a hash of storage attributes that represent.
+ # Each storage class has its own hash of mapped attributes. A domain
+ # can have more than one storage class.
+ #
+ # @param domain [Appfuel::Domain::Entity]
+ # @param type [Symbol] type of storage :db, :file, :memory etc...
+ # @param opts [Hash]
+ # @option exclued [Array] list of columns to exclude from mapping
+ #
+ # @return [Hash] each key is a storage class with a hash of column
+ # name/value
+ def to_storage(domain, type, opts = {})
+ unless domain.respond_to?(:domain_name)
+ fail "Domain entity must implement :domain_name"
+ end
+
+ excluded = opts[:exclude] || []
+ data = {}
+ each_entity_attr(domain.domain_name) do |entry|
+ unless entry.storage?(type)
+ "storage type (#{type}) is not support for (#{domain.domain_name})"
+ end
+ storage_attr = entry.storage_attr
+ storage_class = entry.storage(type)
+ next if excluded.include?(storage_attr) || entry.skip?
+
+ data[storage_class] = {} unless data.key?(storage_class)
+ data[storage_class][storage_attr] = entity_value(domain, entry)
+ end
+ data
+ end
+
def update_entity_hash(domain_attr, value, hash)
if domain_attr.include?('.')
hash.deep_merge!(create_entity_hash(domain_attr, value))
else
hash[domain_attr] = value
@@ -157,10 +191,10 @@
end
def entity_value(domain, map_entry)
value = resolve_entity_value(domain, map_entry.domain_attr)
if map_entry.computed_attr?
- value = map_entry.compute_attr(domain, value)
+ value = map_entry.computed_attr(value, domain)
end
value = nil if undefined?(value)
value