# File lib/rom/relation/loaded.rb, line 115 def primary_keys pluck(source.primary_key) end
class ROM::Relation::Loaded
Materializes a relation and exposes interface to access the data.
This relation type is returned when a lazy relation is called
@api public
Attributes
Materialized relation
@return [Object]
@api private
Source relation
@return [Relation]
@api private
Public Class Methods
@api private
# File lib/rom/relation/loaded.rb, line 38 def initialize(source, collection = source.to_a) @source = source @collection = collection end
Public Instance Methods
Yield relation tuples
@yield [Hash]
@api public
# File lib/rom/relation/loaded.rb, line 48 def each return to_enum unless block_given? collection.each { |tuple| yield(tuple) } end
Return if loaded relation is empty
@return [TrueClass,FalseClass]
@api public
# File lib/rom/relation/loaded.rb, line 124 def empty? collection.empty? end
Return a loaded relation with a new collection
@api public
# File lib/rom/relation/loaded.rb, line 131 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
# File lib/rom/relation/loaded.rb, line 60 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
# File lib/rom/relation/loaded.rb, line 77 def one! one || raise( TupleCountMismatchError, 'The relation does not contain any tuples' ) end
Return a list of values under provided key
@example
all_users = rom.relations[:users].call all_users.pluck(:name) # ["Jane", "Joe"]
@param [Symbol] key The key name
@return [Array]
@raise KeyError when provided key doesn't exist in any of the tuples
@api public
# File lib/rom/relation/loaded.rb, line 98 def pluck(key) map { |tuple| tuple.fetch(key) } end
Pluck primary key values
This method *may not work* with adapters that don't provide relations that have primary key configured
@example
users = rom.relations[:users].call users.primary_keys # [1, 2, 3]
@return [Array]
@api public