Class: Commandorobo::Invoker

Inherits:
Object
  • Object
show all
Defined in:
lib/commandorobo.rb

Overview

Class to represent an invoker (prefix, suffix).

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#typeSymbol (readonly)

The type of the invoker.

Returns:

  • (Symbol)

    the current value of type



34
35
36
# File 'lib/commandorobo.rb', line 34

def type
  @type
end

#valueString (readonly)

The value of the invoker.

Returns:

  • (String)

    the current value of value



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.

Parameters:

  • text (String)

    The text to check.



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.

Parameters:

  • text (String)

    The text to extrapolate.



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.

Parameters:

  • text (String)

    The text to check.

Raises:

  • (RuntimeError)

    The invoker wasn’t a regex.



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