Class: Gamefic::Query::Expression

Inherits:
Base
  • Object
show all
Defined in:
lib/gamefic/query/expression.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/expression.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
39
40
41
42
# File 'lib/gamefic/query/expression.rb', line 18

def execute(subject, description)
  if @arguments.length == 0
    return Matches.new([description], description, '')
  end
  keywords = Keywords.new(description)
  possible = []
  @arguments.each { |regexp|
    remainder = keywords.clone
    used = []
    while remainder.length > 0
      used.push remainder.shift
      if used.join(' ').match(regexp)
        possible.push Matches.new([used.join(' ')], used.join(' '), remainder.join(' '))
      end        
    end
  }
  if possible.length > 0
    possible.sort! { |a, b|
     b.matching_text.length <=> a.matching_text.length
    }
    return possible[0]
  else
    return Matches.new([], '', description)
  end
end

#test_arguments(arguments) ⇒ Object



43
44
45
# File 'lib/gamefic/query/expression.rb', line 43

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/expression.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