Class: ROM::Relation::Loaded
- Inherits:
-
Object
- Object
- ROM::Relation::Loaded
- Includes:
- Enumerable
- Defined in:
- core/lib/rom/relation/loaded.rb
Overview
Materializes a relation and exposes interface to access the data.
This relation type is returned when a lazy relation is called
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
private
Materialized relation.
-
#source ⇒ Relation
readonly
private
Source relation.
Instance Method Summary collapse
-
#each {|Hash| ... } ⇒ Object
Yield relation tuples.
-
#empty? ⇒ TrueClass, FalseClass
Return if loaded relation is empty.
-
#initialize(source, collection = source.to_a) ⇒ Loaded
constructor
private
A new instance of Loaded.
-
#new(collection) ⇒ Object
Return a loaded relation with a new collection.
-
#one ⇒ Object
Returns a single tuple from the relation if there is one.
-
#one! ⇒ Object
Like [one], but additionally raises an error if the relation is empty.
-
#pluck(key) ⇒ Array
Return a list of values under provided key.
-
#primary_keys ⇒ Array
Pluck primary key values.
Constructor Details
#initialize(source, collection = source.to_a) ⇒ Loaded
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Loaded.
36 37 38 39 |
# File 'core/lib/rom/relation/loaded.rb', line 36 def initialize(source, collection = source.to_a) @source = source @collection = collection end |
Instance Attribute Details
#collection ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Materialized relation
33 34 35 |
# File 'core/lib/rom/relation/loaded.rb', line 33 def collection @collection end |
#source ⇒ Relation (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Source relation
26 27 28 |
# File 'core/lib/rom/relation/loaded.rb', line 26 def source @source end |
Instance Method Details
#each {|Hash| ... } ⇒ Object
Yield relation tuples
46 47 48 49 50 |
# File 'core/lib/rom/relation/loaded.rb', line 46 def each return to_enum unless block_given? collection.each { |tuple| yield(tuple) } end |
#empty? ⇒ TrueClass, FalseClass
Return if loaded relation is empty
122 123 124 |
# File 'core/lib/rom/relation/loaded.rb', line 122 def empty? collection.empty? end |
#new(collection) ⇒ Object
Return a loaded relation with a new collection
129 130 131 |
# File 'core/lib/rom/relation/loaded.rb', line 129 def new(collection) self.class.new(source, collection) end |
#one ⇒ Object
Returns a single tuple from the relation if there is one.
58 59 60 61 62 63 64 65 66 67 |
# File 'core/lib/rom/relation/loaded.rb', line 58 def one if collection.count > 1 raise( TupleCountMismatchError, 'The relation consists of more than one tuple' ) else collection.first end end |
#one! ⇒ Object
Like [one], but additionally raises an error if the relation is empty.
75 76 77 78 79 80 |
# File 'core/lib/rom/relation/loaded.rb', line 75 def one! one || raise( TupleCountMismatchError, 'The relation does not contain any tuples' ) end |
#pluck(key) ⇒ Array
Return a list of values under provided key
96 97 98 |
# File 'core/lib/rom/relation/loaded.rb', line 96 def pluck(key) map { |tuple| tuple.fetch(key) } end |
#primary_keys ⇒ Array
Pluck primary key values
This method may not work with adapters that don’t provide relations that have primary key configured
113 114 115 |
# File 'core/lib/rom/relation/loaded.rb', line 113 def primary_keys pluck(source.primary_key) end |