lib/rom/repository/relation_proxy.rb in rom-repository-0.3.1 vs lib/rom/repository/relation_proxy.rb in rom-repository-1.0.0.beta1
- old
+ new
@@ -1,6 +1,6 @@
-require 'rom/support/options'
+require 'rom/initializer'
require 'rom/relation/materializable'
require 'rom/repository/relation_proxy/combine'
require 'rom/repository/relation_proxy/wrap'
@@ -12,34 +12,34 @@
# Relation proxies are being registered within repositories so typically there's
# no need to instantiate them manually.
#
# @api public
class RelationProxy
- include Options
+ extend Initializer
include Relation::Materializable
include RelationProxy::Combine
include RelationProxy::Wrap
- option :name, type: Symbol
- option :mappers, reader: true, default: proc { MapperBuilder.new }
- option :meta, reader: true, type: Hash, default: EMPTY_HASH
- option :registry, type: RelationRegistry, default: proc { RelationRegistry.new }, reader: true
+ RelationRegistryType = Types.Definition(RelationRegistry).constrained(type: RelationRegistry)
# @!attribute [r] relation
# @return [Relation, Relation::Composite, Relation::Graph, Relation::Curried] The decorated relation object
- attr_reader :relation
+ param :relation
- # @!attribute [r] name
- # @return [ROM::Relation::Name] The relation name object
- attr_reader :name
+ option :name, type: Types::Strict::Symbol
+ option :mappers, reader: true, default: proc { MapperBuilder.new }
+ option :meta, reader: true, default: proc { EMPTY_HASH }
+ option :registry, type: RelationRegistryType, default: proc { RelationRegistry.new }, reader: true
- # @api private
- def initialize(relation, options = {})
- super
- @relation = relation
- @name = relation.name.with(options[:name])
+ # Relation name
+ #
+ # @return [ROM::Relation::Name]
+ #
+ # @api public
+ def name
+ @name == Dry::Initializer::UNDEFINED ? relation.name : relation.name.with(@name)
end
# Materializes wrapped relation and sends it through a mapper
#
# For performance reasons a combined relation will skip mapping since
@@ -103,19 +103,10 @@
# @api private
def composite?
relation.is_a?(Relation::Composite)
end
- # Returns meta info for the wrapped relation
- #
- # @return [Hash]
- #
- # @api private
- def meta
- options[:meta]
- end
-
# @return [Symbol] The wrapped relation's adapter identifier ie :sql or :http
#
# @api private
def adapter
relation.class.adapter
@@ -127,15 +118,13 @@
#
# @api private
def to_ast
@to_ast ||=
begin
- attr_ast = (attributes - wraps_attributes).map { |name|
- [:attribute, name]
- }
+ attr_ast = schema.map { |attr| [:attribute, attr] }
- meta = options[:meta].merge(dataset: base_name.dataset)
+ meta = self.meta.merge(dataset: base_name.dataset)
meta.delete(:wraps)
header = attr_ast + nodes_ast + wraps_ast
[:relation, [base_name.relation, meta, [:header, header]]]
@@ -145,22 +134,24 @@
# @api private
def respond_to_missing?(meth, _include_private = false)
relation.respond_to?(meth) || super
end
+ # @api public
+ def inspect
+ %(#<#{relation.class} name=#{name} dataset=#{dataset.inspect}>)
+ end
+
private
# @api private
- def base_name
- relation.base_name
+ def schema
+ meta[:wrap] ? relation.schema.wrap.qualified : relation.schema.reject(&:wrapped?)
end
# @api private
- def wraps_attributes
- @wrap_attributes ||= wraps.flat_map { |wrap|
- prefix = wrap.base_name.dataset
- wrap.attributes.map { |name| :"#{prefix}_#{name}" }
- }
+ def base_name
+ relation.base_name
end
# @api private
def nodes_ast
@nodes_ast ||= nodes.map(&:to_ast)