Class: Wongi::Engine::EntityIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/wongi-engine/entity_iterator.rb

Instance Method Summary collapse

Constructor Details

#initialize(entity, collection) ⇒ EntityIterator

Returns a new instance of EntityIterator.



7
8
9
10
# File 'lib/wongi-engine/entity_iterator.rb', line 7

def initialize(entity, collection)
  @entity = entity
  @collection = collection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Raises:

  • (NoMethodError)


50
51
52
53
# File 'lib/wongi-engine/entity_iterator.rb', line 50

def method_missing(name)
  each { |k, v| return v if k == name }
  raise NoMethodError
end

Instance Method Details

#[](name) ⇒ Object



31
32
33
# File 'lib/wongi-engine/entity_iterator.rb', line 31

def [](name)
  get(name)
end

#eachObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wongi-engine/entity_iterator.rb', line 12

def each
  if block_given?
    @collection.each do |wme|
      yield wme.predicate, wme.object
    end
  else
    Enumerator.new do |y|
      @collection.each do |wme|
        y << [wme.predicate, wme.object]
      end
    end
  end
end

#fetch(name, *args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/wongi-engine/entity_iterator.rb', line 39

def fetch(name, *args, &block)
  each { |k, v| return v if k == name }
  if args.first
    args.first
  elsif block
    block.call(name)
  else
    raise KeyError
  end
end

#get(name) ⇒ Object



26
27
28
29
# File 'lib/wongi-engine/entity_iterator.rb', line 26

def get(name)
  each { |k, v| return v if k == name }
  nil
end

#get_all(name) ⇒ Object



35
36
37
# File 'lib/wongi-engine/entity_iterator.rb', line 35

def get_all(name)
  each.filter_map { |k, v| v if k == name }
end

#respond_to_missing?(name, _include_private) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/wongi-engine/entity_iterator.rb', line 55

def respond_to_missing?(name, _include_private)
  each { |k, v| return true if k == name }
  super
end