Class: Munge::System::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/munge/system/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(item_factory:, items:) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
11
12
13
14
15
# File 'lib/munge/system/collection.rb', line 6

def initialize(item_factory:,
               items:)
  @item_factory = item_factory

  @items =
    items
      .map { |item| parse(**item) }
      .map { |item| [item.id, item] }
      .to_h
end

Instance Method Details

#[](id) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/munge/system/collection.rb', line 38

def [](id)
  found_item = @items[id]

  if found_item.nil?
    raise "item not found (#{id})"
  end

  found_item
end

#build(**args) ⇒ Object



17
18
19
# File 'lib/munge/system/collection.rb', line 17

def build(**args)
  @item_factory.build(**prune_args(args))
end

#eachObject



25
26
27
28
29
30
31
# File 'lib/munge/system/collection.rb', line 25

def each
  return enum_for(:each) unless block_given?

  @items.each_value do |item|
    yield item
  end
end

#parse(**args) ⇒ Object



21
22
23
# File 'lib/munge/system/collection.rb', line 21

def parse(**args)
  @item_factory.parse(**prune_args(args))
end

#push(virtual_item) ⇒ Object



33
34
35
36
# File 'lib/munge/system/collection.rb', line 33

def push(virtual_item)
  key = virtual_item.id
  @items[key] = virtual_item
end