Sha256: 3367bb4bbc2e8ecaf339c419a167418f4e8fbe7893b25c2f5505c6488baff785

Contents?: true

Size: 654 Bytes

Versions: 7

Compression:

Stored size: 654 Bytes

Contents

module Ambition
  module Limit
    def first(limit = 1, offset = nil)
      query_context.add LimitProcessor.new(limit, offset)
      find(limit == 1 ? :first : :all, query_context.to_hash)
    end

    def [](offset, limit = nil)
      return first(offset, limit) if limit

      if offset.is_a? Range
        limit  = offset.end
        limit -= 1 if offset.exclude_end?
        first(offset.first, limit - offset.first)
      else
        first(offset, 1)
      end
    end
  end

  class LimitProcessor 
    def initialize(*args)
      @args = args
    end

    def key
      :limit
    end

    def to_s
      @args.compact * ', '
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
cheat-1.3.3 lib/ambition/lib/ambition/limit.rb
cheat-1.3.2 lib/ambition/lib/ambition/limit.rb
cheat-1.3.1 lib/ambition/lib/ambition/limit.rb
cheat-1.3.0 lib/ambition/lib/ambition/limit.rb
ambition-0.1.0 ./lib/ambition/limit.rb
ambition-0.1.1 ./lib/ambition/limit.rb
ambition-0.1.2 ./lib/ambition/limit.rb