lib/rom/relation.rb
# Return exactly one object matching criteria or raise an error
#
# @example
#   axiom    = Axiom::Relation.new([[:id, Integer]], [1]])
#   relation = ROM::Relation.new(axiom, mapper)
#
#   relation.one.to_a # => {id: 1}
#
# @param [Proc] block
#   optional block to call in case no tuple is returned
#
# @return [Object]
#
# @raise NoTuplesError
#   if no tuples were returned
#
# @raise ManyTuplesError
#   if more than one tuple was returned
#
# @api public
Return exactly one object matching criteria or raise an error
Return exactly one object matching criteria or raise an error
def one(&block)
      block  ||= ->() { raise NoTuplesError }
      tuples   = take(2).to_a
      if tuples.count > 1
        raise ManyTuplesError
      else
        tuples.first || block.call
      end
    end
def one ( & block ) block ||= -> ( ) { raise NoTuplesError } tuples = take ( 2 ) . to_a if tuples . count > 1 raise ManyTuplesError else tuples . first || block . call end end
Return exactly one object matching criteria or raise an error
Return exactly one object matching criteria or raise an error
None
Object
NoTuplesError if no tuples were returned
NoTuplesError if no tuples were returned
ManyTuplesError if more than one tuple was returned
ManyTuplesError if more than one tuple was returned
block
block optional block to call in case no tuple is returned
block optional block to call in case no tuple is returned
example
axiom = Axiom::Relation.new([[:id, Integer]], [1]]) relation = ROM::Relation.new(axiom, mapper) relation.one.to_a # => {id: 1}
axiom = Axiom :: Relation . new ( [[ : id Integer ]] [ 1 ]] ) relation = ROM :: Relation . new ( axiom mapper ) relation . one . to_a # = > { id : 1 }
api
None