class BinData::Registry

This registry contains a register of name -> class mappings.

Numerics (integers and floating point numbers) have an endian property as part of their name (e.g. int32be, float_le).

Classes can be looked up based on their full name or an abbreviated name with hints.

There are two hints supported, :endian and :search_prefix.

#lookup("int32", { endian: :big }) will return Int32Be.

#lookup("my_type", { search_prefix: :ns }) will return NsMyType.

Names are stored in under_score_style, not camelCase.

Public Class Methods

new() click to toggle source
# File lib/bindata/registry.rb, line 22
def initialize
  @registry = {}
end

Public Instance Methods

lookup(name, hints = {}) click to toggle source
# File lib/bindata/registry.rb, line 39
def lookup(name, hints = {})
  key = normalize_name(name, hints)
  @registry[key] || raise(UnRegisteredTypeError, name.to_s)
end
register(name, class_to_register) click to toggle source
# File lib/bindata/registry.rb, line 26
def register(name, class_to_register)
  return if class_to_register.nil?

  formatted_name = underscore_name(name)
  warn_if_name_is_already_registered(formatted_name, class_to_register)

  @registry[formatted_name] = class_to_register
end
underscore_name(name) click to toggle source

Convert CamelCase name to underscore style.

# File lib/bindata/registry.rb, line 45
def underscore_name(name)
  name.
    to_s.
    sub(/.*::/, "").
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    tr("-", "_").
    downcase
end
unregister(name) click to toggle source
# File lib/bindata/registry.rb, line 35
def unregister(name)
  @registry.delete(underscore_name(name))
end