Class: TableQuery::Query
- Inherits:
-
Object
- Object
- TableQuery::Query
- 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
-
#query_schema ⇒ QuerySchema
readonly
query schema.
-
#table ⇒ Table
readonly
target table.
-
#values ⇒ Array<Object>
readonly
query values.
Instance Method Summary collapse
-
#each ⇒ Object
This is an enumeration method.
-
#initialize(table, query_schema, vals) ⇒ Query
constructor
Create a query interface.
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_schema ⇒ QuerySchema (readonly)
query schema
13 14 15 |
# File 'lib/table-query/query.rb', line 13 def query_schema @query_schema end |
#table ⇒ Table (readonly)
target table
9 10 11 |
# File 'lib/table-query/query.rb', line 9 def table @table end |
#values ⇒ Array<Object> (readonly)
query values
17 18 19 |
# File 'lib/table-query/query.rb', line 17 def values @values end |
Instance Method Details
#each ⇒ Object
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.[: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 |