Class: Heliodor::Query
- Inherits:
-
Object
- Object
- Heliodor::Query
- Defined in:
- lib/heliodor/query.rb,
lib/heliodor/query_internal.rb
Overview
Query which will be executed when #finish is called
Instance Attribute Summary collapse
-
#actions ⇒ Array<Hash>
Array of all actions.
-
#db ⇒ Heliodor::DB
Query’s database.
-
#table ⇒ String
Name of current table.
Query Language collapse
-
#create ⇒ self
Creates table with current name.
-
#delete(dat = {}) ⇒ self
Deletes matching items.
-
#ensure ⇒ self
If there’s no table with current name, creates it.
-
#filter {|i| ... } ⇒ self
Filters all items in current table using given block.
-
#finish ⇒ Array
Ends query and processes it.
-
#insert(*dat) ⇒ self
Insert given data into table.
-
#map {|i| ... } ⇒ self
Edits all items in current table.
-
#reduce(&block) ⇒ self
Reduces table to single element using given block.
-
#rename(to) ⇒ self
Renames current table.
-
#select(dat = {}) ⇒ self
Selects all values which match given hash.
-
#update(dat1 = {}, dat2 = {}) ⇒ self
Updates all items which match first hash by merging them with second hash.
-
#write ⇒ self
Writes changes to database.
Instance Method Summary collapse
-
#initialize(db, table, dat) ⇒ Query
constructor
Initializer.
-
#inspect ⇒ Object
Inspect method.
Constructor Details
#initialize(db, table, dat) ⇒ Query
Initializer
9 10 11 12 13 14 15 |
# File 'lib/heliodor/query.rb', line 9 def initialize(db, table, dat) @db = db @table = table @full = dat @dat = @full[@table] @actions = [] end |
Instance Attribute Details
#actions ⇒ Array<Hash>
Array of all actions
5 6 7 |
# File 'lib/heliodor/query.rb', line 5 def actions @actions end |
#table ⇒ String
Name of current table
5 6 7 |
# File 'lib/heliodor/query.rb', line 5 def table @table end |
Instance Method Details
#create ⇒ self
Creates table with current name. If it already exists - truncates
32 33 34 35 36 37 |
# File 'lib/heliodor/query.rb', line 32 def create actions << { 'type' => 'create' } self end |
#delete(dat = {}) ⇒ self
Deletes matching items
130 131 132 133 134 135 136 |
# File 'lib/heliodor/query.rb', line 130 def delete(dat = {}) actions << { 'type' => 'delete', 'dat' => dat } self end |
#ensure ⇒ self
If there’s no table with current name, creates it
41 42 43 44 45 46 |
# File 'lib/heliodor/query.rb', line 41 def ensure actions << { 'type' => 'ensure' } self end |
#filter {|i| ... } ⇒ self
Filters all items in current table using given block
52 53 54 55 56 57 58 |
# File 'lib/heliodor/query.rb', line 52 def filter(&block) actions << { 'type' => 'filter', 'block' => block } self end |
#finish ⇒ Array
Ends query and processes it
140 141 142 143 |
# File 'lib/heliodor/query.rb', line 140 def finish _process @dat end |
#insert(*dat) ⇒ self
Insert given data into table
22 23 24 25 26 27 28 |
# File 'lib/heliodor/query.rb', line 22 def insert(*dat) actions << { 'type' => 'insert', 'data' => dat } self end |
#inspect ⇒ Object
Inspect method
147 148 149 150 |
# File 'lib/heliodor/query.rb', line 147 def inspect %(#<Heliodor::Query:#{object_id.to_s(16)} @table='#{@table}' @db=#{@db}) + %( @actions.length=#{@actions.length}>) end |
#map {|i| ... } ⇒ self
Edits all items in current table
69 70 71 72 73 74 75 |
# File 'lib/heliodor/query.rb', line 69 def map(&block) actions << { 'type' => 'map', 'block' => block } self end |
#reduce(&block) ⇒ self
Reduces table to single element using given block
79 80 81 82 83 84 85 |
# File 'lib/heliodor/query.rb', line 79 def reduce(&block) actions << { 'type' => 'reduce', 'block' => block } self end |
#rename(to) ⇒ self
Renames current table
90 91 92 93 94 95 96 |
# File 'lib/heliodor/query.rb', line 90 def rename(to) actions << { 'type' => 'rename', 'to' => to } self end |
#select(dat = {}) ⇒ self
Selects all values which match given hash
109 110 111 112 113 114 115 |
# File 'lib/heliodor/query.rb', line 109 def select(dat = {}) actions << { 'type' => 'select', 'dat' => dat } self end |
#update(dat1 = {}, dat2 = {}) ⇒ self
Updates all items which match first hash by merging them with second hash
119 120 121 122 123 124 125 126 |
# File 'lib/heliodor/query.rb', line 119 def update(dat1 = {}, dat2 = {}) actions << { 'type' => 'update', 'dat1' => dat1, 'dat2' => dat2 } self end |
#write ⇒ self
Writes changes to database
100 101 102 103 104 105 |
# File 'lib/heliodor/query.rb', line 100 def write actions << { 'type' => 'write' } self end |