Class: BinData::Registry
- Inherits:
-
Object
- Object
- BinData::Registry
- 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
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #lookup(name, endian = nil) ⇒ Object
- #normalize_name(name, endian = nil) ⇒ Object
- #register(name, class_to_register) ⇒ Object
-
#underscore_name(name) ⇒ Object
Convert CamelCase
name
to underscore style. - #unregister(name) ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
14 15 16 |
# File 'lib/bindata/registry.rb', line 14 def initialize @registry = {} end |
Instance Method Details
#lookup(name, endian = nil) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/bindata/registry.rb', line 32 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
39 40 41 42 43 |
# File 'lib/bindata/registry.rb', line 39 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.
46 47 48 49 50 51 52 |
# File 'lib/bindata/registry.rb', line 46 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) ⇒ Object
27 28 29 30 |
# File 'lib/bindata/registry.rb', line 27 def unregister(name) formatted_name = lookup_key(name) @registry.delete(formatted_name) end |