Module: Multicodecs

Defined in:
lib/multicodecs.rb,
lib/multicodecs/bare.rb,
lib/multicodecs/version.rb,
lib/multicodecs/registry.rb

Defined Under Namespace

Classes: AutoHashCollection, Registration

Constant Summary collapse

VERSION =
'0.2.1'
REGISTRATIONS =

rubocop:disable Style/MutableConstant

{}
REGISTRATIONS_PER_TAG =
AutoHashCollection.new

Class Method Summary collapse

Class Method Details

.[](entry) ⇒ Object



35
36
37
# File 'lib/multicodecs/registry.rb', line 35

def [](entry)
  find_by(code: entry, name: entry)
end

.codesObject



60
61
62
# File 'lib/multicodecs/registry.rb', line 60

def codes
  Multicodecs::REGISTRATIONS.values.map(&:code)
end

.each(tag: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/multicodecs/registry.rb', line 76

def each(tag: nil)
  arr = tag.nil? ? Multicodecs::REGISTRATIONS.values : Multicodecs.where(tag: tag)

  if block_given?
    return arr.each { |registration| yield registration }
  end

  arr.each
end

.fetch_by!(code: nil, name: nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/multicodecs/registry.rb', line 46

def fetch_by!(code: nil, name: nil)
  return Multicodecs::REGISTRATIONS.fetch(name) if name

  Multicodecs.find_by(code: code).tap do |found|
    raise KeyError, "No codec has code #{code}" unless found
  end
end

.find_by(code: nil, name: nil) ⇒ Object



54
55
56
57
58
# File 'lib/multicodecs/registry.rb', line 54

def find_by(code: nil, name: nil)
  Multicodecs::REGISTRATIONS.values.find do |v|
    v == code || v == name
  end
end

.load_csv(csv, radix: 16) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/multicodecs/bare.rb', line 9

def load_csv(csv, radix: 16)
  csv.each do |row|
    name  = row['name'] || row[0]
    tag   = row['tag']  || row[1]
    code  = row['code'] || row[2]

    Multicodecs.register(
      name: name.strip,
      tag: tag.strip,
      code: code.strip.to_i(16)
    )
  end
end

.namesObject



64
65
66
# File 'lib/multicodecs/registry.rb', line 64

def names
  Multicodecs::REGISTRATIONS.keys
end

.register(code:, name:, tag:) ⇒ Object



39
40
41
42
43
44
# File 'lib/multicodecs/registry.rb', line 39

def register(code:, name:, tag:)
  Registration.new(code, name, tag).tap do |registration|
    Multicodecs::REGISTRATIONS[name] = registration
    Multicodecs::REGISTRATIONS_PER_TAG[tag] << registration
  end
end

.tagsObject



68
69
70
# File 'lib/multicodecs/registry.rb', line 68

def tags
  Multicodecs::REGISTRATIONS_PER_TAG.keys
end

.where(tag:) ⇒ Object



72
73
74
# File 'lib/multicodecs/registry.rb', line 72

def where(tag:)
  Multicodecs::REGISTRATIONS_PER_TAG[tag]
end