Class: Kookaburra::MentalModel::Collection

Inherits:
SimpleDelegator
  • Object
show all
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.

Examples:

widgets = Kookaburra::MentalModel::Collection.new('widgets')

widgets[:foo] = :a_foo

widgets[:foo]
#=> :a_foo

# Raises an UnknownKeyError
mental_model.widgets[:bar]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, init_data = {}) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • name (String)

    The name of the collection. Used to provide helpful error messages when unknown keys are accessed.

  • init_data (Hash) (defaults to: {})

    Preloads specific data into the 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

#deletedKookaburra::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.

Returns:



123
124
125
# File 'lib/kookaburra/mental_model.rb', line 123

def deleted
  @deleted ||= self.class.new("#{name}.deleted")
end

#nameObject

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.

Returns:

  • (Boolean)


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.

Parameters:

  • key

    the key to delete from the collection

Returns:

  • the value of the deleted key/value pair

Raises:

  • (Kookaburra::UnknownKeyError)

    if the specified key has not been set



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.

Returns:

  • (Hash)

    the key/value pairs still remaining after the deletion



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

#dupObject



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

Note:

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

Parameters:

  • keys (Object)

    The list of keys that should not be copied from the collection

Returns:

  • (Hash)

    The resulting keys/values from the collection



96
97
98
# File 'lib/kookaburra/mental_model.rb', line 96

def except(*keys)
  slice(*(self.keys - keys))
end

#slice(*keys) ⇒ Hash

Note:

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.

Parameters:

  • keys (Object)

    The list of keys that should be copied from the collection

Returns:

  • (Hash)

    The resulting keys/values from the 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