lib/multicodecs/registry.rb in multicodecs-0.2.1 vs lib/multicodecs/registry.rb in multicodecs-1.0.0
- old
+ new
@@ -11,11 +11,32 @@
# rubocop:disable Style/MutableConstant
REGISTRATIONS = {}
REGISTRATIONS_PER_TAG = AutoHashCollection.new
# rubocop:enable Style/MutableConstant
- Registration = Struct.new(:code, :name, :tag) do
+ class Registration
+ STATUS_DRAFT = 'draft'
+ STATUS_PERMANENT = 'permanent'
+
+ attr_reader :code, :tag, :name, :status, :description
+
+ def initialize(code:, name:, tag:, status:, description:)
+ self.code = code
+ self.name = name
+ self.tag = tag
+ self.status = status
+ self.description = description
+ end
+
+ def permanent?
+ status == STATUS_PERMANENT
+ end
+
+ def draft?
+ status == STATUS_DRAFT
+ end
+
def hash
name.hash
end
def ==(other)
@@ -26,20 +47,32 @@
end
def eql?(other)
other.is_a?(Registration) && other.name == name
end
+
+ def to_s
+ "0x#{code.to_s(16)}"
+ end
+
+ def inspect
+ "#{to_s} #{name} (#{tag}/#{status}): #{description || '(no description)'}"
+ end
+
+ private
+
+ attr_writer :code, :tag, :name, :status, :description
end
module_function
def [](entry)
find_by(code: entry, name: entry)
end
- def register(code:, name:, tag:)
- Registration.new(code, name, tag).tap do |registration|
+ 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
@@ -48,9 +81,11 @@
Multicodecs.find_by(code: code).tap do |found|
raise KeyError, "No codec has code #{code}" unless found
end
end
+
+ alias find_by! fetch_by!
def find_by(code: nil, name: nil)
Multicodecs::REGISTRATIONS.values.find do |v|
v == code || v == name
end