class Heliodor::Query
Query
which will be executed when finish
is called @attr db [Heliodor::DB] Query's database @attr table [String] Name of current table @attr actions [Array<Hash>] Array of all actions
Attributes
Public Class Methods
Initializer
# File lib/heliodor/query.rb, line 9 def initialize(db, table, dat) @db = db @table = table @full = dat @dat = @full[@table] @actions = [] end
Public Instance Methods
Creates table with current name. If it already exists - truncates @return [self]
# File lib/heliodor/query.rb, line 32 def create actions << { 'type' => 'create' } self end
Deletes matching items @return [self]
# File lib/heliodor/query.rb, line 130 def delete(dat = {}) actions << { 'type' => 'delete', 'dat' => dat } self end
If there's no table with current name, creates it @return [self]
# File lib/heliodor/query.rb, line 41 def ensure actions << { 'type' => 'ensure' } self end
Filters all items in current table using given block @yield [i] Is executed for each item in table.
Drops values when this block returns false value
@return [self]
# File lib/heliodor/query.rb, line 52 def filter(&block) actions << { 'type' => 'filter', 'block' => block } self end
Ends query and processes it @return [Array] Data in table
# File lib/heliodor/query.rb, line 140 def finish _process @dat end
Insert given data into table @param dat [Any] @return [self]
# File lib/heliodor/query.rb, line 22 def insert(*dat) actions << { 'type' => 'insert', 'data' => dat } self end
Inspect method
# 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
Edits all items in current table @yield [i] Return updated value to update stuff @example Adding key to each hash in table
db.query('table_name').map do |i| i[:keyname] = nil end.write.finish
@example Map-reduce method of getting table length
db.query('table_name').map{1}.reduce(&:+).finish # => length
@return [self]
# File lib/heliodor/query.rb, line 69 def map(&block) actions << { 'type' => 'map', 'block' => block } self end
Reduces table to single element using given block @return [self]
# File lib/heliodor/query.rb, line 79 def reduce(&block) actions << { 'type' => 'reduce', 'block' => block } self end
Renames current table @param to [String] @return [self]
# File lib/heliodor/query.rb, line 90 def rename(to) actions << { 'type' => 'rename', 'to' => to } self end
Selects all values which match given hash @return [self]
# File lib/heliodor/query.rb, line 109 def select(dat = {}) actions << { 'type' => 'select', 'dat' => dat } self end
Updates all items which match first hash by merging them with second hash @return [self]
# File lib/heliodor/query.rb, line 119 def update(dat1 = {}, dat2 = {}) actions << { 'type' => 'update', 'dat1' => dat1, 'dat2' => dat2 } self end
Writes changes to database @return [self]
# File lib/heliodor/query.rb, line 100 def write actions << { 'type' => 'write' } self end
Private Instance Methods
# File lib/heliodor/query_internal.rb, line 53 def _delete(dat) d = @dat @dat = d.map do |i| if dat.class == Hash if i >= dat nil else i end else if i == dat nil else i end end end @dat.delete(nil) @dat end
# File lib/heliodor/query_internal.rb, line 8 def _filter(&_block) dat = [] @dat.each do |item| dat << item if yield(item) end @dat = dat end
# File lib/heliodor/query_internal.rb, line 4 def _gen @dat = [] end
# File lib/heliodor/query_internal.rb, line 40 def _insert(dat) dat.each do |d| @dat << d end end
# File lib/heliodor/query_internal.rb, line 30 def _map(&_block) @dat.each_with_index do |v, k| @dat[k] = yield(v) end end
# File lib/heliodor/query_internal.rb, line 79 def _process actions.each do |action| case action['type'] when 'create' _gen when 'ensure' _gen unless @dat when 'filter' _filter(&action['block']) when 'select' _select(action['dat']) when 'update' _update(action['dat1'], action['dat2']) when 'map' _map(&action['block']) when 'reduce' _reduce(&action['block']) when 'insert' _insert(action['data']) when 'rename' _rename(action['to']) when 'delete' _delete(action['dat']) when 'write' _write else raise NotImplementedError, "Action `#{action['type']}` is not yet implemented!" end end end
# File lib/heliodor/query_internal.rb, line 36 def _reduce(&_block) @dat = [@dat.reduce(&block)] end
# File lib/heliodor/query_internal.rb, line 46 def _rename(to) @full[to] = @dat @full.delete(@table) @table = to @dat = @full[@table] end
# File lib/heliodor/query_internal.rb, line 16 def _select(dat = {}) out = [] @dat.each do |item| out << item if item.class.ancestors.include?(Hash) && item >= dat end @dat = out end
# File lib/heliodor/query_internal.rb, line 24 def _update(dat1 = {}, dat2 = {}) @dat.each_with_index do |v, k| @dat[k] = v.merge(dat2) if v.class.ancestors.include?(Hash) && v >= dat1 end end
# File lib/heliodor/query_internal.rb, line 74 def _write @full[@table] = @dat @db.write(@full) end