Class: Twilio::Rails::Configuration::Registry Abstract
- Inherits:
-
Object
- Object
- Twilio::Rails::Configuration::Registry
- Defined in:
- lib/twilio/rails/configuration.rb
Overview
Base abstract registry class for configuration both phone trees and SMS responders.
Direct Known Subclasses
Instance Method Summary collapse
-
#all ⇒ Hash
Returns all the phone trees or SMS responders as a read-only hash, keyed by name.
-
#finalize! ⇒ true
Finalizes the registry and makes it ready for use.
-
#for(name) ⇒ Class
Returns the phone tree or SMS responder for the given name, or raises an error if it is not found.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#register(klass_or_proc = nil) {|nil| ... } ⇒ nil
Registers a phone tree or SMS responder.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
269 270 271 272 273 |
# File 'lib/twilio/rails/configuration.rb', line 269 def initialize @finalized = false @registry = {}.with_indifferent_access @values = [] end |
Instance Method Details
#all ⇒ Hash
Returns all the phone trees or SMS responders as a read-only hash, keyed by name.
314 315 316 |
# File 'lib/twilio/rails/configuration.rb', line 314 def all @registry.dup.freeze end |
#finalize! ⇒ true
Finalizes the registry and makes it ready for use. It evaluates the blocks and constantizes the class names. Looks up the constants each time ‘to_prepare` is called, so frequently in dev but only once in production.
279 280 281 282 283 |
# File 'lib/twilio/rails/configuration.rb', line 279 def finalize! @registry = {}.with_indifferent_access @values.each { |value| add_to_registry(value) } @finalized = true end |
#for(name) ⇒ Class
Returns the phone tree or SMS responder for the given name, or raises an error if it is not found.
307 308 309 |
# File 'lib/twilio/rails/configuration.rb', line 307 def for(name) @registry[name.to_s] || raise(error_class, "No responder registered for '#{ name }'") end |
#register(klass_or_proc = nil) {|nil| ... } ⇒ nil
Registers a phone tree or SMS responder. It accepts a callable, a Class, a String, or a block which returns any of the aforementioned. The result will all be turned into a class when #finalize! is called. This can be called multiple times.
293 294 295 296 297 298 299 300 301 |
# File 'lib/twilio/rails/configuration.rb', line 293 def register(klass_or_proc=nil, &block) raise Error, "Must pass either a param or a block" unless klass_or_proc.present? ^ block.present? value = klass_or_proc || block @values << value add_to_registry(value) if @finalized nil end |