Class: Crystal::Config

Inherits:
SafeHash show all
Defined in:
lib/crystal/environment/config.rb

Constant Summary collapse

DEFAULTS =
{
  :environment => 'development',
}.stringify_keys

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from SafeHash

#hash

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SafeHash

#[], #[]=, #delete, #include?, #inspect, #is_a_safe_hash?, #method_missing, #reinitialize, #to_hash, #to_yaml

Constructor Details

#initializeConfig

Returns a new instance of Config.



9
10
11
12
13
14
15
# File 'lib/crystal/environment/config.rb', line 9

def initialize
  super
  
  config = DEFAULTS.clone
  config.merge! Config.command_line_config if Config.command_line_config
  config.each{|k, v| self.send "#{k}=", v}            
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SafeHash

Class Attribute Details

.command_line_configObject

Returns the value of attribute command_line_config.



94
95
96
# File 'lib/crystal/environment/config.rb', line 94

def command_line_config
  @command_line_config
end

Instance Attribute Details

#log_levelObject



80
81
82
# File 'lib/crystal/environment/config.rb', line 80

def log_level
  @log_level ||= production? ? :info : :debug
end

#log_pathObject



71
72
73
# File 'lib/crystal/environment/config.rb', line 71

def log_path
  @log_path ||= root? ? "#{root}/log/#{environment}.log" : ""
end

Class Method Details

.apply_command_line_arguments!Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/crystal/environment/config.rb', line 96

def apply_command_line_arguments!
  require 'optparse'      

  config = {}
  case ARGV.first
    when "p" then config['environment'] = "production"
    when "d" then config['environment'] = "development"
    when "t" then config['environment'] = "test"
  end

  OptionParser.new{|op|
    op.on('-e env'){|val| config['environment'] = val}
    op.on('-s server'){|val| config['server'] = val}
    op.on('-p port'){|val| config['port'] = val.to_i}
  }.parse!(ARGV.dup)      
  
  self.command_line_config = config
end

.defered_configsObject



115
# File 'lib/crystal/environment/config.rb', line 115

def defered_configs; @defered_configs ||= [] end

Instance Method Details

#development?Boolean

Returns:

  • (Boolean)


62
# File 'lib/crystal/environment/config.rb', line 62

def development?; environment == 'development' end

#environment=(env) ⇒ Object

environment



56
57
58
59
60
# File 'lib/crystal/environment/config.rb', line 56

def environment= env
  env = env.to_s
  env.must_be.in %w{production development test}
  self['environment'] = env
end

#load_app_config!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/crystal/environment/config.rb', line 17

def load_app_config!
  return unless root?
  
  # loading app_root/config/config.yml
  cfile = "#{root!}/config/config.yml"
  if File.exist? cfile
    data = YAML.load_file cfile
    if data
      data.must_be.a Hash
      data.each{|k, v| self[k] = v}
    end
  end
  
  # loading app_root/config/config.<environment>.yml
  env_cfile = "#{root!}/config/config.#{environment!}.yml"
  if File.exist? env_cfile
    data = YAML.load_file env_cfile
    if data
      data.must_be.a Hash
      data.each{|k, v| self[k] = v}
    end
  end
end

#production?Boolean

Returns:

  • (Boolean)


63
# File 'lib/crystal/environment/config.rb', line 63

def production?; environment == 'production' end

#root=(root) ⇒ Object

root



44
45
46
47
48
49
50
# File 'lib/crystal/environment/config.rb', line 44

def root= root 
  if root
    self['root'] = File.expand_path(root)
  else
    self.delete 'root'
  end
end

#test?Boolean

Returns:

  • (Boolean)


64
# File 'lib/crystal/environment/config.rb', line 64

def test?; environment == 'test' end