Class: Gamefic::Command

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

Overview

A concrete representation of an input as a verb and an array of arguments.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, arguments, strictness = 0, precision = 0) ⇒ Command

TODO:

Consider making strictness and precision required or providing another generator

Returns a new instance of Command.

Parameters:



26
27
28
29
30
31
# File 'lib/gamefic/command.rb', line 26

def initialize verb, arguments, strictness = 0, precision = 0
  @verb = verb
  @arguments = arguments
  @strictness = strictness
  @precision = precision
end

Instance Attribute Details

#argumentsArray<Array<Entity>, Entity, String> (readonly)

Returns:



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

def arguments
  @arguments
end

#precisionInteger (readonly)

Returns:

  • (Integer)


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

def precision
  @precision
end

#strictnessInteger (readonly)

Returns:

  • (Integer)


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

def strictness
  @strictness
end

#verbSymbol (readonly)

Returns:

  • (Symbol)


8
9
10
# File 'lib/gamefic/command.rb', line 8

def verb
  @verb
end

Class Method Details

.compose(actor, input) ⇒ Command

Compose a command from input.

Parameters:

Returns:



47
48
49
50
51
# File 'lib/gamefic/command.rb', line 47

def compose actor, input
  expressions = Syntax.tokenize(input, actor.epic.syntaxes)
  expressions.flat_map { |expression| expression_to_commands(actor, expression) }
             .first || Command.new(nil, [])
end

Instance Method Details

#inspectObject



37
38
39
# File 'lib/gamefic/command.rb', line 37

def inspect
  "#<#{self.class} #{([verb] + arguments).map(&:inspect).join(', ')}>"
end

#substantialityObject



33
34
35
# File 'lib/gamefic/command.rb', line 33

def substantiality
  @substantiality ||= arguments.that_are(Entity).length + (verb ? 1 : 0)
end