module Axiom::Algebra::Restriction::Methods

Public Instance Methods

restrict(*args, &block) click to toggle source

Return a relation with restricted tuples

@example restriction with a predicate

restriction = relation.restrict(predicate)

@example restriction using a block

restriction = relation.restrict do |context|
  context.a.eq('other').and(context.b.gte(42))
end

@example restriction using a Hash

restriction = relation.restrict(id: 1)

@example restriction using an Array

restriction = relation.restrict([[:id, 1]])

@param [Array] args

optional arguments

@yield [context]

optional block to restrict the tuples with

@yieldparam [Evaluator::Context] context

the context to evaluate the restriction with

@yieldreturn [Function, call]

predicate to restrict the tuples with

@return [Restriction]

@api public

# File lib/axiom/algebra/restriction.rb, line 124
def restrict(*args, &block)
  Restriction.new(self, coerce_to_predicate(*args, &block))
end

Private Instance Methods

coerce_to_predicate(predicate = Undefined) { |context| ... } click to toggle source

Coerce the arguments and block into a predicate

@param [Function, call] predicate

optional predicate

@yield [relation]

optional block to restrict the tuples with

@yieldparam [Evaluator::Context] context

the context to evaluate the restriction with

@yieldreturn [Function, call]

predicate to restrict the tuples with

@return [Function, call]

@api private

# File lib/axiom/algebra/restriction.rb, line 147
def coerce_to_predicate(predicate = Undefined)
  case predicate
  when Undefined
    Evaluator::Context.new(header) { |context| yield context }.yield
  when Enumerable
    predicate.reduce(TAUTOLOGY) do |entry, (name, value)|
      entry.and(header[name].eq(value))
    end
  else
    predicate
  end
end