Class: RandomDigest

Inherits:
Object
  • Object
show all
Defined in:
lib/random_digest.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RandomDigest) initialize(digest = nil)

Returns a new instance of RandomDigest



21
22
23
24
25
26
27
# File 'lib/random_digest.rb', line 21

def initialize(digest = nil)
  if digest.nil?
    generate_digest
  else
    @digest = digest.dup.freeze
  end
end

Instance Attribute Details

- (Object) digest (readonly)

Returns the value of attribute digest



29
30
31
# File 'lib/random_digest.rb', line 29

def digest
  @digest
end

Class Method Details

+ (Object) dump(obj)



7
8
9
10
11
12
13
14
# File 'lib/random_digest.rb', line 7

def dump(obj)
  unless obj.is_a?(self)
    fail ::ActiveRecord::SerializationTypeMismatch,
      "Attribute was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}"
  end

  obj.digest
end

+ (Object) from_hex(hexdigest)



16
17
18
# File 'lib/random_digest.rb', line 16

def from_hex(hexdigest)
  new([hexdigest].pack('H*'))
end

+ (Object) load(digest)



3
4
5
# File 'lib/random_digest.rb', line 3

def load(digest)
  new(digest)
end

Instance Method Details

- (Object) ===(other)



35
36
37
38
39
40
41
42
# File 'lib/random_digest.rb', line 35

def ===(other)
  case other
  when self.class
    digest == other.digest
  when String
    digest == other || digest == [other].pack('H*')
  end
end

- (Object) as_json(options = nil)



44
45
46
# File 'lib/random_digest.rb', line 44

def as_json(options = nil)
  hexdigest
end

- (Object) hexdigest



31
32
33
# File 'lib/random_digest.rb', line 31

def hexdigest
  digest.unpack('H*').first
end