Module: Gibbler::Hash

Includes:
Object
Included in:
Hash
Defined in:
lib/gibbler.rb

Overview

Creates a digest based on:

  • parse each key, value pair into an Array containing keys: CLASS:KEY:VALUE.__gibbler

    • The gibbler method is called on each element so if it is a Hash or Array etc it will be parsed recursively according to the gibbler method for that class type.

  • Digest the Array of digests

  • Return the digest for class:length:value where:

    • “class” is equal to the current object class (e.g. Hash).

    • “length” is the size of the Array of digests (which should equal the number of keys in the original Hash object).

    • “value” is the Array of digests joined with a colon (“:”).

This method can be used by any class with a keys method.

class MyOrderedHash
  include Gibbler::Hash
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Object

#gibbled?, #gibbler, #gibbler_debug

Class Method Details

.included(obj) ⇒ Object



168
169
170
171
# File 'lib/gibbler.rb', line 168

def self.included(obj)
  obj.extend Attic
  obj.attic :__gibbler_cache
end

Instance Method Details

#__gibbler(h = self) ⇒ Object

Creates a digest for the current state of self.



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gibbler.rb', line 174

def __gibbler(h=self)
  klass = h.class
  d = h.keys.sort { |a,b| a.inspect <=> b.inspect }
  d.collect! do |name| 
    value = h[name]
    '%s:%s:%s' % [value.class, name, value.__gibbler]
  end 
  d = d.join(':').__gibbler
  a = Gibbler.digest '%s:%s:%s' % [klass, d.size, d]
  gibbler_debug klass, a, [klass, d.size, d]
  a  
end