Class: Upl::Query

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/upl/query.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term_or_string, &map_blk) ⇒ Query

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.



9
10
11
12
13
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



15
16
17
# File 'lib/upl/query.rb', line 15

def source
  @source
end

#termObject (readonly)

Returns the value of attribute term.



15
16
17
# File 'lib/upl/query.rb', line 15

def term
  @term
end

#varsObject (readonly)

Returns the value of attribute vars.



15
16
17
# File 'lib/upl/query.rb', line 15

def vars
  @vars
end

Instance Method Details

#[](name) ⇒ Object



36
# File 'lib/upl/query.rb', line 36

def [] name; @vars[name] end

#[]=(name, value) ⇒ Object



38
39
40
# File 'lib/upl/query.rb', line 38

def []= name, value
  @vars[name] === value or raise "Unification failure"
end

#callObject



42
43
44
# File 'lib/upl/query.rb', line 42

def call
  @results ||= Upl::Runtime.term_vars_query @term, @vars
end

#each(&blk) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# 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

#namesObject



17
# File 'lib/upl/query.rb', line 17

def names; @vars.keys end