Class: Trinidad::Configuration

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

Overview

Trinidad’s (global) configuration instance. Use Trinidad#configure to update and obtain the global instance or access the instance using Trinidad#configuration

Constant Summary collapse

DEFAULTS =
{
  # :port => 3000, HTTP (depends on connector used)
  :address => 'localhost',
  :environment => 'development',
  :context_path => '/',
  :public => 'public',
  :java_lib => 'lib/java',
  :default_web_xml => 'config/web.xml',
  :jruby_min_runtimes =>
    java.lang.System.getProperty('jruby.min.runtimes') || 1,
  :jruby_max_runtimes =>
    java.lang.System.getProperty('jruby.max.runtimes') || 5,
  :log => 'INFO',
  :trap => true
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



51
52
53
54
# File 'lib/trinidad/configuration.rb', line 51

def initialize(options = {})
  @config = DEFAULTS.clone
  update!(options)
end

Class Method Details

.merge_options(target, current, deep = true) ⇒ Object

a Hash like deep_merge helper



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/trinidad/configuration.rb', line 114

def self.merge_options(target, current, deep = true)
  return target unless current
  target_dup = target.dup
  current.keys.each do |key|
    target_dup[key] =
      if deep && options_like?(target[key]) && options_like?(current[key])
        merge_options(target[key], current[key], deep)
      else
        current[key]
      end
  end
  target_dup
end

.symbolize_options(options, deep = true) ⇒ Object

a Hash like #symbolize helper



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/trinidad/configuration.rb', line 96

def self.symbolize_options(options, deep = true)
  new_options = options.class.new
  options.each do |key, value|
    if deep && value.is_a?(Array) # YAML::Omap is an Array
      array = new_options[key.to_sym] = value.class.new
      value.each do |v|
        array << ( options_like?(v) ? symbolize_options(v, deep)  : v )
      end
    elsif deep && options_like?(value)
      new_options[key.to_sym] = symbolize_options(value, deep)
    else
      new_options[key.to_sym] = value
    end
  end
  new_options
end

Instance Method Details

#[](name) ⇒ Object



56
57
58
# File 'lib/trinidad/configuration.rb', line 56

def [](name)
  @config[name.to_sym]
end

#[]=(name, value) ⇒ Object



60
61
62
# File 'lib/trinidad/configuration.rb', line 60

def []=(name, value)
  @config[name.to_sym] = value
end

#each(&block) ⇒ Object



73
74
75
# File 'lib/trinidad/configuration.rb', line 73

def each(&block)
  @config.each(&block)
end

#has_key?(name) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)


64
65
66
# File 'lib/trinidad/configuration.rb', line 64

def has_key?(name)
  @config.has_key?(name.to_sym)
end

#keysObject



69
70
71
# File 'lib/trinidad/configuration.rb', line 69

def keys
  @config.keys
end

#update!(options) ⇒ Object



77
78
79
80
81
# File 'lib/trinidad/configuration.rb', line 77

def update!(options)
  options.each do |key, value|
    self[key] = value.respond_to?(:strip) ? value.strip : value
  end
end