class Jisota::Collection
Stores items with a `:key` attribute In Jisota
, this is used for collections of `Package`, `Role` and `Server`
Unlike most ruby enumerables, errors are raised when adding duplicate item or getting non-existent item.
Attributes
items[R]
Public Class Methods
new(items = {})
click to toggle source
# File lib/jisota/collection.rb, line 22 def initialize(items = {}) @items = items end
Public Instance Methods
[](key)
click to toggle source
# File lib/jisota/collection.rb, line 33 def [](key) items.fetch(key) { raise KeyNotFoundError, "The key #{key.inspect} was not found in the collection" } end
add(item_with_key)
click to toggle source
# File lib/jisota/collection.rb, line 26 def add(item_with_key) key = item_with_key.key raise DuplicateKeyError, "Collection already contains an item with key #{key.inspect}" if items.has_key?(key) items[key] = item_with_key end
Also aliased as: <<
each(&block)
click to toggle source
# File lib/jisota/collection.rb, line 41 def each(&block) @items.values.each(&block) end
first()
click to toggle source
# File lib/jisota/collection.rb, line 37 def first @items.first[1] end
merge(other, &block)
click to toggle source
# File lib/jisota/collection.rb, line 45 def merge(other, &block) Collection.new(items.merge(other.items, &block)) end