Class: KuberKit::Core::Store
Constant Summary collapse
- NotFoundError =
Class.new(KuberKit::NotFoundError)
- AlreadyAddedError =
Class.new(KuberKit::Error)
Instance Attribute Summary collapse
-
#object_class_name ⇒ Object
readonly
Returns the value of attribute object_class_name.
Instance Method Summary collapse
- #add(item_name, item) ⇒ Object
- #exists?(name) ⇒ Boolean
- #get(item_name) ⇒ Object
-
#initialize(object_class_name) ⇒ Store
constructor
A new instance of Store.
- #items ⇒ Object
- #reset! ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(object_class_name) ⇒ Store
Returns a new instance of Store.
7 8 9 |
# File 'lib/kuber_kit/core/store.rb', line 7 def initialize(object_class_name) @object_class_name = object_class_name end |
Instance Attribute Details
#object_class_name ⇒ Object (readonly)
Returns the value of attribute object_class_name.
5 6 7 |
# File 'lib/kuber_kit/core/store.rb', line 5 def object_class_name @object_class_name end |
Instance Method Details
#add(item_name, item) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/kuber_kit/core/store.rb', line 11 def add(item_name, item) unless item.is_a?(object_class_name) raise ArgumentError.new("#{self.object_class_name}: should be an instance of #{object_class_name}, got: #{item.inspect}") end unless items[item_name].nil? raise AlreadyAddedError, "#{self.object_class_name}: item with name #{item_name} was already added" end items[item_name] = item end |
#exists?(name) ⇒ Boolean
45 46 47 |
# File 'lib/kuber_kit/core/store.rb', line 45 def exists?(name) !items[name].nil? end |
#get(item_name) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/kuber_kit/core/store.rb', line 23 def get(item_name) item = items[item_name] if item.nil? raise NotFoundError, "#{self.object_class_name}: item '#{item_name}' not found" end item end |
#items ⇒ Object
33 34 35 |
# File 'lib/kuber_kit/core/store.rb', line 33 def items @items ||= {} end |
#reset! ⇒ Object
37 38 39 |
# File 'lib/kuber_kit/core/store.rb', line 37 def reset! @items = {} end |
#size ⇒ Object
41 42 43 |
# File 'lib/kuber_kit/core/store.rb', line 41 def size items.count end |