class Attentive::Entity

Constants

NOMATCH

Attributes

phrases[RW]
published[W]
token_name[RW]
variable_name[R]

Public Class Methods

[](entity_name) click to toggle source
# 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) click to toggle source
# File lib/attentive/entity.rb, line 30
def define(entity_name, *phrases, &block)
  options = phrases.last.is_a?(::Hash) ? phrases.pop : {}

  phrase_value_map = {}
  options.each do |phrase, value|
    next if phrase.is_a?(Symbol) # symbols are options, strings are phrases
    options.delete phrase
    phrase_value_map[phrase] = value
    phrases.push phrase
  end

  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)

    if block_given?
      entity_klass.send :define_method, :_value_from_match, &block
    else
      entity_klass.send :define_method, :_value_from_match do |match|
        phrase_value_map.fetch(match.to_s, match.to_s)
      end
    end
  end
end
entities() click to toggle source
# File lib/attentive/entity.rb, line 19
def entities
  @entities.values.select(&:published?)
end
new(variable_name=self.class.token_name, pos=0) click to toggle source
Calls superclass method
# File lib/attentive/entity.rb, line 85
def initialize(variable_name=self.class.token_name, pos=0)
  @variable_name = variable_name.to_s
  super pos
end
published?() click to toggle source
# File lib/attentive/entity.rb, line 15
def published?
  @published
end
undefine(entity_name) click to toggle source
# File lib/attentive/entity.rb, line 57
def undefine(entity_name)
  entity_symbol = entity_name.to_sym
  unregister! entity_symbol
end

Protected Class Methods

create!(entity_name) { |entity_klass| ... } click to toggle source
# File lib/attentive/entity.rb, line 64
def create!(entity_name)
  entity_symbol = entity_name.to_sym
  entity_klass = Class.new(self)
  entity_klass.token_name = entity_symbol
  Entity.register! entity_symbol, entity_klass
  yield entity_klass
end
register!(entity_name, entity_klass) click to toggle source
# File lib/attentive/entity.rb, line 72
def register!(entity_name, entity_klass)
  raise ArgumentError, "Entity #{entity_name.inspect} has already been defined" if @entities.key?(entity_name)
  @entities[entity_name] = entity_klass
end
unregister!(entity_name) click to toggle source
# File lib/attentive/entity.rb, line 77
def unregister!(entity_name)
  @entities.delete entity_name
end

Public Instance Methods

==(other) click to toggle source
# File lib/attentive/entity.rb, line 94
def ==(other)
  self.class == other.class && self.variable_name == other.variable_name
end
_value_from_match(match) click to toggle source
# File lib/attentive/entity.rb, line 125
def _value_from_match(match)
  match.to_s
end
entity?() click to toggle source
# File lib/attentive/entity.rb, line 106
def entity?
  true
end
matches?(cursor) click to toggle source
# File lib/attentive/entity.rb, line 110
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
name() click to toggle source
# File lib/attentive/entity.rb, line 90
def name
  self.class.token_name
end
nomatch!() click to toggle source
# File lib/attentive/entity.rb, line 129
def nomatch!
  throw NOMATCH
end
to_s() click to toggle source
# File lib/attentive/entity.rb, line 98
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