Class: Gamefic::Query::Base

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

Direct Known Subclasses

Children, External, 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)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gamefic/query/base.rb', line 83

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)


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

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

#precisionObject



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

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

      if a.kind_of?(Class)
        @precision += 100
      elsif a.kind_of?(Gamefic::Entity)
        @precision += 1000
      end
    }
    @precision
  end
  @precision
end

#rankObject



75
76
77
# File 'lib/gamefic/query/base.rb', line 75

def rank
  precision
end

#resolve(subject, token, continued: false) ⇒ Gamefic::Query::Matches

Get a collection of objects that exist in the subject’s context and match the provided token. The result is provided as a Matches object.



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

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



79
80
81
# File 'lib/gamefic/query/base.rb', line 79

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