Skip to content

Commit

Permalink
ADDED: Gibbler::Object#freeze to create digest before freezing.
Browse files Browse the repository at this point in the history
  • Loading branch information
delano committed Oct 7, 2009
1 parent c33f917 commit 961896c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
11 changes: 11 additions & 0 deletions CHANGES.txt
@@ -1,6 +1,17 @@
GIBBLER, CHANGES


#### 0.6.4 (2009-10-??) #################################

* FIXED: Now using correct superclass for DateTime (Date)
* CHANGE: aliases.rb will now require gibbler so you don't need to specify both.
i.e. require 'gibbler'
require 'gibbler/aliases'
* CHANGE: Gibbler::Object#gibbler returns the value of gibbler_cache when
the object is frozen, without calculation.
* ADDED: Gibbler::Object#freeze to create digest before freezing.


#### 0.6.3 (2009-09-30) #################################

* FIXED: Won't save digest to cache if the object is frozen
Expand Down
24 changes: 20 additions & 4 deletions lib/gibbler/object.rb
Expand Up @@ -14,12 +14,21 @@ def self.included(obj)
# 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.
# 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
# <tt>gibbler_cache</tt>.
#
def gibbler
gibbler_debug :GIBBLER, self.class, self
digest = Gibbler::Digest.new self.__gibbler
self.gibbler_cache = digest unless self.frozen?
digest
return self.gibbler_cache if self.frozen?
self.gibbler_cache = Gibbler::Digest.new self.__gibbler
end

# Has this object been modified?
Expand Down Expand Up @@ -57,6 +66,13 @@ def __gibbler(h=self)
gibbler_debug klass, a, [klass, nom.size, nom]
a
end

# A simple override on Object#freeze to create a digest
# before the object is frozen. Once the object is frozen
# <tt>obj.gibbler</tt> will return the cached value with
# out calculation.
def freeze() self.gibbler; super; self end

end

end
11 changes: 10 additions & 1 deletion tryouts/10_basic_tryouts.rb
Expand Up @@ -39,7 +39,7 @@
1
end

dream :gibbler, '259afadb4ef8abaeb367db97d0c3015c8a4a504a'
dream :gibbler, '608256db120251843843bba57e9b2c7adb7342aa'
drill "Bignum instance" do
100000000000
end
Expand Down Expand Up @@ -103,6 +103,15 @@
Hash.attic_vars
end

drill "Freezing an object will update the digest", true do
a = { :a => 1 }
pre = a.gibbler;
a[:a] = 2
post = a.freeze.gibbler
stash :pre, pre
stash :post, post
pre != post && post == a.gibbler_cache
end

dream :gibbler, "6ea546919dc4caa2bab69799b71d48810a1b48fa"
drill "works on arbitrary objects" do
Expand Down
2 changes: 1 addition & 1 deletion tryouts/15_file_tryouts.rb
Expand Up @@ -24,7 +24,7 @@
path = File.join(File.dirname(__FILE__), '..', 'README.rdoc')
File.new(File.expand_path(path))
end
def freeze() super; @path.freeze; self end

## JRuby doesn't like to use File.new with directories
##dream :gibbler, '92cbcb7de73d7748b28d9e911f461013de34410f'
##drill "File gibbler cares about trailing slash (/tmp/)", File.new(__FILE__)
Expand Down

0 comments on commit 961896c

Please sign in to comment.