Skip to content

Commit

Permalink
Limit .options to search options (not accessors)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanl committed Jun 20, 2013
1 parent f9efd04 commit 3ded3e0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
12 changes: 2 additions & 10 deletions lib/searchlight/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ def truthy?(value)
include @accessors_module
end

eval_string = attribute_names.map { |attribute_name|
eval_string = "attr_accessor *#{attribute_names}\n"
eval_string << attribute_names.map { |attribute_name|
<<-LEPRECHAUN_JUICE
def #{attribute_name}=(val)
self.options ||= {}
self.options[:#{attribute_name}] = val
end
def #{attribute_name}
self.options[:#{attribute_name}]
end
def #{attribute_name}?
truthy?(public_send("#{attribute_name}"))
end
Expand Down
18 changes: 14 additions & 4 deletions lib/searchlight/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Searchlight
class Search
extend DSL

attr_accessor :options

def self.search_target
return @search_target if defined?(@search_target)
return superclass.search_target if superclass.respond_to?(:search_target) && superclass != Searchlight::Search
Expand All @@ -22,12 +20,25 @@ def results
@results ||= run
end

def options
search_methods.inject({}) { |hash, method_name|
opt_name = method_name.sub(/\Asearch_/, '')
hash[opt_name.to_sym] = send(opt_name)

hash
}.reject { |name, value| is_blank?(value) }
end

protected

attr_writer :search

private

def search_methods
public_methods.map(&:to_s).select { |m| m.start_with?('search_') }
end

def self.guess_search_class!
if self.name.end_with?('Search')
@search_target = name.sub(/Search\z/, '').split('::').inject(Kernel, &:const_get)
Expand All @@ -44,8 +55,7 @@ def self.search_target=(value)
end

def filter_and_mass_assign(provided_options = {})
provided_options = {} if provided_options.nil?
self.options = provided_options.reject { |key, value| is_blank?(value) }
options = (provided_options || {}).reject { |key, value| is_blank?(value) }
begin
options.each { |key, value| public_send("#{key}=", value) } if options && options.any?
rescue NoMethodError => e
Expand Down
29 changes: 28 additions & 1 deletion spec/searchlight/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

describe Searchlight::Search do

let(:search_class) { Named::Class.new('ExampleSearch', described_class).tap { |klass| klass.searches *allowed_options } }
let(:search_class) { Named::Class.new('ExampleSearch', described_class).tap {|klass|
klass.searches *allowed_options
allowed_options.each { |name| klass.send(:define_method, "search_#{name}") {} }
}
}
let(:allowed_options) { Hash.new }
let(:provided_options) { Hash.new }
let(:search) { search_class.new(provided_options) }
Expand Down Expand Up @@ -57,6 +61,26 @@

end

context "when some options are do not map to search methods (eg, attr_accessor)" do
let(:search_class) {
Named::Class.new('ExampleSearch', described_class) do
attr_accessor :krazy_mode
def search_name; end
end.tap { |klass| klass.searches *allowed_options }
}
let(:provided_options) { {name: 'Reese Roper', krazy_mode: true} }

it "sets all the provided values" do
expect(search.name).to eq('Reese Roper')
expect(search.krazy_mode).to eq(true)
end

it "only lists options for the values corresponding to search methods" do
expect(search.options).to eq({name: 'Reese Roper'})
end

end

end

end
Expand All @@ -73,6 +97,9 @@ def initialize(options)
self.age ||= 37
end

def search_name; end
def search_age; end

end.tap { |klass| klass.searches *allowed_options }
}

Expand Down

0 comments on commit 3ded3e0

Please sign in to comment.