Class: Gamefic::Query::Base

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

Overview

A base class for entity-based queries that can be applied to responses. Each query represents an attempt to match an argument in a command to a game entity.

Direct Known Subclasses

General, Scoped, Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments, ambiguous: false, name: self.class.to_s) ⇒ Base

Returns a new instance of Base.

Parameters:

  • arguments (Array<Object>)
  • ambiguous (Boolean) (defaults to: false)
  • name (String) (defaults to: self.class.to_s)

Raises:

  • (ArgumentError)

    if any of the arguments are nil



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

def initialize *arguments, ambiguous: false, name: self.class.to_s
  raise ArgumentError, "nil argument in query" if arguments.any?(&:nil?)

  @arguments = arguments
  @ambiguous = ambiguous
  @name = name
end

Instance Attribute Details

#ambiguousBoolean (readonly)

Returns:

  • (Boolean)


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

def ambiguous
  @ambiguous
end

#argumentsArray<Object> (readonly)

Returns:



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

def arguments
  @arguments
end

#narrativeObject

Returns the value of attribute narrative.



16
17
18
# File 'lib/gamefic/query/base.rb', line 16

def narrative
  @narrative
end

Instance Method Details

#accept?(subject, object) ⇒ Boolean

True if the object is selectable by the subject.

Parameters:

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File 'lib/gamefic/query/base.rb', line 80

def accept?(subject, object)
  available = select(subject)
  if ambiguous?
    object & available == object
  else
    available.include?(object)
  end
end

#ambiguous?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/gamefic/query/base.rb', line 94

def ambiguous?
  @ambiguous
end

#inspectObject



102
103
104
# File 'lib/gamefic/query/base.rb', line 102

def inspect
  "##{ambiguous? ? '*' : ''}#{name}(#{normalized_arguments.map(&:inspect).join(', ')})"
end

#nameObject



98
99
100
# File 'lib/gamefic/query/base.rb', line 98

def name
  @name || self.class.to_s
end

#precisionInteger

Returns:

  • (Integer)


90
91
92
# File 'lib/gamefic/query/base.rb', line 90

def precision
  @precision ||= calculate_precision
end

#query(subject, token) ⇒ Result Also known as: filter

Get a query result for a given subject and token.

Examples:

respond :reds do |actor|
  reds = available(ambiguous: true).query(actor, 'red').match
  actor.tell "The red things you can see here are #{reds.join_and}."
end

Parameters:

Returns:



42
43
44
45
46
47
48
49
50
51
# File 'lib/gamefic/query/base.rb', line 42

def query(subject, token)
  first_pass = Scanner.scan(span(subject), token)
  if ambiguous?
    ambiguous_result(first_pass.filter(*normalized_arguments))
  elsif first_pass.match.one?
    unambiguous_result(first_pass.filter(*normalized_arguments))
  else
    unambiguous_result(first_pass)
  end
end

#select(subject) ⇒ Array<Entity>

Get an array of entities that match the arguments from the context of the subject.

Parameters:

Returns:



59
60
61
# File 'lib/gamefic/query/base.rb', line 59

def select subject
  span(subject).that_are(*normalized_arguments)
end

#span(_subject) ⇒ Array<Entity>

Get an array of entities that are candidates for selection from the context of the subject. These are the entities that #select will filter through query’s arguments.

Subclasses should override this method.

Parameters:

Returns:



71
72
73
# File 'lib/gamefic/query/base.rb', line 71

def span _subject
  []
end