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 =
'1.0.0'
REGISTRATIONS =

rubocop:disable Style/MutableConstant

{}
REGISTRATIONS_PER_TAG =
AutoHashCollection.new

Class Method Summary collapse

Class Method Details

.[](entry) ⇒ Object



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

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

.codes ⇒ Object



95
96
97
# File 'lib/multicodecs/registry.rb', line 95

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

.each(tag: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/multicodecs/registry.rb', line 111

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 Also known as: find_by!



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

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



89
90
91
92
93
# File 'lib/multicodecs/registry.rb', line 89

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
22
23
24
25
# 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]
    status = row['status'] || row[3]
    description = row['description'] || row[4]

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

.names ⇒ Object



99
100
101
# File 'lib/multicodecs/registry.rb', line 99

def names
  Multicodecs::REGISTRATIONS.keys
end

.register(name:, tag:, **kwargs) ⇒ Object



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

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

.tags ⇒ Object



103
104
105
# File 'lib/multicodecs/registry.rb', line 103

def tags
  Multicodecs::REGISTRATIONS_PER_TAG.keys
end

.where(tag:) ⇒ Object



107
108
109
# File 'lib/multicodecs/registry.rb', line 107

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