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 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.
-
#deleted ⇒ Kookaburra::MentalModel::Collection
Finds or initializes, and returns, the subcollection of deleted items.
- #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 = nil) ⇒ 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 = nil) ⇒ Collection
Returns a new instance of Collection.
54 55 56 57 58 59 60 61 |
# File 'lib/kookaburra/mental_model.rb', line 54 def initialize(name, init_data = nil) @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) unless init_data.nil? super(data) end |
Instance Method Details
#===(other) ⇒ Boolean
Unlike a Hash, this object is only identical to another if the actual
#object_id
attributes match.
67 68 69 |
# File 'lib/kookaburra/mental_model.rb', line 67 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
.
110 111 112 113 |
# File 'lib/kookaburra/mental_model.rb', line 110 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
.
132 133 134 135 |
# File 'lib/kookaburra/mental_model.rb', line 132 def delete_if(&block) move = lambda { |k,v| deleted[k] = v; true } super { |k,v| block.call(k,v) && move.call(k,v) } end |
#deleted ⇒ Kookaburra::MentalModel::Collection
Finds or initializes, and returns, the subcollection of deleted items
Key/value pairs #delete
d from a collection on the MentalModel will be added
to this subcollection.
121 122 123 |
# File 'lib/kookaburra/mental_model.rb', line 121 def deleted @deleted ||= self.class.new("deleted") end |
#dup ⇒ Object
137 138 139 140 141 |
# File 'lib/kookaburra/mental_model.rb', line 137 def dup new_data = {}.merge(self) new_data = Marshal.load(Marshal.dump(new_data)) self.class.new(@name, new_data) 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
94 95 96 |
# File 'lib/kookaburra/mental_model.rb', line 94 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.
79 80 81 82 83 84 |
# File 'lib/kookaburra/mental_model.rb', line 79 def slice(*keys) data = keys.inject({}) { |memo, key| memo[key] = self[key] memo } end |