Class: MooTool::Models::Digest

Inherits:
Object
  • Object
show all
Defined in:
lib/mootool/models/digest.rb

Overview

Digest is a misnomer, this is a generic holder of binary data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, hint = nil) ⇒ Digest

Returns a new instance of Digest.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mootool/models/digest.rb', line 10

def initialize(value, hint = nil)
  @hint = hint

  case value
  when String
    @value = value
    @hint = 'SHA1' if value.length == 20
    @hint = 'SHA256' if value.length == 32
    @hint = 'SHA384' if value.length == 48
  when Integer
    @value = [value.to_s(16)].pack('H*')
    @hint = hint
    @integer = true
  else
    raise ArgumentError, "Invalid Input: #{value.inspect}"
  end
rescue StandardError
  raise "Value #{value} with class #{value.class} could not be parsed"
end

Instance Attribute Details

#hintObject

Returns the value of attribute hint.



8
9
10
# File 'lib/mootool/models/digest.rb', line 8

def hint
  @hint
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/mootool/models/digest.rb', line 7

def value
  @value
end

Class Method Details

.create(input, hint = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mootool/models/digest.rb', line 51

def self.create(input, hint = nil)
  if input.is_a?(String) && input.length == 16 && hint.nil?
    UUIDTools::UUID.parse_raw input
  elsif input.is_a?(Array)
    input.map { |i| create(i, hint) }
  elsif input.nil?
    nil
  elsif input.is_a?(Digest)
    input
  else
    Digest.new input, hint
  end
end

.digest(value) ⇒ Object



42
43
44
45
# File 'lib/mootool/models/digest.rb', line 42

def self.digest(value)
  hash = ::Digest::SHA384.digest value
  new hash, 'SHA384'
end

.from_hex(value) ⇒ Object



47
48
49
# File 'lib/mootool/models/digest.rb', line 47

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

.load_manifests(path) ⇒ Object



85
86
87
# File 'lib/mootool/models/digest.rb', line 85

def self.load_manifests(path)
  @manifest = MooTool::Models::IOReg.create path
end

.manifestsObject



81
82
83
# File 'lib/mootool/models/digest.rb', line 81

def self.manifests
  @manifests ||= MooTool::Models::IOReg.create
end

.parse(value) ⇒ Object



38
39
40
# File 'lib/mootool/models/digest.rb', line 38

def self.parse(value)
  new [value].pack('H*')
end

Instance Method Details

#==(other) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/mootool/models/digest.rb', line 97

def ==(other)
  if other.is_a?(MooTool::Models::Digest)
    value == other.value
  elsif other.is_a?(String)
    hex == other.unpack1('H*').upcase || hex == other
  else
    value == other
  end
end

#as_json(*_options) ⇒ Object



89
90
91
# File 'lib/mootool/models/digest.rb', line 89

def as_json(*_options)
  shasum
end

#filesObject



34
35
36
# File 'lib/mootool/models/digest.rb', line 34

def files
  @files ||= FileIndex.current.files_with_hash(shasum)
end

#hexObject



73
74
75
# File 'lib/mootool/models/digest.rb', line 73

def hex
  to_s.unpack1('H*').upcase
end

#inspectObject



111
112
113
# File 'lib/mootool/models/digest.rb', line 111

def inspect
  to_s.unpack1('H*').upcase
end

#integer?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/mootool/models/digest.rb', line 30

def integer?
  @integer
end

#propertiesObject



77
78
79
# File 'lib/mootool/models/digest.rb', line 77

def properties
  Digest.manifests.properties_with_hash(shasum)
end

#shasumObject



69
70
71
# File 'lib/mootool/models/digest.rb', line 69

def shasum
  hex
end

#sizeObject



93
94
95
# File 'lib/mootool/models/digest.rb', line 93

def size
  value.size
end

#to_json(*options) ⇒ Object



107
108
109
# File 'lib/mootool/models/digest.rb', line 107

def to_json(*options)
  as_json(*options).to_json(*options)
end

#to_sObject



65
66
67
# File 'lib/mootool/models/digest.rb', line 65

def to_s
  value
end