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,
  :address => 'localhost',
  :environment => 'development',
  :context_path => '/',
  :public => 'public',
  :java_lib => 'lib/java',
  :default_web_xml => 'config/web.xml',
  :jruby_min_runtimes => 1,
  :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.



49
50
51
52
# File 'lib/trinidad/configuration.rb', line 49

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



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

def self.merge_options(target, current, deep = true)
  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



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

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



54
55
56
# File 'lib/trinidad/configuration.rb', line 54

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

#[]=(name, value) ⇒ Object



58
59
60
# File 'lib/trinidad/configuration.rb', line 58

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

#each(&block) ⇒ Object



71
72
73
# File 'lib/trinidad/configuration.rb', line 71

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

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/trinidad/configuration.rb', line 62

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

#keysObject



67
68
69
# File 'lib/trinidad/configuration.rb', line 67

def keys
  @config.keys
end

#update!(options) ⇒ Object



75
76
77
78
79
# File 'lib/trinidad/configuration.rb', line 75

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