Class: Stringfire::Interpreter
- Inherits:
-
Object
- Object
- Stringfire::Interpreter
- Defined in:
- lib/stringfire.rb
Instance Method Summary collapse
- #command(name, &block) ⇒ Object
- #command_names ⇒ Object
- #default(*args, &block) ⇒ Object
-
#initialize ⇒ Interpreter
constructor
A new instance of Interpreter.
- #interpret(command_text, *args) ⇒ Object
- #split_first_word(str) ⇒ Object
Constructor Details
#initialize ⇒ Interpreter
Returns a new instance of Interpreter.
12 13 14 15 |
# File 'lib/stringfire.rb', line 12 def initialize @commands = { } default { |command, *args| command } end |
Instance Method Details
#command(name, &block) ⇒ Object
17 18 19 |
# File 'lib/stringfire.rb', line 17 def command name, &block @commands[name] = block end |
#command_names ⇒ Object
21 22 23 |
# File 'lib/stringfire.rb', line 21 def command_names @commands.keys end |
#default(*args, &block) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/stringfire.rb', line 25 def default *args, &block if block_given? @default = block else @default.call *args end end |
#interpret(command_text, *args) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/stringfire.rb', line 33 def interpret command_text, *args name, details = split_first_word command_text if @commands.key? name @commands[name].call details, *args else default command_text, *args end end |
#split_first_word(str) ⇒ Object
6 7 8 9 10 |
# File 'lib/stringfire.rb', line 6 def split_first_word str result = str.strip.split /\s/, 2 result << "" while result.size < 2 result end |