Class: BinData::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/bindata/registry.rb

Overview

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). The lookup can either be on the full name, or on the shortened name plus endian (e.g. “int32”, :big)

Names are stored in under_score_style, not camelCase.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



14
15
16
# File 'lib/bindata/registry.rb', line 14

def initialize
  @registry = {}
end

Instance Method Details

#is_registered?(name, endian = nil) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/bindata/registry.rb', line 40

def is_registered?(name, endian = nil)
  lookup(name, endian) != nil
end

#lookup(name, endian = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/bindata/registry.rb', line 27

def lookup(name, endian = nil)
  key = lookup_key(name, endian)
  try_registering_key(key) unless @registry.has_key?(key)

  @registry[key] || raise(UnRegisteredTypeError, name.to_s)
end

#normalize_name(name, endian = nil) ⇒ Object



34
35
36
37
38
# File 'lib/bindata/registry.rb', line 34

def normalize_name(name, endian = nil)
  if lookup(name, endian)
    lookup_key(name, endian)
  end
end

#register(name, class_to_register) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/bindata/registry.rb', line 18

def register(name, class_to_register)
  return if class_to_register.nil?

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

  @registry[formatted_name] = class_to_register
end

#underscore_name(name) ⇒ Object

Convert CamelCase name to underscore style.



45
46
47
48
49
50
51
# 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