Module: Gibbler::Complex

Includes:
Object
Defined in:
lib/gibbler.rb

Overview

Creates a digest based on:

  • An Array of instance variable names and values in the format: CLASS:LENGTH:VALUE

    • 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. FullHouse).

    • “length” is the size of the Array of digests (which should equal the number of instance variables in the object).

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

This method can be used by any class which stores values in instance variables.

class Episodes
  include Gibbler::Complex
  attr_accessor :season, :year, :cast
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Object

#digest_cache, #freeze, #gibbled?, #gibbler, #gibbler_debug, gibbler_fields

Class Method Details

.included(obj) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/gibbler.rb', line 243

def self.included(obj)
  obj.extend Attic
  obj.attic :gibbler_cache
  obj.class_eval do
    @__gibbler_fields = []
    def self.gibbler_fields
      @__gibbler_fields
    end
    def self.gibbler *fields
      @__gibbler_fields = *fields
    end
    def self.inherited(obj)
      obj.extend Attic
      obj.attic :gibbler_cache
      fields = @__gibbler_fields.clone
      obj.class_eval do
        @__gibbler_fields = fields
      end
    end
  end
end

Instance Method Details

#__gibbler(h = self) ⇒ Object

Creates a digest for the current state of self.



276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/gibbler.rb', line 276

def __gibbler(h=self)
  klass = h.class
  d = []
  gibbler_debug :gibbler_fields, gibbler_fields
  gibbler_fields.each do |n|
    value = instance_variable_get("@#{n}")
    d << '%s:%s:%s' % [value.class, n, value.__gibbler]
  end
  d = d.join(':').__gibbler
  a = Gibbler.digest "%s:%d:%s" % [klass, d.size, d]
  gibbler_debug klass, a, [klass, d.size, d]
  a
end

#__gibbler_revert!Object



290
291
292
293
294
295
296
# File 'lib/gibbler.rb', line 290

def __gibbler_revert!
  state = self.gibbler_object self.gibbler_cache
  state.instance_variables do |n|
    v = state.instance_variable_get n
    self.instance_variable_set v
  end
end

#gibbler_fieldsObject



265
266
267
268
269
270
271
272
273
# File 'lib/gibbler.rb', line 265

def gibbler_fields
  f = self.class.gibbler_fields
  if f.empty?
    f = instance_variables.sort.collect { |n|
      n.to_s[1..-1].to_sym # remove the '@'
    }
  end
  f
end