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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/trinidad/configuration.rb', line 35

def initialize(options = {})
  @config = {
    :port => 3000,
    :address => 'localhost', 
    :environment => 'development',
    :context_path => '/',
    :libs_dir => 'lib',
    :classes_dir => 'classes',
    :default_web_xml => 'config/web.xml',
    :jruby_min_runtimes => 1,
    :jruby_max_runtimes => 5,
    :log => 'INFO',
    :trap => true
  }
  update!(options)
end

Class Method Details

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

a Hash like deep_merge helper



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/trinidad/configuration.rb', line 109

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



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

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



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

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

#[]=(name, value) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys
  @config.keys
end

#update!(options) ⇒ Object



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

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