Class: Gamefic::Query::Text

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

Constant Summary

Constants included from Gamefic

DOWN, EAST, NORTH, NORTHEAST, NORTHWEST, SOUTH, SOUTHEAST, SOUTHWEST, UP, VERSION, WEST

Instance Attribute Summary

Attributes inherited from Base

#arguments

Instance Method Summary collapse

Methods inherited from Base

#allow_ambiguous?, #allow_many?, #context_from, #initialize, #last_match_for, #match, #optional?, #signature, #specificity

Constructor Details

This class inherits a constructor from Gamefic::Query::Base

Instance Method Details

#base_specificityObject



3
4
5
# File 'lib/gamefic/query/text.rb', line 3

def base_specificity
  10
end

#execute(subject, description) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gamefic/query/text.rb', line 18

def execute(subject, description)
  if @arguments.length == 0
    return Matches.new([description], description, '')
  end
  keywords = Keywords.new(description)
  args = Keywords.new(@arguments)
  found = Array.new
  remainder = keywords.clone
  while remainder.length > 0
    if args.include?(remainder.first)
      found.push remainder.shift
    else
      break
    end
  end
  if found.length > 0
    return Matches.new(found, found.join(' '), remainder.join(' '))
  else
    return Matches.new([], '', description)
  end
end

#test_arguments(arguments) ⇒ Object



39
40
41
# File 'lib/gamefic/query/text.rb', line 39

def test_arguments arguments
  # No test for text
end

#validate(subject, description) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gamefic/query/text.rb', line 6

def validate(subject, description)
  return false unless description.kind_of?(String)
  valid = false
  words = description.split_words
  words.each { |word|
    if description.include?(word)
      valid = true
      break
    end
  }
  valid
end