Class: RailsOmnibar::Command::Base

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

Direct Known Subclasses

Search

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern:, resolver:, description: nil, example: nil) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
# File 'lib/rails_omnibar/command/base.rb', line 6

def initialize(pattern:, resolver:, description: nil, example: nil)
  @pattern = cast_to_pattern(pattern)
  @resolver = RailsOmnibar.cast_to_proc(resolver)
  @description = description
  @example = example
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



4
5
6
# File 'lib/rails_omnibar/command/base.rb', line 4

def description
  @description
end

#exampleObject (readonly)

Returns the value of attribute example.



4
5
6
# File 'lib/rails_omnibar/command/base.rb', line 4

def example
  @example
end

#patternObject (readonly)

Returns the value of attribute pattern.



4
5
6
# File 'lib/rails_omnibar/command/base.rb', line 4

def pattern
  @pattern
end

#resolverObject (readonly)

Returns the value of attribute resolver.



4
5
6
# File 'lib/rails_omnibar/command/base.rb', line 4

def resolver
  @resolver
end

Instance Method Details

#call(input, controller:, omnibar:) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/rails_omnibar/command/base.rb', line 13

def call(input, controller:, omnibar:)
  match = pattern.match(input)
  # look at match for highest capturing group or whole pattern
  value = match.to_a.last || raise(ArgumentError, 'input !~ pattern')
  results = resolver.call(value, controller: controller, omnibar: omnibar)
  results = results.try(:to_ary) || [results]
  results.map { |e| RailsOmnibar.cast_to_item(e) }
end