Class: Attentive::Entity

Inherits:
Token
  • Object
show all
Defined in:
lib/attentive/entity.rb

Direct Known Subclasses

CompositeEntity

Constant Summary collapse

NOMATCH =
:nomatch.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Token

#begin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Token

#ambiguous?, #end, #eof?, #inspect, #skippable?, #whitespace?

Constructor Details

#initialize(variable_name = self.class.token_name, pos = 0) ⇒ Entity



70
71
72
73
# File 'lib/attentive/entity.rb', line 70

def initialize(variable_name=self.class.token_name, pos=0)
  @variable_name = variable_name.to_s
  super pos
end

Class Attribute Details

.phrasesObject

Returns the value of attribute phrases.



11
12
13
# File 'lib/attentive/entity.rb', line 11

def phrases
  @phrases
end

.published=(value) ⇒ Object (writeonly)

Sets the attribute published



13
14
15
# File 'lib/attentive/entity.rb', line 13

def published=(value)
  @published = value
end

.token_nameObject

Returns the value of attribute token_name.



12
13
14
# File 'lib/attentive/entity.rb', line 12

def token_name
  @token_name
end

Instance Attribute Details

#variable_nameObject (readonly)

Returns the value of attribute variable_name.



6
7
8
# File 'lib/attentive/entity.rb', line 6

def variable_name
  @variable_name
end

Class Method Details

.[](entity_name) ⇒ Object



23
24
25
26
27
28
# File 'lib/attentive/entity.rb', line 23

def [](entity_name)
  entity_name = entity_name.to_sym
  @entities.fetch(entity_name)
rescue KeyError
  raise Attentive::UndefinedEntityError.new("Undefined Entity #{entity_name.inspect}")
end

.define(entity_name, *phrases, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/attentive/entity.rb', line 30

def define(entity_name, *phrases, &block)
  options = phrases.last.is_a?(::Hash) ? phrases.pop : {}

  create! entity_name do |entity_klass|
    entity_klass.phrases = phrases.map do |phrase|
      Attentive::Tokenizer.tokenize(phrase, entities: true, regexps: true)
    end
    entity_klass.published = options.fetch(:published, true)
    entity_klass.send :define_method, :_value_from_match, &block if block_given?
  end
end

.entitiesObject



19
20
21
# File 'lib/attentive/entity.rb', line 19

def entities
  @entities.values.select(&:published?)
end

.published?Boolean



15
16
17
# File 'lib/attentive/entity.rb', line 15

def published?
  @published
end

.undefine(entity_name) ⇒ Object



42
43
44
45
# File 'lib/attentive/entity.rb', line 42

def undefine(entity_name)
  entity_symbol = entity_name.to_sym
  unregister! entity_symbol
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
# File 'lib/attentive/entity.rb', line 79

def ==(other)
  self.class == other.class && self.variable_name == other.variable_name
end

#_value_from_match(match) ⇒ Object



110
111
112
# File 'lib/attentive/entity.rb', line 110

def _value_from_match(match)
  match.to_s
end

#entity?Boolean



91
92
93
# File 'lib/attentive/entity.rb', line 91

def entity?
  true
end

#matches?(cursor) ⇒ Boolean



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/attentive/entity.rb', line 95

def matches?(cursor)
  self.class.phrases.each do |phrase|
    catch NOMATCH do
      cursor_copy = cursor.new_from_here
      match = Attentive::Matcher.new(phrase, cursor_copy).match!
      if match
        value = _value_from_match(match) # <-- might throw
        cursor.advance cursor_copy.pos
        return { variable_name => value }
      end
    end
  end
  false
end

#nameObject



75
76
77
# File 'lib/attentive/entity.rb', line 75

def name
  self.class.token_name
end

#nomatch!Object



114
115
116
# File 'lib/attentive/entity.rb', line 114

def nomatch!
  throw NOMATCH
end

#to_sObject



83
84
85
86
87
88
89
# File 'lib/attentive/entity.rb', line 83

def to_s
  if variable_name.to_s == self.class.token_name.to_s
    "{{#{self.class.token_name}}}"
  else
    "{{#{variable_name}:#{self.class.token_name}}}"
  end
end