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, if: nil) ⇒ Base

Returns a new instance of Base.



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

def initialize(pattern:, resolver:, description: nil, example: nil, if: nil)
  @pattern = cast_to_pattern(pattern)
  @resolver = RailsOmnibar.cast_to_proc(resolver)
  @description = description
  @example = example
  @if = RailsOmnibar.cast_to_condition(binding.local_variable_get(:if))
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

#ifObject (readonly)

Returns the value of attribute if.



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

def if
  @if
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



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

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

#handle?(input, controller:, omnibar:) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/rails_omnibar/command/base.rb', line 23

def handle?(input, controller:, omnibar:)
  return false unless pattern.match?(input)

  RailsOmnibar.evaluate_condition(self.if, context: controller, omnibar: omnibar)
end