class RnDB::Query

Public Class Methods

new(table, ids) click to toggle source

Query records of the given table based on the IDs in the supplied Thicket.

# File lib/rndb/query.rb, line 8
def initialize(table, ids)
  @table, @ids = table, ids
end

Public Instance Methods

[](index) click to toggle source

Retrieve the ID of an index into this query and use it to instantiate a record.

# File lib/rndb/query.rb, line 18
def [](index)
  @table[@ids[index]]
end
count() click to toggle source

Delegate counting to the Thicket.

# File lib/rndb/query.rb, line 13
def count
  @ids.count
end
each() { |table| ... } click to toggle source

Delegate iteration to the Thicket, yielding records to the caller.

# File lib/rndb/query.rb, line 33
def each
  @ids.each { |id| yield @table[id] }
end
index(id) click to toggle source

Retrieve the index of the specified ID.

# File lib/rndb/query.rb, line 23
def index(id)
  @ids.index(id)
end
last() click to toggle source

Implemented to be consistent with first, which we get by magic.

# File lib/rndb/query.rb, line 28
def last
  self[-1] unless count.zero?
end
pluck(*args) click to toggle source

Return an array or a hash of plucked values, avoiding generation of all attributes.

# File lib/rndb/query.rb, line 38
def pluck(*args)
  @ids.map do |id|
    if args.count == 1
      @table.value(id, args.first)
    else
      args.map do |attribute|
        [attribute, @table.value(id, attribute)]
      end.to_h
    end
  end
end
sample(limit=1) click to toggle source

Return a new query that takes a random sampling of IDs from the current query.

# File lib/rndb/query.rb, line 51
def sample(limit=1)
  _db.prng.srand
  self.class.new(@table, @ids.sample(limit, _db.prng))
end

Private Instance Methods

_db() click to toggle source
# File lib/rndb/query.rb, line 58
def _db
  Thread.current[:rndb_database]
end