Module: Treat::Entities::Entity::Iterable

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

Instance Method Summary collapse

Instance Method Details

#ancestor_with_feature(feature) ⇒ Object

Returns the first ancestor that has a feature with the given name, otherwise nil.



90
91
92
93
94
# File 'lib/treat/entities/entity/iterable.rb', line 90

def ancestor_with_feature(feature)
  each_ancestor do |ancestor|
    return ancestor if ancestor.has?(feature)
  end
end

#ancestor_with_type(type) ⇒ Object

Returns the first ancestor of this entity that has the given type.



57
58
59
60
61
62
63
64
65
66
# File 'lib/treat/entities/entity/iterable.rb', line 57

def ancestor_with_type(type)
  return unless has_parent?
  ancestor = @parent
  type_klass = Treat::Entities.const_get(type.cc)
  while not ancestor.is_a?(type_klass)
    return nil unless (ancestor && ancestor.has_parent?)
    ancestor = ancestor.parent
  end
  ancestor
end

#ancestors_with_type(type) ⇒ Object

Returns an array of ancestors of this entity that have the given type.



80
81
82
83
84
85
86
# File 'lib/treat/entities/entity/iterable.rb', line 80

def ancestors_with_type(type)
  ancestors = []
  each_ancestor(type) do |a| 
    ancestors << a
  end
  ancestors
end

#each_ancestor(type = :entity) ⇒ Object

Yields each ancestors of this entity that has the given type.



70
71
72
73
74
75
76
# File 'lib/treat/entities/entity/iterable.rb', line 70

def each_ancestor(type = :entity)
  ancestor = self
  while (a = ancestor.ancestor_with_type(type))
    yield a
    ancestor = ancestor.parent
  end
end

#each_entity(*types) {|_self| ... } ⇒ Object

Yields each entity of any of the supplied types in the children tree of this Entity. Note that this function is recursive, unlike #each. It does not yield the top element being recursed.

This function NEEDS to be ported to C. #FIXME

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/treat/entities/entity/iterable.rb', line 10

def each_entity(*types)
  types = [:entity] if types.size == 0
  f = false
  types.each do |t2|
    if is_a?(Treat::Entities.const_get(t2.cc))
      f = true; break
    end
  end
  yield self if f
  unless @children.size == 0
    # return unless contains_types?(types)
    @children.each do |child|
      child.each_entity(*types) { |y| yield y }
    end
  end
end

#entities_with_category(category, type = nil) ⇒ Object

Returns an array of the entities with the given category.



51
52
53
# File 'lib/treat/entities/entity/iterable.rb', line 51

def entities_with_category(category, type = nil)
  entities_with_feature(:category, category, type)
end

#entities_with_feature(feature, value, type = nil) ⇒ Object

Returns an array of the children that have a feature equal to value within the entities of the given type.



29
30
31
32
33
34
35
36
37
# File 'lib/treat/entities/entity/iterable.rb', line 29

def entities_with_feature(feature, value, type = nil)
  a = []
  type = :entity unless type
  each_entity(type) do |e|
    r = e.get(feature)
    a << e if r == value
  end
  a
end

#entities_with_types(*types) ⇒ Object Also known as: entities_with_type

Returns an array of the children that have a type within the supplied types.



41
42
43
44
45
# File 'lib/treat/entities/entity/iterable.rb', line 41

def entities_with_types(*types)
  a = []
  each_entity(*types) { |e| a << e }
  a
end

#num_children_with_feature(feature, value = nil, recursive = true) ⇒ Object

Number of children that have a given feature. Second variable to allow for passing value to check for.



98
99
100
101
102
103
104
105
106
107
# File 'lib/treat/entities/entity/iterable.rb', line 98

def num_children_with_feature(feature, value = nil, recursive = true)
  i = 0
  m = method(recursive ? :each_entity : :each)
  m.call do |c|
    next unless c.has?(feature)
    i += (value == nil ? 1 : 
    (c.get(feature) == value ? 1 : 0))
  end
  i
end