Class: Adhearsion::Configuration

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

Constant Summary collapse

ConfigurationError =

Error raised while trying to configure a non existent plugin

Class.new Adhearsion::Error

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Adhearsion::Configuration

Initialize the configuration object

  • &block platform configuration block

Adhearsion::Configuration.new do

foo "bar", :desc => "My description"

end



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/adhearsion/configuration.rb', line 19

def initialize(&block)
  initialize_environments

  Loquacious.env_config = true
  Loquacious.env_prefix = "AHN"

  Loquacious::Configuration.for :platform do
    root nil, :desc => "Adhearsion application root folder"

    lib "lib", :desc => <<-__
      Folder to include the own libraries to be used. Adhearsion loads any ruby file
      located into this folder during the bootstrap process. Set to nil if you do not
      want these files to be loaded. This folder is relative to the application root folder.
    __

    environment :development, :transform => Proc.new { |v| v.to_sym }, :desc => <<-__
      Active environment. Supported values: development, production, staging, test
    __

    process_name "ahn", :desc => <<-__
      Adhearsion process name, useful to make it easier to find in the process list
      Pro tip: set this to your application's name and you can do "killall myapp"
      Does not work under JRuby.
    __

    desc "Log configuration"
    logging {
      level :info, :transform => Proc.new { |v| v.to_sym }, :desc => <<-__
        Supported levels (in increasing severity) -- :trace < :debug < :info < :warn < :error < :fatal
      __
      outputters ["log/adhearsion.log"], :transform => Proc.new { |val| Array(val) }, :desc => <<-__
        An array of log outputters to use. The default is to log to stdout and log/adhearsion.log.
        Each item must be either a string to use as a filename, or a valid Logging appender (see http://github.com/TwP/logging)
      __
      formatter nil, :desc => <<-__
        A log formatter to apply to all active outputters. If nil, the Adhearsion default formatter will be used.
      __
    }

    after_hangup_lifetime 1, :transform => Proc.new { |v| v.to_i }, :desc => <<-__
      Lifetime of a call after it has hung up. Should be set to the minimum functional value for your application. Call actors (threads) living after hangup consume more system resources and reduce the concurrent call capacity of your application.
    __

    desc "Media configuration"
    media {
      default_voice nil, desc: 'The default voice used for all output. Set nil to use platform default.'
      default_renderer nil, desc: 'The default renderer used for all output. Set nil to use platform default.'
    }
  end

  Loquacious::Configuration.for :platform, &block if block_given?

  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Wrapper to access to a specific configuration object

Adhearsion.config.foo => returns the configuration object associated to the foo plugin



114
115
116
117
118
# File 'lib/adhearsion/configuration.rb', line 114

def method_missing(method_name, *args, &block)
  config = Loquacious::Configuration.for method_name, &block
  raise Adhearsion::Configuration::ConfigurationError.new "Invalid plugin #{method_name}" if config.nil?
  config
end

Instance Method Details

#[](value) ⇒ Loquacious::Configuration

Direct access to a specific configuration object

Adhearsion.config => returns the configuration object associated to the Adhearsion platform

Returns:

  • (Loquacious::Configuration)

    configuration object or nil if the plugin does not exist



106
107
108
# File 'lib/adhearsion/configuration.rb', line 106

def [](value)
  self.send value.to_sym
end

#add_environment(env) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/adhearsion/configuration.rb', line 90

def add_environment(env)
  return if self.class.method_defined? env.to_sym
  self.class.send(:define_method, env.to_sym) do |*args, &block|
    unless block.nil? || env != self.platform.environment.to_sym
      self.instance_eval(&block)
    end
    self
  end
end

#description(name, args = {:show_values => true}) ⇒ Object

Fetchs the configuration info for the Adhearsion platform or a specific plugin

Parameters:

  • name (Symbol)
    • :all => Adhearsion platform and all the loaded plugins

    • nil => Adhearsion platform configuration

    • :platform => Adhearsion platform configuration

    • :<plugin-config-name> => Adhearsion plugin configuration

  • args (Hash) (defaults to: {:show_values => true})
    • @option :show_values [Boolean] true | false to return the current values or just the description

Returns:

  • string with the configuration description/values



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/adhearsion/configuration.rb', line 155

def description(name, args = {:show_values => true})
  desc = StringIO.new

  name.nil? and name = :platform
  if name.eql? :all
    value = ""
    Loquacious::Configuration.instance_variable_get("@table").keys.map do |config|
      value.concat description config, args
    end
    return value
  else
    return "" if Loquacious::Configuration.for(name).nil?

    if args[:show_values]
      name_leader = "  config.#{name}."
      desc_leader = "  # "
      name_value_sep = " = "
      title_leader = "  "
    else
      name_leader = ""
      desc_leader = "#"
      name_value_sep = " => "
      title_leader = ""
    end

    config = Loquacious::Configuration.help_for name,
                            :name_leader => name_leader,
                            :desc_leader => desc_leader,
                            :colorize    => true,
                            :io          => desc,
                            :name_value_sep => name_value_sep
    config.show :values => args[:show_values]
    "#{title_leader}# ******* Configuration for #{name} **************\n\n#{desc.string}"
  end
end

#initialize_environmentsObject



74
75
76
77
78
79
80
# File 'lib/adhearsion/configuration.rb', line 74

def initialize_environments
  # Create a method per each valid environment that, when invoked, may execute
  # the block received if the environment is active
  valid_environments.each do |enviro|
    add_environment enviro
  end
end

#platform(&block) ⇒ Loquacious::Configuration

Handle the Adhearsion platform configuration

It accepts a block that will be executed in the Adhearsion config var environment to update the desired values

Adhearsion.config.platform do

foo "bar", :desc => "My new description"

end

values = Adhearsion.config.platform values.foo => “bar”

Returns:

  • (Loquacious::Configuration)

    configuration object or nil if the plugin does not exist



139
140
141
# File 'lib/adhearsion/configuration.rb', line 139

def platform(&block)
  Loquacious::Configuration.for :platform, &block
end

#rootObject

root accessor



121
122
123
# File 'lib/adhearsion/configuration.rb', line 121

def root
  platform.root
end

#valid_environment?(env) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/adhearsion/configuration.rb', line 82

def valid_environment?(env)
  env && self.valid_environments.include?(env.to_sym)
end

#valid_environmentsObject



86
87
88
# File 'lib/adhearsion/configuration.rb', line 86

def valid_environments
  @valid_environments ||= [:production, :development, :staging, :test]
end