From 6f1f154f560f3077c207075c6ece0e82e83afc71 Mon Sep 17 00:00:00 2001 From: Grant Hutchins & Peter Jaros Date: Wed, 17 Nov 2010 09:50:03 -0500 Subject: [PATCH] Reorganize files and cleanup syntax --- lib/pg_search.rb | 45 ++++++---------------------------- lib/pg_search/scope.rb | 30 +++++++++++++++++++++++ lib/pg_search/scope_options.rb | 25 +++++++++++-------- 3 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 lib/pg_search/scope.rb diff --git a/lib/pg_search.rb b/lib/pg_search.rb index a2d72030..7cd785b9 100644 --- a/lib/pg_search.rb +++ b/lib/pg_search.rb @@ -1,4 +1,5 @@ require "active_record" +require "pg_search/scope" require "pg_search/scope_options" module PgSearch @@ -6,46 +7,16 @@ 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 diff --git a/lib/pg_search/scope.rb b/lib/pg_search/scope.rb new file mode 100644 index 00000000..609302e7 --- /dev/null +++ b/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 diff --git a/lib/pg_search/scope_options.rb b/lib/pg_search/scope_options.rb index 266904a9..15b91898 100644 --- a/lib/pg_search/scope_options.rb +++ b/lib/pg_search/scope_options.rb @@ -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]) @@ -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