Class: BiblioTech::Builders::Base::AdapterRegistry
- Inherits:
-
Object
- Object
- BiblioTech::Builders::Base::AdapterRegistry
- Defined in:
- lib/bibliotech/builders.rb
Instance Method Summary collapse
- #fetch(name) ⇒ Object
-
#initialize ⇒ AdapterRegistry
constructor
A new instance of AdapterRegistry.
- #put(name, value) ⇒ Object
Constructor Details
#initialize ⇒ AdapterRegistry
Returns a new instance of AdapterRegistry.
7 8 9 10 |
# File 'lib/bibliotech/builders.rb', line 7 def initialize @hash = {} @has_regexp = false end |
Instance Method Details
#fetch(name) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bibliotech/builders.rb', line 21 def fetch(name) @hash.fetch(name.to_s) do klass = nil if @has_regexp _, klass = @hash.find{ |pattern, klass| next unless pattern.is_a? Regexp name =~ pattern } end if klass.nil? if block_given? yield else raise KeyError, "No adapter registered for #{name} - try #{@hash.keys.join(", ")}" end else klass end end end |
#put(name, value) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/bibliotech/builders.rb', line 12 def put(name, value) if name.is_a?(Regexp) @has_regexp = true else name = name.to_s end @hash[name] = value end |