Class: Commandorobo::Invoker
- Inherits:
-
Object
- Object
- Commandorobo::Invoker
- Defined in:
- lib/commandorobo.rb
Overview
Class to represent an invoker (prefix, suffix).
Instance Attribute Summary collapse
-
#type ⇒ Symbol
readonly
The type of the invoker.
-
#value ⇒ String
readonly
The value of the invoker.
Instance Method Summary collapse
-
#check(text) ⇒ Object
Checks to see if the invoker is correct for the text.
-
#extrapolate(text) ⇒ Object
Gets the actual command and arguments from a string.
-
#initialize(val, type, **kwargs) ⇒ Invoker
constructor
A new instance of Invoker.
-
#match(text) ⇒ Object
Internal - checks to see if regexes match.
Constructor Details
#initialize(val, type, **kwargs) ⇒ Invoker
Returns a new instance of Invoker.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/commandorobo.rb', line 36 def initialize(val, type, **kwargs) if type == :regex val = Regexp::new(val) end if !kwargs[:sep].nil? && type == :dual val = val.split(kwargs[:sep]) end @value = val @type = type end |
Instance Attribute Details
#type ⇒ Symbol (readonly)
The type of the invoker.
34 35 36 |
# File 'lib/commandorobo.rb', line 34 def type @type end |
#value ⇒ String (readonly)
The value of the invoker.
34 35 36 |
# File 'lib/commandorobo.rb', line 34 def value @value end |
Instance Method Details
#check(text) ⇒ Object
Checks to see if the invoker is correct for the text.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/commandorobo.rb', line 74 def check(text) case @type when :prefix return text.start_with? @value when :suffix return text.end_with? @value when :dual return text.start_with?(@value[0]) && text.end_with?(@value[1]) when :regex return !self.match(text).nil? end end |
#extrapolate(text) ⇒ Object
Gets the actual command and arguments from a string.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/commandorobo.rb', line 59 def extrapolate(text) case @type when :prefix return text[@value.length..text.length] when :suffix return text[0..-@value.length-1] when :dual return text[@value[0].length..-@value[1].length-1] when :regex return self.match(text) end end |
#match(text) ⇒ Object
Internal - checks to see if regexes match.
50 51 52 53 54 55 |
# File 'lib/commandorobo.rb', line 50 def match(text) if @type != :regex raise "Incorrect type for function." end return @value.match(text) end |