Class: Gamefic::Query::Base

Inherits:
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

Determine whether the specified entity passes the query’s arguments.

Returns:

  • (Boolean)


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

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

#ambiguous?Boolean

Determine whether the query allows ambiguous entity references. If false, actions that use this query will only be valid if the token passed into it resolves to a single entity. If true, actions will accept an array of matching entities instead. Queries are not ambiguous by default (ambiguous? == false).

Returns:

  • (Boolean)


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

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:



27
28
29
# File 'lib/gamefic/query/base.rb', line 27

def context_from(subject)
  []
end

#include?(subject, object) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/gamefic/query/base.rb', line 52

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

#precisionInteger Also known as: rank

A ranking of how precise the query’s arguments are.

Query precision is a factor in calculating Action#rank.

Returns:

  • (Integer)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gamefic/query/base.rb', line 63

def precision
  if @precision.nil?
    @precision = 1
    arguments.each { |a|
      if a.is_a?(Class)
        @precision += 100
      elsif a.is_a?(Gamefic::Entity)
        @precision += 1000
      end
    }
    @precision
  end
  @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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gamefic/query/base.rb', line 35

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.specified?(token) }
  result = available.select{ |e| e.specified?(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.split('::').last.downcase}(#{simplify_arguments.join(', ')})"
end