Class: OPSA::Language

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

Instance Method Summary collapse

Constructor Details

#initializeLanguage

Returns a new instance of Language.



4
5
6
# File 'lib/opsa/language.rb', line 4

def initialize
  @actions = Hash.new nil
end

Instance Method Details

#input(text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/opsa/language.rb', line 23

def input(text)
  @actions.each do |regex, action|
    if _match = text.match(regex)
      args = _match.captures
      if args.size > 0
        action.call(*args)
      else
        action.call(text)
      end
    else
      next
    end
  end
end

#respond_to(regex, &action) ⇒ Object

Define language

Example:

lang.respond_to Regexp.new('^say_hello (.+)') do |match|
   puts "> Hello, #{match}!"
end

Arguments:

regex: Regexp
action: Block


19
20
21
# File 'lib/opsa/language.rb', line 19

def respond_to(regex, &action)
  @actions[regex] ||= action
end