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 Method Summary collapse

Constructor Details

#initialize(name, init_data = nil) ⇒ 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: nil)

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

Returns:

  • (Boolean)


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.

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



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.

Returns:

  • (Hash)

    the key/value pairs still remaining after the deletion



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

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



121
122
123
# File 'lib/kookaburra/mental_model.rb', line 121

def deleted
  @deleted ||= self.class.new("deleted")
end

#dupObject



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

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



94
95
96
# File 'lib/kookaburra/mental_model.rb', line 94

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



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