Module: Treat::Entities::Entity::Countable

Included in:
Treat::Entities::Entity
Defined in:
lib/treat/entities/entity/countable.rb

Instance Method Summary collapse

Instance Method Details

#count(type) ⇒ Object

Get the number of children with a type in this entity.



37
38
39
# File 'lib/treat/entities/entity/countable.rb', line 37

def count(type)
  @registry[:type][type].size
end

#frequency_in(parent_type = nil) ⇒ Object Also known as: frequency

Find the frequency of the entity in the supplied parent or in the root node if nil.



23
24
25
26
27
28
29
# File 'lib/treat/entities/entity/countable.rb', line 23

def frequency_in(parent_type = nil)
  unless parent_type
    root.registry[:value][id]
  end

  registry(parent_type)[:value][value]
end

#frequency_of(value) ⇒ Object

Returns the frequency of the given value in the this entity.



43
44
45
46
47
48
49
50
51
52
# File 'lib/treat/entities/entity/countable.rb', line 43

def frequency_of(value)
  value = value.downcase
  if is_a?(Treat::Entities::Token)
    raise Treat::Exception,
    "Cannot get the frequency " +
    "of something within a leaf."
  end
  tv = @registry[:value][value]
  tv ? tv : 0
end

#positionObject

Find the position of the current entity inside the parent entity, starting at 1.



5
6
7
8
9
10
11
# File 'lib/treat/entities/entity/countable.rb', line 5

def position
  unless has_parent?
    raise Treat::Exception,
    "No parent to get position in."
  end
  parent.children.index(self)
end

#position_from_endObject

Find the position of this entity from the end of the parent entity.



15
16
17
18
# File 'lib/treat/entities/entity/countable.rb', line 15

def position_from_end
  p = position
  parent.children.size - p
end