Class: RailsOmnibar::Command::Base
- Inherits:
-
Object
- Object
- RailsOmnibar::Command::Base
- Defined in:
- lib/rails_omnibar/command/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#example ⇒ Object
readonly
Returns the value of attribute example.
-
#if ⇒ Object
readonly
Returns the value of attribute if.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
Instance Method Summary collapse
- #call(input, controller:, omnibar:) ⇒ Object
- #handle?(input, controller:, omnibar:) ⇒ Boolean
-
#initialize(pattern:, resolver:, description: nil, example: nil, if: nil) ⇒ Base
constructor
A new instance of Base.
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
#description ⇒ Object (readonly)
Returns the value of attribute description.
4 5 6 |
# File 'lib/rails_omnibar/command/base.rb', line 4 def description @description end |
#example ⇒ Object (readonly)
Returns the value of attribute example.
4 5 6 |
# File 'lib/rails_omnibar/command/base.rb', line 4 def example @example end |
#if ⇒ Object (readonly)
Returns the value of attribute if.
4 5 6 |
# File 'lib/rails_omnibar/command/base.rb', line 4 def if @if end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
4 5 6 |
# File 'lib/rails_omnibar/command/base.rb', line 4 def pattern @pattern end |
#resolver ⇒ Object (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: ) results = results.try(:to_ary) || [results] results.map { |e| RailsOmnibar.cast_to_item(e) } end |
#handle?(input, controller:, omnibar:) ⇒ 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: ) end |