Class: Gamefic::Query::Base

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

Direct Known Subclasses

Children, Family, Itself, Parent, Siblings, Text

Constant Summary collapse

NEST_REGEXP =
/ in | on | of | from | inside /

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



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

def initialize *args
  @arguments = args
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



6
7
8
# File 'lib/gamefic/query/base.rb', line 6

def arguments
  @arguments
end

Instance Method Details

#accept?(entity) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gamefic/query/base.rb', line 77

def accept?(entity)
  result = true
  arguments.each { |a|
    if a.kind_of?(Symbol)
      result = (entity.send(a) != false)
    elsif a.kind_of?(Regexp)
      result = (!entity.to_s.match(a).nil?)
    elsif a.is_a?(Module) or a.is_a?(Class)
      result = (entity.is_a?(a))
    else
      result = (entity == a)
    end
    break if result == false
  }
  result
end

#ambiguous?Boolean

Returns:

  • (Boolean)


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

def ambiguous?
  false
end

#context_from(subject) ⇒ Array<Object>

Subclasses should override this method with the logic required to collect all entities that exist in the query’s context.

Returns:



20
21
22
# File 'lib/gamefic/query/base.rb', line 20

def context_from(subject)
  []
end

#include?(subject, object) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/gamefic/query/base.rb', line 44

def include?(subject, object)
  return false unless accept?(object)
  result = context_from(subject)
  result.include?(object)
end

#precisionObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gamefic/query/base.rb', line 50

def precision
  if @precision.nil?
    @precision = 1
    arguments.each { |a|
      if a.kind_of?(Symbol) or a.kind_of?(Regexp)
        @precision += 1
      elsif a.kind_of?(Class)
        @precision += (count_superclasses(a) * 100)
      elsif a.kind_of?(Module)
        @precision += 10
      elsif a.kind_of?(Object)
        @precision += 1000
      end
    }
    @precision
  end
  @precision
end

#rankObject



69
70
71
# File 'lib/gamefic/query/base.rb', line 69

def rank
  precision
end

#resolve(subject, token, continued: false) ⇒ Object

Get an array of objects that exist in the subject’s context and match the provided token.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gamefic/query/base.rb', line 27

def resolve(subject, token, continued: false)
  available = context_from(subject)
  return Matches.new([], '', token) if available.empty?
  if continued
    return Matches.execute(available, token, continued: continued)
  elsif nested?(token)
    drill = denest(available, token)
    drill.keep_if{ |e| accept?(e) }
    return Matches.new(drill, token, '') unless drill.length != 1
    return Matches.new([], '', token)
  end
  result = available.select{ |e| e.match?(token) }
  result = available.select{ |e| e.match?(token, fuzzy: true) } if result.empty?
  result.keep_if{ |e| accept? e }
  Matches.new(result, (result.empty? ? '' : token), (result.empty? ? token : ''))
end

#signatureObject



73
74
75
# File 'lib/gamefic/query/base.rb', line 73

def signature
  "#{self.class.to_s.downcase}(#{@arguments.join(',')})"
end