Module: Gibbler::Object

Included in:
Class, Array, Complex, DateTime, File, Hash, Nil, Range, String, Time, Module, OpenStruct, Proc
Defined in:
lib/gibbler.rb,
lib/gibbler/aliases.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gibbler_fieldsObject



129
130
# File 'lib/gibbler.rb', line 129

def self.gibbler_fields
end

.included(obj) ⇒ Object



124
125
126
127
# File 'lib/gibbler.rb', line 124

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

Instance Method Details

#__gibbler(digest_type = nil) ⇒ Object

Creates a digest for the current state of self based on:

  • Object#class

  • Length of Object#name || 0

  • Object#name || ”

e.g. Digest::SHA1.hexdigest “Class:6:Object” #=>

This is a default method appropriate for only the most basic objects like Class and Module.



184
185
186
187
188
189
190
191
# File 'lib/gibbler.rb', line 184

def __gibbler(digest_type=nil)
  klass = self.class
  nom = self.name if self.respond_to?(:name)
  nom ||= ''
  a = Gibbler.digest '%s:%s:%s' % [klass, nom.size, nom], digest_type
  gibbler_debug klass, a, [klass, nom.size, nom]
  a
end

#digest_cacheObject

The cache is in the Attic.



12
# File 'lib/gibbler/aliases.rb', line 12

def    digest_cache;     gibbler_cache;  end

#freezeObject

A simple override on Object#freeze to create a digest before the object is frozen. Once the object is frozen ‘obj.gibbler` will return the cached value with out calculation.



197
198
199
200
201
202
# File 'lib/gibbler.rb', line 197

def freeze()
  gibbler_debug :FREEZE, self.class, caller[0] if Gibbler.debug?
  self.gibbler
  super
  self
end

#gibbled?Boolean Also known as: changed?

Has this object been modified?

This method compares the return value from digest with the previous value returned by gibbler (the value is stored in the attic as ‘gibbler_cache`). See Attic

Returns:

  • (Boolean)


162
163
164
165
166
167
# File 'lib/gibbler.rb', line 162

def gibbled?
  self.gibbler_cache ||= self.gibbler
  was, now = self.gibbler_cache.clone, self.gibbler
  gibbler_debug :gibbled?, was, now
  was != now
end

#gibbler(digest_type = nil) ⇒ Object Also known as: digest

Calculates a digest for the current object instance. Objects that are a kind of Hash or Array are processed recursively. The length of the returned String depends on the digest type. Also stores the value in the attic.

obj.gibbler          # => a5b1191a
obj.gibbler_cache    # => a5b1191a

Calling gibbler_cache returns the most recent digest without calculation.

If the object is frozen, this will return the value of ‘gibbler_cache`.



148
149
150
151
152
153
154
# File 'lib/gibbler.rb', line 148

def gibbler(digest_type=nil)
  #gibbler_debug caller[0]
  gibbler_debug :GIBBLER, self.class, self
  ret = Gibbler::Digest.new self.__gibbler(digest_type)
  self.gibbler_cache = ret unless self.frozen?
  ret
end

#gibbler_debug(*args) ⇒ Object



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

def gibbler_debug(*args)
  return unless Gibbler.debug?
  p args
end

#gibbler_fieldsObject Also known as: digest_fields



131
132
# File 'lib/gibbler.rb', line 131

def gibbler_fields
end