Class: ROM::Relation
- Inherits:
-
Object
- Object
- ROM::Relation
- Includes:
- Enumerable
- Defined in:
- lib/rom/relation.rb,
lib/rom/version.rb
Overview
Enhanced ROM relation wrapping axiom relation and using injected mapper to load/dump tuples/objects
Constant Summary collapse
- VERSION =
'0.1.1'
Class Method Summary collapse
-
.build(relation, mapper) ⇒ Relation
Build a new relation.
Instance Method Summary collapse
-
#delete(object) ⇒ Relation
Delete an object from the relation.
-
#drop(offset) ⇒ Relation
Drop objects from the relation by the given offset.
-
#each {|| ... } ⇒ Relation
Iterate over tuples yielded by the wrapped relation.
-
#first(limit = 1) ⇒ Relation
Take first n-objects from the relation.
-
#inject_mapper(mapper) ⇒ Relation
Inject a new mapper into this relation.
-
#insert(object) ⇒ Relation
(also: #<<)
Insert an object into relation.
-
#last(limit = 1) ⇒ Relation
Take last n-objects from the relation.
-
#one(&block) ⇒ Object
Return exactly one object matching criteria or raise an error.
-
#replace(objects) ⇒ Relation
Replace all objects in the relation with new ones.
-
#restrict(*args, &block) ⇒ Relation
Restrict the relation.
-
#sort_by(*args, &block) ⇒ Relation
Sort the relation by provided attributes.
-
#take(limit) ⇒ Relation
Take objects form the relation with provided limit.
-
#update(object, original_tuple) ⇒ Relation
Update an object.
Class Method Details
.build(relation, mapper) ⇒ Relation
Build a new relation
57 58 59 |
# File 'lib/rom/relation.rb', line 57 def self.build(relation, mapper) new(mapper.call(relation).optimize, mapper) end |
Instance Method Details
#delete(object) ⇒ Relation
Delete an object from the relation
144 145 146 |
# File 'lib/rom/relation.rb', line 144 def delete(object) new(relation.delete([mapper.dump(object)])) end |
#drop(offset) ⇒ Relation
Drop objects from the relation by the given offset
249 250 251 |
# File 'lib/rom/relation.rb', line 249 def drop(offset) new(sorted.drop(offset)) end |
#each {|| ... } ⇒ Relation
Iterate over tuples yielded by the wrapped relation
85 86 87 88 89 |
# File 'lib/rom/relation.rb', line 85 def each return to_enum unless block_given? relation.each { |tuple| yield(mapper.load(tuple)) } self end |
#first(limit = 1) ⇒ Relation
Take first n-objects from the relation
214 215 216 |
# File 'lib/rom/relation.rb', line 214 def first(limit = 1) new(sorted.first(limit)) end |
#inject_mapper(mapper) ⇒ Relation
Inject a new mapper into this relation
313 314 315 |
# File 'lib/rom/relation.rb', line 313 def inject_mapper(mapper) new(relation, mapper) end |
#insert(object) ⇒ Relation Also known as: <<
Insert an object into relation
105 106 107 |
# File 'lib/rom/relation.rb', line 105 def insert(object) new(relation.insert([mapper.dump(object)])) end |
#last(limit = 1) ⇒ Relation
Take last n-objects from the relation
232 233 234 |
# File 'lib/rom/relation.rb', line 232 def last(limit = 1) new(sorted.last(limit)) end |
#one(&block) ⇒ Object
Return exactly one object matching criteria or raise an error
290 291 292 293 294 295 296 297 298 299 |
# File 'lib/rom/relation.rb', line 290 def one(&block) block ||= ->() { raise NoTuplesError } tuples = take(2).to_a if tuples.count > 1 raise ManyTuplesError else tuples.first || block.call end end |
#replace(objects) ⇒ Relation
Replace all objects in the relation with new ones
162 163 164 |
# File 'lib/rom/relation.rb', line 162 def replace(objects) new(relation.replace(objects.map(&mapper.method(:dump)))) end |
#restrict(*args, &block) ⇒ Relation
Restrict the relation
179 180 181 |
# File 'lib/rom/relation.rb', line 179 def restrict(*args, &block) new(relation.restrict(*args, &block)) end |
#sort_by(*args, &block) ⇒ Relation
Sort the relation by provided attributes
266 267 268 |
# File 'lib/rom/relation.rb', line 266 def sort_by(*args, &block) new(relation.sort_by(*args, &block)) end |
#take(limit) ⇒ Relation
Take objects form the relation with provided limit
196 197 198 |
# File 'lib/rom/relation.rb', line 196 def take(limit) new(sorted.take(limit)) end |
#update(object, original_tuple) ⇒ Relation
Update an object
126 127 128 |
# File 'lib/rom/relation.rb', line 126 def update(object, original_tuple) new(relation.delete([original_tuple]).insert([mapper.dump(object)])) end |