Sha256: c3171db5bcc9b485f5381b3a4aad4333f9d938254ee7ea10e740a54af7c60e2e

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

module StaticRecord
  # Contains ActiveRecord-like query interface methods
  module QueryInterface
    # Contains retrievers
    module Retrievers
      private

      def to_a
        exec_request
      end

      def all
        to_a
      end

      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

      def find_by(query)
        add_subclause(q: query)
        take(1)
      end

      def take(amount = 1)
        @sql_limit = amount
        @result_type = :record if amount == 1
        to_a
      end

      def first(amount = 1)
        @order_by << { :"#{@primary_key}" => :asc } if @primary_key && @order_by.empty?
        take(amount)
      end

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
static-record-1.2.1 lib/static_record/models/query_interface/retrievers.rb
static-record-1.2.0 lib/static_record/models/query_interface/retrievers.rb
static-record-1.1.0 lib/static_record/models/query_interface/retrievers.rb
static-record-1.0.1 lib/static_record/models/query_interface/retrievers.rb