Sha256: b6e0e474e2a4567c0c28af9d13a40cfb338bb8ccfc0e28a6b817171c65337560

Contents?: true

Size: 1.78 KB

Versions: 18

Compression:

Stored size: 1.78 KB

Contents

module ROM
  class Relation
    # Materializes a relation and exposes interface to access the data
    #
    # @api public
    class Loaded
      include Enumerable

      # Coerce loaded relation to an array
      #
      # @return [Array]
      #
      # @api public
      alias_method :to_ary, :to_a

      # Source relation
      #
      # @return [Relation]
      #
      # @api private
      attr_reader :source

      # Materialized relation
      #
      # @return [Object]
      #
      # @api private
      attr_reader :collection

      # @api private
      def initialize(source, collection = source.to_a)
        @source = source
        @collection = collection
      end

      # Yield relation tuples
      #
      # @yield [Hash]
      #
      # @api public
      def each(&block)
        return to_enum unless block
        collection.each { |object| yield(object) }
      end

      # @api public
      def new(collection)
        self.class.new(source, collection)
      end

      # Returns a single tuple from the relation if there is one.
      #
      # @raise [ROM::TupleCountMismatchError] if the relation contains more than
      #   one tuple
      #
      # @api public
      def one
        if collection.count > 1
          raise(
            TupleCountMismatchError,
            'The relation consists of more than one tuple'
          )
        else
          collection.first
        end
      end

      # Like [one], but additionally raises an error if the relation is empty.
      #
      # @raise [ROM::TupleCountMismatchError] if the relation does not contain
      #   exactly one tuple
      #
      # @api public
      def one!
        one || raise(
          TupleCountMismatchError,
          'The relation does not contain any tuples'
        )
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rom-1.0.0 lib/rom/relation/loaded.rb
rom-1.0.0.rc1 lib/rom/relation/loaded.rb
rom-1.0.0.beta2 lib/rom/relation/loaded.rb
rom-1.0.0.beta1 lib/rom/relation/loaded.rb
rom-0.9.1 lib/rom/relation/loaded.rb
rom-0.9.0 lib/rom/relation/loaded.rb
rom-0.9.0.rc1 lib/rom/relation/loaded.rb
rom-0.9.0.beta1 lib/rom/relation/loaded.rb
rom-0.8.1 lib/rom/relation/loaded.rb
rom-0.8.0 lib/rom/relation/loaded.rb
rom-0.7.1 lib/rom/relation/loaded.rb
rom-0.7.0 lib/rom/relation/loaded.rb
rom-0.6.2 lib/rom/relation/loaded.rb
rom-0.6.1 lib/rom/relation/loaded.rb
rom-0.6.0 lib/rom/relation/loaded.rb
rom-0.6.0.rc1 lib/rom/relation/loaded.rb
rom-0.6.0.beta3 lib/rom/relation/loaded.rb
rom-0.6.0.beta2 lib/rom/relation/loaded.rb