Sha256: 47b9ea59982e903989c5c19a736925e6139723cea43ea1b898f090a736185ca6

Contents?: true

Size: 782 Bytes

Versions: 10

Compression:

Stored size: 782 Bytes

Contents

module PgSearch
  class Scope
    def initialize(name, model, scope_options_or_proc)
      @name = name
      @model = model
      @options_proc = build_options_proc(scope_options_or_proc)
    end

    def to_proc
      lambda { |*args|
        config = Configuration.new(@options_proc.call(*args), @model)
        ScopeOptions.new(@name, @model, config).to_relation
      }
    end

    private

    def build_options_proc(scope_options_or_proc)
      case scope_options_or_proc
        when Proc
          scope_options_or_proc
        when Hash
          lambda { |query|
            scope_options_or_proc.reverse_merge(:query => query)
          }
        else
          raise ArgumentError, "A PgSearch scope expects a Proc or Hash for its options"
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pg_search-0.5.1 lib/pg_search/scope.rb
pg_search-0.5 lib/pg_search/scope.rb
pg_search-0.4.2 lib/pg_search/scope.rb
pg_search-0.4.1 lib/pg_search/scope.rb
pg_search-0.4 lib/pg_search/scope.rb
pg_search-0.3.4 lib/pg_search/scope.rb
pg_search-0.3.3 lib/pg_search/scope.rb
pg_search-0.3.2 lib/pg_search/scope.rb
pg_search-0.3.1 lib/pg_search/scope.rb
pg_search-0.3 lib/pg_search/scope.rb