Permalink
Browse files

Gibbler.delimiter

  • Loading branch information...
1 parent 6eaad3f commit b04eae80cbd52df9c3729120192ffd4cadcb9dcb @delano committed Apr 20, 2012
Showing with 15 additions and 5 deletions.
  1. +10 −5 lib/gibbler.rb
  2. +5 −0 try/10_standalone_try.rb
View
@@ -240,6 +240,7 @@ class Gibbler < String
@debug = false
@digest_type = ::Digest::SHA1
+ @delimiter = ':'
class << self
# Specify a different digest class. The default is +Digest::SHA1+. You
@@ -248,6 +249,10 @@ class << self
# Object.digest_type = Digest::SHA256
#
attr_accessor :digest_type
+ # The delimiter to use when joining Array values before creating a
+ # new digest hash. The default is ":".
+ attr_accessor :delimiter
+ # Set to true for debug output (including all digest inputs)
attr_accessor :debug
# Returns the current debug status (true or false)
def debug?; @debug != false; end
@@ -262,9 +267,9 @@ def debug?; @debug != false; end
#
# See: digest_type
def self.digest(input, digest_type=nil)
- input = input.flatten.collect(&:to_s).join(':') if ::Array === input
+ input = input.flatten.collect(&:to_s).join(delimiter) if ::Array === input
digest_type ||= @digest_type
- input = [Gibbler.secret, input].join(':') unless Gibbler.secret.nil?
+ input = [Gibbler.secret, input].join(delimiter) unless Gibbler.secret.nil?
dig = digest_type.hexdigest(input)
dig = dig.to_i(16).to_s(Gibbler.default_base) if 16 != Gibbler.default_base
dig
@@ -350,7 +355,7 @@ def __gibbler(digest_type=nil)
end
d << '%s:%s:%s' % [value.class, n, value.__gibbler(digest_type)]
end
- d = d.join(':').__gibbler(digest_type)
+ 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
@@ -436,7 +441,7 @@ def __gibbler(digest_type=nil)
end
'%s:%s:%s' % [value.class, name, value.__gibbler(digest_type)]
end
- d = d.join(':').__gibbler(digest_type)
+ d = d.join(Gibbler.delimiter).__gibbler(digest_type)
a = Gibbler.digest '%s:%s:%s' % [klass, d.size, d], digest_type
gibbler_debug klass, a, [klass, d.size, d]
a
@@ -480,7 +485,7 @@ def __gibbler(digest_type=nil)
d << '%s:%s:%s' % [value.class, index, value.__gibbler(digest_type)]
index += 1
end
- d = d.join(':').__gibbler(digest_type)
+ d = d.join(Gibbler.delimiter).__gibbler(digest_type)
a = Gibbler.digest '%s:%s:%s' % [klass, d.size, d], digest_type
gibbler_debug klass, a, [klass, d.size, d]
a
View
@@ -1,5 +1,10 @@
require 'gibbler'
+
+## Default delimiter
+Gibbler.delimiter
+#=> ':'
+
## Create a digest from flattened Array
Gibbler.digest [1, :sym, ['string', 2,3]].flatten.join(':')
#=> 'd84d6ad2bd5c9589842fb02cf3c384e4924b1d3f'

0 comments on commit b04eae8

Please sign in to comment.