Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Gibbler::Simple

  • Loading branch information...
commit b23052ceceb3b151b68c946ee28c6f5732d2fd01 1 parent 0133d51
@delano authored
View
2  CHANGES.txt
@@ -8,7 +8,7 @@ GIBBLER, CHANGES
* CHANGE: Removed Gibbler.enable_debug/disable_debug
* CHANGE: Gibbler.digest now returns nil for an empty Array
* ADDED: Gibbler.delimiter
-
+* ADDED: Gibbler::Simple
#### 0.8.10 (2011-10-23) ###############################
View
61 lib/gibbler.rb
@@ -301,6 +301,64 @@ def self.included(obj)
end
# Creates a digest based on:
+ module Simple
+ include Gibbler::Object
+ 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
+
+ def gibbler_fields
+ [self.class.gibbler_fields].compact.flatten
+ end
+
+ # Creates a digest for the current state of self.
+ def __gibbler(digest_type=nil)
+ klass = self.class
+ d = []
+ gibbler_debug :gibbler_fields, gibbler_fields
+ gibbler_fields.each do |n|
+ value = respond_to?(n) ? send(n) : 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.delimiter).__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
+
+
+ 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
+ end
+
+ # Creates a digest based on:
# * An Array of instance variable names and values in the format: <tt>CLASS:LENGTH:VALUE</tt>
# * 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.
@@ -319,7 +377,7 @@ def self.included(obj)
# end
#
module Complex
- include Gibbler::Object
+ include Gibbler::Simple
def self.included(obj)
obj.extend Attic
@@ -379,7 +437,6 @@ def __gibbler_revert!
self.instance_variable_set v
end
end
-
end
# Creates a digest based on: <tt>CLASS:LENGTH:VALUE</tt>.
View
28 try/13_simple_object_try.rb
@@ -0,0 +1,28 @@
+require 'gibbler/mixins'
+require 'time'
+
+# arbitrary objects can specify instance variables to gibbler
+ class ::SimpleTest
+ include Gibbler::Simple
+ attr_accessor :roles
+ attr_accessor :ready
+ gibbler :roles
+ end
+ a = SimpleTest.new
+ a.roles = [:jesse, :joey, :danny, :kimmy, :michelle, :dj, :stephanie]
+ a.ready = true
+ a.gibbler
+#=> "b0a659d02e854721f2c865f4eaaae97bfdeda33b"
+
+# arbitrary objects can append more instance variables later on
+ class ::SimpleTest
+ gibbler :stamp, :ready
+ def stamp
+ Time.parse('2009-08-25 16:43:53 UTC')
+ end
+ end
+ a = SimpleTest.new
+ a.roles = [:jesse, :joey, :danny, :kimmy, :michelle, :dj, :stephanie]
+ a.ready = true
+ a.gibbler
+#=> "5daf07b7aae043d0ea6b079a630e034dad7f5982"
View
1  try/17_complex_object_try.rb
@@ -1,4 +1,5 @@
require 'gibbler/mixins'
+require 'time'
# arbitrary objects can specify instance variables to gibbler
class ::FullHouse
Please sign in to comment.
Something went wrong with that request. Please try again.