Class: Gamefic::Command

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

Overview

A decomposition of a text-based command into its verb and arguments.

Commands are typically derived from tokenization against syntaxes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, arguments) ⇒ Command

Returns a new instance of Command.



15
16
17
18
# File 'lib/gamefic/command.rb', line 15

def initialize verb, arguments
  @verb = verb
  @arguments = arguments
end

Instance Attribute Details

#argumentsArray<String> (readonly)

Returns:



13
14
15
# File 'lib/gamefic/command.rb', line 13

def arguments
  @arguments
end

#verbSymbol (readonly)

Returns:

  • (Symbol)


10
11
12
# File 'lib/gamefic/command.rb', line 10

def verb
  @verb
end

Instance Method Details

#compare(other) ⇒ Object

Compare two syntaxes for the purpose of ordering them by relevance while dispatching.



23
24
25
26
27
28
29
# File 'lib/gamefic/command.rb', line 23

def compare other
  if verb == other.verb
    other.arguments.compact.length <=> arguments.compact.length
  else
    (other.verb ? 1 : 0) <=> (verb ? 1 : 0)
  end
end