Sha256: 7fc593efcd5698f6a350ebc5279219be696d22b355e6b93479af9a3914787881

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

class Heliodor::Query
  private

  def _gen
    @dat = []
  end

  def _filter(&_block)
    dat = []
    @dat.each do |item|
      dat << item if yield(item)
    end
    @dat = dat
  end

  def _select(dat = {})
    out = []
    @dat.each do |item|
      out << item if (v.class == Hash) && (item >= dat)
    end
    @dat = out
  end

  def _update(dat1 = {}, dat2 = {})
    @dat.each_with_index do |v, k|
      @dat[k] = v.merge(dat2) if (v.class == Hash) && (v >= dat1)
    end
  end

  def _map(&_block)
    @dat.each_with_index do |v, k|
      @dat[k] = yield(v)
    end
  end

  def _reduce(&_block)
    @dat = [@dat.reduce(&block)]
  end

  def _insert(dat)
    dat.each do |d|
      @dat << d
    end
  end

  def _rename(to)
    @full[to] = @dat
    @full.delete(@table)
    @table = to
    @dat = @full[@table]
  end

  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
  end

  def _write
    @full[@table] = @dat
    @db.write(@full)
  end

  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(actopm['dat'])
      when 'write'
        _write
      else
        raise NotImplementedError,
              "Action `#{action['type']}` is not yet implemented!"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
heliodor-0.1.1 lib/heliodor/query_internal.rb