Class: TableQuery::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/table-query/query.rb

Overview

Query is a class for providing query interfaces. This is enumerable, so you can access results by various enumeration methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, query_schema, vals) ⇒ Query

Create a query interface.



20
21
22
23
24
# File 'lib/table-query/query.rb', line 20

def initialize(table, query_schema, vals)
  @table = table
  @query_schema = query_schema
  @values = vals
end

Instance Attribute Details

#query_schemaQuerySchema (readonly)

query schema

Returns:



13
14
15
# File 'lib/table-query/query.rb', line 13

def query_schema
  @query_schema
end

#tableTable (readonly)

target table

Returns:



9
10
11
# File 'lib/table-query/query.rb', line 9

def table
  @table
end

#valuesArray<Object> (readonly)

query values

Returns:

  • (Array<Object>)


17
18
19
# File 'lib/table-query/query.rb', line 17

def values
  @values
end

Instance Method Details

#eachObject

This is an enumeration method. If block is not given, then return an Enumerator object.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/table-query/query.rb', line 28

def each
  if block_given?
    vals = []
    @query_schema.inputs.each_with_index do |input, i|
      vals << input.type.convert_from_ruby(@values[i])
    end
    @table.csv.each do |row|
      if @query_schema.inputs.zip(vals).all? {|input, val| row[input.pos] == val} and
          @query_schema.values.all? {|value| row[value.pos] == value.type.convert_from_ruby(value.value)}
        outputs = []
        cond = @query_schema.outputs.each do |output|
          val = row[output.pos]
          if output.options[:empty] == :ignore and val == ""
            break :break
          else
            outputs << output.type.convert_to_ruby(val)
          end
        end
        yield *outputs unless cond == :break
      end
    end
  else
    return Enumerator.new(self)
  end
end