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



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/gibbler.rb', line 289

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.push *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(digest_type = nil) ⇒ Object

Creates a digest for the current state of self.



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/gibbler.rb', line 322

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

#__gibbler_revert!Object



340
341
342
343
344
345
346
# File 'lib/gibbler.rb', line 340

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



311
312
313
314
315
316
317
318
319
# File 'lib/gibbler.rb', line 311

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