Sha256: bfa36552aa8df1935a5495ee29a217058aaf70cebf860381605e6f1f1ef8963d

Contents?: true

Size: 1.07 KB

Versions: 28

Compression:

Stored size: 1.07 KB

Contents

module Friendly
  class Query
    attr_reader :conditions, :limit, :order, 
                :preserve_order, :offset, :uuid_klass,
                :page, :per_page

    def initialize(parameters, uuid_klass = UUID)
      @uuid_klass = uuid_klass
      @conditions = parameters.reject { |k,v| k.to_s =~ /!$/ }
      @page       = (parameters[:page!] || 1).to_i
      
      [:per_page!, :limit!, :offset!, :order!, :preserve_order!].each do |p|
        instance_variable_set("@#{p.to_s.gsub(/!/, '')}", parameters[p])
      end

      handle_pagination if per_page
      convert_ids_to_uuids
    end

    def preserve_order?
      preserve_order
    end

    def offset?
      offset
    end

    protected
      def convert_ids_to_uuids
        if conditions[:id] && conditions[:id].is_a?(Array)
          conditions[:id] = conditions[:id].map { |i| uuid_klass.new(i) }
        elsif conditions[:id]
          conditions[:id] = uuid_klass.new(conditions[:id])
        end
      end

      def handle_pagination
        @limit  = per_page
        @offset = (page - 1) * per_page
      end
  end
end

Version data entries

28 entries across 28 versions & 7 rubygems

Version Path
ihoka-friendly-0.8.0.pre lib/friendly/query.rb
ihoka-friendly-0.7.1.2 lib/friendly/query.rb
ihoka-friendly-0.7.1.1 lib/friendly/query.rb
ihoka-friendly-0.7.1 lib/friendly/query.rb
ihoka-friendly-0.7.0 lib/friendly/query.rb
ihoka-friendly-0.6.3 lib/friendly/query.rb
ihoka-friendly-0.6.2 lib/friendly/query.rb
friendly-0.6.0 lib/friendly/query.rb
honkster-friendly-0.5.3 lib/friendly/query.rb
honkster-friendly-0.5.2 lib/friendly/query.rb
honkster-friendly-0.5.1 lib/friendly/query.rb
wayne-friendly-0.5.1 lib/friendly/query.rb
wego-friendly-0.5.1 lib/friendly/query.rb
arunthampi-friendly-0.5.1 lib/friendly/query.rb
friendly_postgres-0.5.1 lib/friendly/query.rb
friendly-0.5.1 lib/friendly/query.rb
friendly-0.5.0 lib/friendly/query.rb
friendly_postgres-0.4.5 lib/friendly/query.rb
friendly-0.4.5 lib/friendly/query.rb
friendly-0.4.4 lib/friendly/query.rb