Class: RailsOmnibar::Command::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_omnibar/command/search.rb

Overview

Generic search.

Direct Known Subclasses

RecordSearch

Instance Attribute Summary

Attributes inherited from Base

#description, #example, #if, #pattern, #resolver

Instance Method Summary collapse

Methods inherited from Base

#call, #handle?

Constructor Details

#initialize(finder:, itemizer:, **kwargs) ⇒ Search

Returns a new instance of Search.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_omnibar/command/search.rb', line 13

def initialize(finder:, itemizer:, **kwargs)
  finder   = RailsOmnibar.cast_to_proc(finder)
  itemizer = RailsOmnibar.cast_to_proc(itemizer)

  resolver = ->(value, controller:, omnibar:) do
    findings = finder.call(value, controller: controller, omnibar: omnibar)
    findings = Array(findings) unless findings.respond_to?(:first)

    findings.first(omnibar.max_results).flat_map do |finding|
      itemizer.call(finding, controller: controller, omnibar: omnibar)
    end
  end

  super(resolver: resolver, **kwargs)
end