Class: SRSGame::Commands

Inherits:
Object show all
Defined in:
lib/srs_game.rb

Overview

Class methods beginning with ‘_’ are available as commands during the game

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object

Called when a non-existing command is entered during the game



276
277
278
# File 'lib/srs_game.rb', line 276

def method_missing(m, *a)
  "#{self.class}##{m.downcase}: not found".red
end

Instance Method Details

#_exit(a, g) ⇒ Object Also known as: _quit

Quit the game

Raises:



314
315
316
# File 'lib/srs_game.rb', line 314

def _exit(a, g)
  raise DONE_WITH_SRS_GAME
end

#_help(a, g) ⇒ Object Also known as: _?

Display help text



327
328
329
# File 'lib/srs_game.rb', line 327

def _help(a, g)
  "All available commands:\n#{callable_methods.map(&:command_pp).to_sentence(:bold => true)}"
end

#_look(a, g) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/srs_game.rb', line 318

def _look(a, g)
  if a.args.blank?
    g.room.info
  else # We are looking for an item
    g.room.use_item(a) { |item| "You see #{item.name.bold}." }
  end
end

#callable_methodsObject

Methods that start with ‘_’ and don’t end with ‘_



281
282
283
# File 'lib/srs_game.rb', line 281

def callable_methods
  methods.grep(/^_\w+[^_]$/)
end

#matching_methods(match) ⇒ Object



285
286
287
# File 'lib/srs_game.rb', line 285

def matching_methods(match)
  callable_methods.grep(/^_#{match}/)
end

#parse(input, game) ⇒ Object

Parse input and __send__ it



290
291
292
293
294
295
296
# File 'lib/srs_game.rb', line 290

def parse(input, game)
  method = input.words.first.command_pp

  arguments = input.words[1..-1]

  output = __send__("_#{method}", arguments.join(" "), game)
end