Sha256: c9d7156683a52493d40ef4e6585f7dde8661264e5e1710007135c30bb53c25e9

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require 'rom/relation/class_interface'

require 'rom/relation/lazy'
require 'rom/relation/curried'

module ROM
  # Base relation class
  #
  # Relation is a proxy for the dataset object provided by the gateway. It
  # forwards every method to the dataset, which is why the "native" interface of
  # the underlying gateway is available in the relation. This interface,
  # however, is considered private and should not be used outside of the
  # relation instance.
  #
  # ROM builds sub-classes of this class for every relation defined in the env
  # for easy inspection and extensibility - every gateway can provide extensions
  # for those sub-classes but there is always a vanilla relation instance stored
  # in the schema registry.
  #
  # @api public
  class Relation
    extend ClassInterface

    include Options
    include Equalizer.new(:dataset)

    # Dataset used by the relation
    #
    # This object is provided by the gateway during the setup
    #
    # @return [Object]
    #
    # @api private
    attr_reader :dataset

    # @api private
    def initialize(dataset, options = {})
      @dataset = dataset
      super
    end

    # Yield dataset tuples
    #
    # @yield [Hash]
    #
    # @api private
    def each(&block)
      return to_enum unless block
      dataset.each { |tuple| yield(tuple) }
    end

    # Materialize a relation into an array
    #
    # @return [Array<Hash>]
    #
    # @api public
    def to_a
      to_enum.to_a
    end

    # Turn relation into a lazy-loadable and composable relation
    #
    # @see Lazy
    #
    # @return [Lazy]
    #
    # @api public
    def to_lazy(*args)
      Lazy.new(self, *args)
    end

    private

    # @api private
    def __new__(dataset, new_opts = {})
      self.class.new(dataset, options.merge(new_opts))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-0.8.1 lib/rom/relation.rb
rom-0.8.0 lib/rom/relation.rb