Module: ContentAddressableFile::SharedInterface

Included in:
ContentAddressableFile, Shrine::Plugins::ContentAddressable::FileMethods
Defined in:
lib/content_addressable_file/shared_interface.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Returns true if the other File has the same id



35
36
37
# File 'lib/content_addressable_file/shared_interface.rb', line 35

def ==(other)
  other.respond_to?(:digest) && content_addressable == other.content_addressable
end

#content_addressableObject



7
8
9
# File 'lib/content_addressable_file/shared_interface.rb', line 7

def content_addressable
  @content_addressable ||= String(id).rpartition('/').last
end

#decodeObject

Tries to decode the multihash. This is a good check to see if the given id is actually a content-addressable, but also easy to “fake”, as the only way to be certain that the id is a content addressable is actually getting the file and hashing it again.



15
16
17
# File 'lib/content_addressable_file/shared_interface.rb', line 15

def decode
  @decode ||= Multihashes.decode([content_addressable].pack('H*'))
end

#digestObject

The #deocode digest as a byte array



20
21
22
# File 'lib/content_addressable_file/shared_interface.rb', line 20

def digest
  decode[:digest]
end

#digest_hash_functionObject

The #decode hash function



30
31
32
# File 'lib/content_addressable_file/shared_interface.rb', line 30

def digest_hash_function
  decode[:hash_function]
end

#digest_lengthObject

The #decode digest length



25
26
27
# File 'lib/content_addressable_file/shared_interface.rb', line 25

def digest_length
  decode[:length]
end

#hashObject

Enables using File objects as hash keys.



41
42
43
# File 'lib/content_addressable_file/shared_interface.rb', line 41

def hash
  [content_addressable].hash
end