Class: Attentive::CompositeEntity

Inherits:
Entity show all
Defined in:
lib/attentive/composite_entity.rb

Constant Summary

Constants inherited from Entity

Entity::NOMATCH

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Entity

#variable_name

Attributes inherited from Token

#begin

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#==, [], #_value_from_match, #entity?, #name, #nomatch!, published?, #to_s, undefine

Methods inherited from Token

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

Constructor Details

#initialize(*args) ⇒ CompositeEntity

Returns a new instance of CompositeEntity.



21
22
23
24
# File 'lib/attentive/composite_entity.rb', line 21

def initialize(*args)
  super
  @entities = self.class.entities.map { |entity_klass| entity_klass.new(variable_name) }
end

Class Attribute Details

.entitiesObject

Returns the value of attribute entities.



9
10
11
# File 'lib/attentive/composite_entity.rb', line 9

def entities
  @entities
end

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



5
6
7
# File 'lib/attentive/composite_entity.rb', line 5

def entities
  @entities
end

Class Method Details

.define(entity_name, *entities) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/attentive/composite_entity.rb', line 11

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

  create! entity_name do |entity_klass|
    entity_klass.entities = entities.map { |entity| Entity[entity] }
    entity_klass.published = options.fetch(:published, true)
  end
end

Instance Method Details

#matches?(cursor) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/attentive/composite_entity.rb', line 26

def matches?(cursor)
  entities.each do |entity|
    match = entity.matches?(cursor)
    return match if match
  end
  false
end