Class: Kookaburra::MentalModel::Collection
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Kookaburra::MentalModel::Collection
- Defined in:
- lib/kookaburra/mental_model.rb
Overview
A MentalModel::Collection behaves much like a Hash object, with the exception that it will raise an UnknownKeyError rather than return nil if you attempt to access a key that has not been set. The exception attempts to provide a more helpful error message.
Instance Attribute Summary collapse
-
#deleted ⇒ Kookaburra::MentalModel::Collection
Finds or initializes, and returns, the subcollection of deleted items.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#===(other) ⇒ Boolean
Unlike a Hash, this object is only identical to another if the actual #object_id attributes match.
-
#delete(key, &block) ⇒ Object
Deletes a key/value pair from the collection, and persists the deleted pair in a subcollection.
-
#delete_if(&block) ⇒ Hash
Deletes key/value pairs from the collection for which the given block evaluates to true, and persists all deleted pairs in a subcollection.
- #dup ⇒ Object
-
#except(*keys) ⇒ Hash
Returns a new hash that contains every key/value from this collection except for the specified keys.
-
#initialize(name, init_data = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#slice(*keys) ⇒ Hash
Returns a new hash that contains key/value pairs for the specified keys with values copied from this collection.
Constructor Details
#initialize(name, init_data = {}) ⇒ Collection
Returns a new instance of Collection.
56 57 58 59 60 61 62 63 |
# File 'lib/kookaburra/mental_model.rb', line 56 def initialize(name, init_data = {}) self.name = name data = Hash.new do |hash, key| raise UnknownKeyError, "Can't find mental_model.#{@name}[#{key.inspect}]. Did you forget to set it?" end data.merge!(init_data) super(data) end |
Instance Attribute Details
#deleted ⇒ Kookaburra::MentalModel::Collection
Finds or initializes, and returns, the subcollection of deleted items
Key/value pairs #deleted from a collection on the MentalModel will be added to this subcollection.
123 124 125 |
# File 'lib/kookaburra/mental_model.rb', line 123 def deleted @deleted ||= self.class.new("#{name}.deleted") end |
#name ⇒ Object
Returns the value of attribute name.
51 52 53 |
# File 'lib/kookaburra/mental_model.rb', line 51 def name @name end |
Instance Method Details
#===(other) ⇒ Boolean
Unlike a Hash, this object is only identical to another if the actual #object_id attributes match.
69 70 71 |
# File 'lib/kookaburra/mental_model.rb', line 69 def ===(other) self.object_id == other.object_id end |
#delete(key, &block) ⇒ Object
Deletes a key/value pair from the collection, and persists the deleted pair in a subcollection.
Deleting a key/value pair from a collection on the MentalModel works just like Hash#delete but with a side effect - deleted members are added to a subcollection, accessible at #deleted.
112 113 114 115 |
# File 'lib/kookaburra/mental_model.rb', line 112 def delete(key, &block) self[key] # simple fetch to possibly trigger UnknownKeyError deleted[key] = super end |
#delete_if(&block) ⇒ Hash
Deletes key/value pairs from the collection for which the given block evaluates to true, and persists all deleted pairs in a subcollection.
Works just like Hash#delete_if but with a side effect - deleted members are added to a subcollection, accessible at #deleted.
134 135 136 137 |
# File 'lib/kookaburra/mental_model.rb', line 134 def delete_if(&block) move = ->(key, value) { deleted[key] = value; true } super { |key, value| block.call(key, value) && move.call(key, value) } end |
#dup ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/kookaburra/mental_model.rb', line 139 def dup new_data = {}.merge(self) new_data = Marshal.load(Marshal.dump(new_data)) self.class.new(@name, new_data).tap do |mm| mm.deleted = deleted.dup unless deleted.empty? end end |
#except(*keys) ⇒ Hash
This is semantically the same as Hash#except as provided by ActiveSupport::CoreExt::Hash
Returns a new hash that contains every key/value from this collection except for the specified keys
96 97 98 |
# File 'lib/kookaburra/mental_model.rb', line 96 def except(*keys) slice(*(self.keys - keys)) end |
#slice(*keys) ⇒ Hash
This is semantically the same as Hash#slice as provided by ActiveSupport::CoreExt::Hash
Returns a new hash that contains key/value pairs for the specified keys with values copied from this collection.
81 82 83 84 85 86 |
# File 'lib/kookaburra/mental_model.rb', line 81 def slice(*keys) data = keys.inject({}) { |memo, key| memo[key] = self[key] memo } end |