Skip to content

Commit

Permalink
Reorganize files and cleanup syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Hutchins & Peter Jaros committed Nov 17, 2010
1 parent e898dfd commit 6f1f154
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
45 changes: 8 additions & 37 deletions lib/pg_search.rb
@@ -1,51 +1,22 @@
require "active_record"
require "pg_search/scope"
require "pg_search/scope_options"

module PgSearch
def self.included(base)
base.send(:extend, ClassMethods)
end

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

def to_proc
lambda { |*args|
ScopeOptions.new(@name, @options_proc, @model, args).to_hash
}
end

private

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


module ClassMethods
def pg_search_scope(name, options)
scope = PgSearch::Scope.new(name, options, self)
scope_method = if respond_to?(:scope) && !protected_methods.include?('scope')
:scope # ActiveRecord 3.x
else
:named_scope # ActiveRecord 2.x
end
scope_method =
if respond_to?(:scope) && !protected_methods.include?('scope')
:scope # ActiveRecord 3.x
else
:named_scope # ActiveRecord 2.x
end

send(scope_method, name, scope.to_proc)
end
end
Expand Down
30 changes: 30 additions & 0 deletions lib/pg_search/scope.rb
@@ -0,0 +1,30 @@
module PgSearch
class Scope
def initialize(name, scope_options_or_proc, model)
@name = name
@model = model
@options_proc = build_options_proc(scope_options_or_proc)
end

def to_proc
lambda { |*args|
ScopeOptions.new(@name, @options_proc, @model, args).to_hash
}
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
25 changes: 15 additions & 10 deletions lib/pg_search/scope_options.rb
Expand Up @@ -12,11 +12,6 @@ def initialize(name, options_proc, model, args)
@args = args
end

def pg_search_options
@options_proc.call(*@args).reverse_merge(default_options()).tap { |options| assert_valid_options(options) }
end
memoize :pg_search_options

def to_hash
query = pg_search_options[:query].to_s
normalizing = Array.wrap(pg_search_options[:normalizing])
Expand Down Expand Up @@ -79,19 +74,29 @@ def to_hash

private

def pg_search_options
@options_proc.call(*@args).reverse_merge(default_options).tap do |options|
assert_valid_options(options)
end
end
memoize :pg_search_options

def default_options
{:using => :tsearch}
end

def assert_valid_options(options)
options.assert_valid_keys(:against, :ranked_by, :normalizing, :with_dictionary, :using, :query)

{
valid_keys = [:against, :ranked_by, :normalizing, :with_dictionary, :using, :query]
valid_values = {
:using => [:trigram, :tsearch],
:normalizing => [:prefixes, :diacritics]
}.each do |key, valid_values|
}

options.assert_valid_keys(valid_keys)

valid_values.each do |key, values_for_key|
Array.wrap(options[key]).each do |value|
unless valid_values.include?(value)
unless values_for_key.include?(value)
raise ArgumentError, ":#{key} cannot accept #{value}"
end
end
Expand Down

0 comments on commit 6f1f154

Please sign in to comment.