Class: Gibbler

Inherits:
String show all
Includes:
Digest::InstanceMethods
Defined in:
lib/gibbler.rb,
lib/gibbler.rb,
lib/gibbler.rb,
lib/gibbler.rb,
lib/gibbler/aliases.rb,
lib/gibbler/history.rb

Overview

Gibbler

“Hola, Tanneritos”

Defined Under Namespace

Modules: Array, Complex, DateTime, File, Hash, History, Nil, Object, Range, String, Time, VERSION Classes: BadDigest, Digest, Error, NoHistory, NoRevert

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Digest::InstanceMethods

#==, #===, #base, #base36, #short, #shorten, #shorter, #tiny, #to_i, #to_s

Methods inherited from String

#__gibbler_revert!, #clear

Methods included from History

#commit, #find_long, #gibbler_commit, #gibbler_find_long, #gibbler_history, #gibbler_history?, #gibbler_object, #gibbler_revert!, #gibbler_stamp, #gibbler_valid?, #history, #history?, mutex, #object, #revert!, #stamp, #valid?

Methods included from String

#__gibbler

Methods included from Object

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

Constructor Details

#initialize(*input) ⇒ Gibbler

Creates a digest from the given input. See Gibbler.digest.

If only one argument is given and it’s a digest, this will simply create an instance of that digest. In other words, it won’t calculate a new digest based on that input.



231
232
233
234
235
236
237
238
# File 'lib/gibbler.rb', line 231

def initialize *input
  if input.size == 1 && Gibbler::Digest::InstanceMethods === input.first
    super input.first
  else
    input.collect!(&:to_s)
    super Gibbler.digest(input) || ''
  end
end

Class Attribute Details

.debugObject

Set to true for debug output (including all digest inputs)



266
267
268
# File 'lib/gibbler.rb', line 266

def debug
  @debug
end

.default_baseObject

Returns the value of attribute default_base.



33
34
35
# File 'lib/gibbler.rb', line 33

def default_base
  @default_base
end

.delimiterObject

The delimiter to use when joining Array values before creating a new digest hash. The default is “:”.



264
265
266
# File 'lib/gibbler.rb', line 264

def delimiter
  @delimiter
end

.digest_typeObject

Specify a different digest class. The default is Digest::SHA1. You could try Digest::SHA256 by doing this:

Object.digest_type = Digest::SHA256


261
262
263
# File 'lib/gibbler.rb', line 261

def digest_type
  @digest_type
end

.secretObject

Returns the value of attribute secret.



33
34
35
# File 'lib/gibbler.rb', line 33

def secret
  @secret
end

Instance Attribute Details

#digest_typeObject



239
240
241
# File 'lib/gibbler.rb', line 239

def digest_type
  @digest_type || self.class.digest_type
end

#inputObject (readonly)

Returns the value of attribute input.



225
226
227
# File 'lib/gibbler.rb', line 225

def input
  @input
end

Class Method Details

.debug?Boolean

Returns the current debug status (true or false)

Returns:

  • (Boolean)


268
# File 'lib/gibbler.rb', line 268

def debug?;  @debug != false; end

.digest(input, digest_type = nil) ⇒ Object

Sends input to Digest::SHA1.hexdigest. If another digest class has been specified, that class will be used instead. If Gibbler.secret is set, str will be prepended with the value.

If input is an Array, it will be flattened and joined.

See: digest_type



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

def self.digest(input, digest_type=nil)
  input = input.flatten.collect(&:to_s).join(delimiter) if ::Array === input
  return if input.empty?
  digest_type ||= @digest_type
  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
end

.gibbler_debug(*args) ⇒ Object



289
290
291
292
# File 'lib/gibbler.rb', line 289

def self.gibbler_debug(*args)
  return unless Gibbler.debug?
  p args
end

.included(obj) ⇒ Object

Raises an exception. The correct usage is to include a Gibbler::Object:

  • Gibbler::Complex

  • Gibbler::String

  • Gibbler::Object

  • etc …



299
300
301
# File 'lib/gibbler.rb', line 299

def self.included(obj)
  raise "You probably want to include Gibbler::Complex or Gibbler::Object"
end

Instance Method Details

#digest(*input) ⇒ Object



243
244
245
# File 'lib/gibbler.rb', line 243

def digest *input
  replace Gibbler.digest(input, digest_type)
end