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

actions[RW]
db[RW]
table[RW]

Public Class Methods

new(db, table, dat) click to toggle source

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

create() click to toggle source

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
delete(dat = {}) click to toggle source

Deletes matching items @return [self]

# File lib/heliodor/query.rb, line 130
def delete(dat = {})
  actions << {
    'type' => 'delete',
    'dat'  => dat
  }
  self
end
ensure() click to toggle source

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
filter(&block) click to toggle source

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
finish() click to toggle source

Ends query and processes it @return [Array] Data in table

# File lib/heliodor/query.rb, line 140
def finish
  _process
  @dat
end
insert(*dat) click to toggle source

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() click to toggle source

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
map(&block) click to toggle source

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
reduce(&block) click to toggle source

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
rename(to) click to toggle source

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
select(dat = {}) click to toggle source

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
update(dat1 = {}, dat2 = {}) click to toggle source

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
write() click to toggle source

Writes changes to database @return [self]

# File lib/heliodor/query.rb, line 100
def write
  actions << {
    'type' => 'write'
  }
  self
end

Private Instance Methods

_delete(dat) click to toggle source
# 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
_filter() { |item| ... } click to toggle source
# 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
_gen() click to toggle source
# File lib/heliodor/query_internal.rb, line 4
def _gen
  @dat = []
end
_insert(dat) click to toggle source
# File lib/heliodor/query_internal.rb, line 40
def _insert(dat)
  dat.each do |d|
    @dat << d
  end
end
_map() { |v| ... } click to toggle source
# File lib/heliodor/query_internal.rb, line 30
def _map(&_block)
  @dat.each_with_index do |v, k|
    @dat[k] = yield(v)
  end
end
_process() click to toggle source
# 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
_reduce(&_block) click to toggle source
# File lib/heliodor/query_internal.rb, line 36
def _reduce(&_block)
  @dat = [@dat.reduce(&block)]
end
_rename(to) click to toggle source
# File lib/heliodor/query_internal.rb, line 46
def _rename(to)
  @full[to] = @dat
  @full.delete(@table)
  @table = to
  @dat = @full[@table]
end
_select(dat = {}) click to toggle source
# 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
_update(dat1 = {}, dat2 = {}) click to toggle source
# 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
_write() click to toggle source
# File lib/heliodor/query_internal.rb, line 74
def _write
  @full[@table] = @dat
  @db.write(@full)
end