Class: Twilio::Rails::Configuration::Registry Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio/rails/configuration.rb

Overview

This class is abstract.

Base abstract registry class for configuration both phone trees and SMS responders.

Direct Known Subclasses

PhoneTreeRegistry, SMSResponderRegistry

Instance Method Summary collapse

Constructor Details

#initializeRegistry

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

#allHash

Returns all the phone trees or SMS responders as a read-only hash, keyed by name.

Returns:

  • (Hash)

    all the phone trees or SMS responders.



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.

Returns:

  • (true)


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.

Parameters:

  • name (String, Symbol)

    of the phone tree or SMS responder to find.

Returns:

  • (Class)

    the phone tree or SMS responder class.



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.

Parameters:

  • klass_or_proc (Class, String, Proc) (defaults to: nil)

    value containing the Class to be lazily initialized when #finalize! is called.

Yields:

  • (nil)

    if a block is passed, it will be called and the result will be used as the value.

Yield Returns:

  • (Class, String, Proc)

    containing the Class to be lazily initialized when #finalize! is called.

Returns:

  • (nil)

Raises:



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