Class: Searchlight::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/searchlight/search.rb

Constant Summary collapse

SEARCH_METHOD_PATTERN =
/\Asearch_(?<option>.*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(raw_options = {})
  string_keys, non_string_keys = raw_options.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 = raw_options
end

Instance Attribute Details

#queryObject

Returns the value of attribute query.



7
8
9
# File 'lib/searchlight/search.rb', line 7

def query
  @query
end

#raw_optionsObject (readonly)

Returns the value of attribute raw_options.



8
9
10
# File 'lib/searchlight/search.rb', line 8

def raw_options
  @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
      options.key?(option_name) ? options[option_name] : options[option_name.to_sym]
    end
  end
end

Instance Method Details

#checked?(value) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/searchlight/search.rb', line 41

def checked?(value)
  Searchlight::Options.checked?(value)
end

#empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/searchlight/search.rb', line 37

def empty?(value)
  Searchlight::Options.empty?(value)
end

#explainObject



45
46
47
48
49
50
51
52
53
# File 'lib/searchlight/search.rb', line 45

def explain
  [
    "Initialized with `raw_options`: #{raw_options.keys.inspect}",
    "Of those, the non-blank ones are available as `options`: #{options.keys.inspect}",
    "Of those, the following have corresponding `search_` methods: #{options_with_search_methods.keys}. These would be used to build the query.",
    "Blank options are: #{(raw_options.keys - options.keys).inspect}",
    "Non-blank options with no corresponding `search_` method are: #{options.keys - options_with_search_methods.keys}",
  ].join("\n\n")
end

#optionsObject



33
34
35
# File 'lib/searchlight/search.rb', line 33

def options
  Searchlight::Options.excluding_empties(raw_options)
end

#options_with_search_methodsObject



55
56
57
58
59
60
61
62
# File 'lib/searchlight/search.rb', line 55

def options_with_search_methods
  {}.tap do |map|
    options.each do |option_name, _|
      method_name = "search_#{option_name}" 
      map[option_name] = method_name if respond_to?(method_name)
    end
  end
end

#resultsObject



29
30
31
# File 'lib/searchlight/search.rb', line 29

def results
  @results ||= run
end