lib/rom/relation.rb in rom-2.0.2 vs lib/rom/relation.rb in rom-3.0.0.beta1
- old
+ new
@@ -1,5 +1,6 @@
+require 'rom/initializer'
require 'rom/relation/class_interface'
require 'rom/pipeline'
require 'rom/mapper_registry'
@@ -27,58 +28,49 @@
# provide extensions for those sub-classes but there is always a vanilla
# relation instance stored in the schema registry.
#
# @api public
class Relation
+ extend Initializer
extend ClassInterface
- include Options
include Dry::Equalizer(:dataset)
include Materializable
include Pipeline
+ # @!attribute [r] dataset
+ # @return [Object] dataset used by the relation provided by relation's gateway
+ # @api public
+ param :dataset
+
# @!attribute [r] mappers
# @return [MapperRegistry] an optional mapper registry (empty by default)
option :mappers, reader: true, default: proc { MapperRegistry.new }
+ # @!attribute [r] schema
+ # @return [Schema] relation schema, defaults to class-level canonical
+ # schema (if it was defined) and sets an empty one as
+ # the fallback
+ # @api public
+ option :schema, reader: true, optional: true, default: -> relation {
+ relation.class.schema || Schema.define(Dry::Core::Inflector.underscore(relation.class.name || EMPTY_STRING).to_sym)
+ }
+
# @!attribute [r] schema_hash
# @return [Object#[]] tuple processing function, uses schema or defaults to Hash[]
# @api private
option :schema_hash, reader: true, default: -> relation {
relation.schema? ? Types::Coercible::Hash.schema(relation.schema.to_h) : Hash
}
- # @!attribute [r] associations
- # @return [AssociationSet] Schema's association set (empty by default)
- option :associations, reader: true, default: -> rel {
- rel.schema? ? rel.schema.associations : Schema::EMPTY_ASSOCIATION_SET
- }
-
- # @!attribute [r] dataset
- # @return [Object] dataset used by the relation provided by relation's gateway
- # @api public
- attr_reader :dataset
-
- # @!attribute [r] schema
- # @return [Schema] returns relation schema object (if defined)
- # @api public
- attr_reader :schema
-
- # Initializes a relation object
+ # Return schema attribute
#
- # @param dataset [Object]
+ # @return [Schema::Type]
#
- # @param options [Hash]
- # @option :mappers [MapperRegistry]
- # @option :schema_hash [#[]]
- # @option :associations [AssociationSet]
- #
# @api public
- def initialize(dataset, options = EMPTY_HASH)
- @dataset = dataset
- @schema = self.class.schema
- super
+ def [](name)
+ schema[name]
end
# Yields relation tuples
#
# @yield [Hash]
@@ -141,29 +133,52 @@
#
# @return [TrueClass, FalseClass]
#
# @api private
def schema?
- ! schema.nil?
+ ! schema.empty?
end
+ # Return a new relation with provided dataset and additional options
+ #
+ # @param [Object] dataset
+ # @param [Hash] new_opts Additional options
+ #
+ # @api public
+ def new(dataset, new_opts = EMPTY_HASH)
+ self.class.new(dataset, new_opts.empty? ? options : options.merge(new_opts))
+ end
+
# Returns a new instance with the same dataset but new options
#
# @param new_options [Hash]
#
# @return [Relation]
#
# @api private
def with(new_options)
- __new__(dataset, options.merge(new_options))
+ new(dataset, options.merge(new_options))
end
- private
+ # Return all registered relation schemas
+ #
+ # @return [Hash<Symbol=>Schema>]
+ #
+ # @api public
+ def schemas
+ @schemas ||= self.class.schemas
+ end
- # @api private
- def __new__(dataset, new_opts = EMPTY_HASH)
- self.class.new(dataset, options.merge(new_opts))
+ # Return schema's association set (empty by default)
+ #
+ # @return [AssociationSet] Schema's association set (empty by default)
+ #
+ # @api public
+ def associations
+ @associations ||= schema.associations
end
+
+ private
# @api private
def composite_class
Relation::Composite
end