Class: BinData::Registry

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



8
9
10
# File 'lib/bindata/registry.rb', line 8

def initialize
  @registry = {}
end

Instance Method Details

#lookup(name) ⇒ Object

Returns the class matching a previously registered name.



33
34
35
# File 'lib/bindata/registry.rb', line 33

def lookup(name)
  @registry[name.to_s]
end

#register(name, klass) ⇒ Object

Registers the mapping of name to klass. name is converted from camelCase to underscore style. Returns the converted name



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bindata/registry.rb', line 15

def register(name, klass)
  # convert camelCase name to underscore style
  underscore_name = name.to_s.sub(/.*::/, "").
                         gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
                         gsub(/([a-z\d])([A-Z])/,'\1_\2').
                         tr("-", "_").
                         downcase

  # warn if replacing an existing class
  if $VERBOSE and (existing = @registry[underscore_name])
    warn "warning: replacing registered class #{existing} with #{klass}"
  end

  @registry[underscore_name] = klass
  underscore_name.dup
end