module StaticRecord::QueryInterface::Retrievers

Contains retrievers

Private Instance Methods

all() click to toggle source
# File lib/static_record/models/query_interface/retrievers.rb, line 12
def all
  to_a
end
find(value) click to toggle source
# File lib/static_record/models/query_interface/retrievers.rb, line 16
def find(value)
  raise StaticRecord::NoPrimaryKey, 'No primary key have been set' if @primary_key.nil?
  unless value.is_a?(Array)
    @result_type = :record
    @sql_limit = 1
  end
  add_subclause(q: { :"#{@primary_key}" => value })
  res = to_a

  unless @only_sql
    case @result_type
    when :array
      if res.size != value.size
        err = "Couldn't find all #{@store.singularize.capitalize} "\
        "with '#{@primary_key}' IN #{value}"
        raise StaticRecord::RecordNotFound, err
      end
    when :record
      if res.nil?
        err = "Couldn't find #{@store.singularize.capitalize} "\
        "with '#{@primary_key}'=#{value}"
        raise StaticRecord::RecordNotFound, err
      end
    end
  end

  res
end
find_by(query) click to toggle source
# File lib/static_record/models/query_interface/retrievers.rb, line 45
def find_by(query)
  add_subclause(q: query)
  take(1)
end
first(amount = 1) click to toggle source
# File lib/static_record/models/query_interface/retrievers.rb, line 56
def first(amount = 1)
  @order_by << { :"#{@primary_key}" => :asc } if @primary_key && @order_by.empty?
  take(amount)
end
last(amount = 1) click to toggle source
# File lib/static_record/models/query_interface/retrievers.rb, line 61
def last(amount = 1)
  if !@primary_key && @order_by.empty?
    cnt = self.class.new(self, store: @store, primary_key: @primary_key).no_sql.send(:count)
    @sql_offset = [cnt - amount, 0].max
    res = take(amount)
  else
    @order_by << { :"#{@primary_key}" => :desc } if @order_by.empty?
    res = take(amount)
    res.reverse! if res.is_a?(Array)
  end
  res
end
take(amount = 1) click to toggle source
# File lib/static_record/models/query_interface/retrievers.rb, line 50
def take(amount = 1)
  @sql_limit = amount
  @result_type = :record if amount == 1
  to_a
end
to_a() click to toggle source
# File lib/static_record/models/query_interface/retrievers.rb, line 8
def to_a
  exec_request
end