Class: Searchlight::Search
- Inherits:
-
Object
- Object
- Searchlight::Search
- Defined in:
- lib/searchlight/search.rb
Constant Summary collapse
- SEARCH_METHOD_PATTERN =
/\Asearch_(?<option>.*)/
Instance Attribute Summary collapse
-
#query ⇒ Object
Returns the value of attribute query.
-
#raw_options ⇒ Object
readonly
Returns the value of attribute raw_options.
Class Method Summary collapse
Instance Method Summary collapse
- #checked?(value) ⇒ Boolean
- #empty?(value) ⇒ Boolean
- #explain ⇒ Object
-
#initialize(raw_options = {}) ⇒ Search
constructor
A new instance of Search.
- #options ⇒ Object
- #options_with_search_methods ⇒ Object
- #results ⇒ Object
Constructor Details
#initialize(raw_options = {}) ⇒ Search
Returns a new instance of Search.
20 21 22 23 24 25 26 27 |
# File 'lib/searchlight/search.rb', line 20 def initialize( = {}) string_keys, non_string_keys = .keys.partition {|k| k.is_a?(String) } intersection = string_keys & non_string_keys.map(&:to_s) if intersection.any? fail ArgumentError, "more than one key converts to these string values: #{intersection}" end @raw_options = end |
Instance Attribute Details
#query ⇒ Object
Returns the value of attribute query.
7 8 9 |
# File 'lib/searchlight/search.rb', line 7 def query @query end |
#raw_options ⇒ Object (readonly)
Returns the value of attribute raw_options.
8 9 10 |
# File 'lib/searchlight/search.rb', line 8 def @raw_options end |
Class Method Details
.method_added(method_name) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/searchlight/search.rb', line 10 def self.method_added(method_name) method_name.to_s.match(SEARCH_METHOD_PATTERN) do |match| option_name = match.captures.fetch(0) # accessor - eg, if method_name is #search_title, define #title define_method(option_name) do .key?(option_name) ? [option_name] : [option_name.to_sym] end end end |
Instance Method Details
#checked?(value) ⇒ Boolean
41 42 43 |
# File 'lib/searchlight/search.rb', line 41 def checked?(value) Searchlight::Options.checked?(value) end |
#empty?(value) ⇒ Boolean
37 38 39 |
# File 'lib/searchlight/search.rb', line 37 def empty?(value) Searchlight::Options.empty?(value) end |
#explain ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/searchlight/search.rb', line 45 def explain [ "Initialized with `raw_options`: #{.keys.inspect}", "Of those, the non-blank ones are available as `options`: #{.keys.inspect}", "Of those, the following have corresponding `search_` methods: #{.keys}. These would be used to build the query.", "Blank options are: #{(.keys - .keys).inspect}", "Non-blank options with no corresponding `search_` method are: #{.keys - .keys}", ].join("\n\n") end |
#options ⇒ Object
33 34 35 |
# File 'lib/searchlight/search.rb', line 33 def Searchlight::Options.excluding_empties() end |
#options_with_search_methods ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/searchlight/search.rb', line 55 def {}.tap do |map| .each do |option_name, _| method_name = "search_#{option_name}" map[option_name] = method_name if respond_to?(method_name) end end end |
#results ⇒ Object
29 30 31 |
# File 'lib/searchlight/search.rb', line 29 def results @results ||= run end |