class Upl::Query

Mostly sugar TODO needs a row_proc like Sequel uses to generate the yielded values TODO maybe cache @count (after first enumeration)

Attributes

source[R]
term[R]
vars[R]

Public Class Methods

new(term_or_string, &map_blk) click to toggle source

TODO can only be string at this point One-use only. If you want a new query, create another instance. Is an enumerable for the result set.

# File lib/upl/query.rb, line 9
def initialize term_or_string, &map_blk
  @source = term_or_string
  @term, @vars = Upl::Runtime.term_vars term_or_string
  @map_blk = map_blk
end

Public Instance Methods

[](name;) click to toggle source
# File lib/upl/query.rb, line 36
def [] name; @vars[name] end
[]=(name, value) click to toggle source
# File lib/upl/query.rb, line 38
def []= name, value
  @vars[name] === value or raise "Unification failure"
end
call() click to toggle source
# File lib/upl/query.rb, line 42
def call
  @results ||= Upl::Runtime.query @term, @vars
end
each(&blk) click to toggle source
# File lib/upl/query.rb, line 46
def each &blk
  call.map do |row|
    # TODO this assume the if statement is faster than a call to a default ->i{i}
    if @map_blk
      blk.call @map_blk[row]
    else
      blk.call row
    end
  end
end
method_missing(meth, *args, &blk) click to toggle source
Calls superclass method
# File lib/upl/query.rb, line 19
def method_missing meth, *args, &blk
  if meth.end_with? '='
    assign = true
    name = meth[..-2].to_sym
  else
    name = meth
  end

  return super unless @vars.include? name

  if assign
    @vars[name] === args.first or raise "Unification failure"
  else
    @vars[name]
  end
end
names() click to toggle source
# File lib/upl/query.rb, line 17
def names; @vars.keys end