Class: AppConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/appconf.rb,
lib/appconf_version.rb

Defined Under Namespace

Classes: ConfigError

Constant Summary collapse

CONFIG_BASE_NAME =
'config'
VERSION =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_file_nameObject (readonly)

Returns the value of attribute config_file_name.



88
89
90
# File 'lib/appconf.rb', line 88

def config_file_name
  @config_file_name
end

#config_filesObject (readonly)

Returns the value of attribute config_files.



88
89
90
# File 'lib/appconf.rb', line 88

def config_files
  @config_files
end

#deploy_config_dirObject (readonly)

Returns the value of attribute deploy_config_dir.



88
89
90
# File 'lib/appconf.rb', line 88

def deploy_config_dir
  @deploy_config_dir
end

#dist_config_dirObject (readonly)

Returns the value of attribute dist_config_dir.



88
89
90
# File 'lib/appconf.rb', line 88

def dist_config_dir
  @dist_config_dir
end

#global_config_dirObject (readonly)

Returns the value of attribute global_config_dir.



88
89
90
# File 'lib/appconf.rb', line 88

def global_config_dir
  @global_config_dir
end

#platform_config_file_nameObject (readonly)

Returns the value of attribute platform_config_file_name.



88
89
90
# File 'lib/appconf.rb', line 88

def platform_config_file_name
  @platform_config_file_name
end

#user_config_dirObject (readonly)

Returns the value of attribute user_config_dir.



88
89
90
# File 'lib/appconf.rb', line 88

def user_config_dir
  @user_config_dir
end

Class Method Details

.app_nameObject



55
56
57
# File 'lib/appconf.rb', line 55

def self.app_name
	@@app_raw_name
end

.get_config(config_name = nil, config_dir = nil) ⇒ Object

Raises:



58
59
60
61
# File 'lib/appconf.rb', line 58

def self.get_config config_name = nil, config_dir = nil
	raise ConfigError, ('please call %s::setup() before getting a config instance' % self.name) if @@app_name.nil?
	self.new config_name, config_dir
end

.setup(app_name, app_root_dir, deploy_conf_dir = nil) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/appconf.rb', line 41

def self.setup(app_name, app_root_dir, deploy_conf_dir = nil)
	raise ArgumentError, 'app name cannot be empty' if app_name.nil? or app_name.length == 0
	raise ArgumentError, 'app root directory not specified' if app_root_dir.nil? or app_root_dir.length == 0
	raise ArgumentError, 'app root directory does not exist or not a directory' unless File.directory?(app_root_dir)

	@@app_raw_name = app_name
	@@app_name = app_name.downcase
	@@app_root_dir = File.expand_path(app_root_dir)

	raise ArgumentError, 'deploy config directory does not exist or not a directory' if deploy_conf_dir && !File.directory?(deploy_conf_dir)
	@@deploy_config_dir = deploy_conf_dir or ENV[@@app_name.upcase + '_CONF_DIR']
	raise ConfigError, 'config directory `%s\' specifed by environment does not exist or not a directory' %
		@@deploy_config_dir if @@deploy_config_dir && !File.directory?(@@deploy_config_dir)
end

Instance Method Details

#[](key) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/appconf.rb', line 63

def [] key
	@deploy_platform_config.path_lookup(key) ||
		@deploy_config.path_lookup(key) ||
		@user_platform_config.path_lookup(key) ||
		@user_config.path_lookup(key) ||
		@global_platform_config.path_lookup(key) ||
		@global_config.path_lookup(key) ||
		@dist_platform_config.path_lookup(key) ||
		@dist_config.path_lookup(key)
end

#reloadObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/appconf.rb', line 74

def reload
	@deploy_platform_config = deploy_platform_config || {}
	@deploy_config = deploy_config || {}

	@user_platform_config = user_platform_config || {}
	@user_config = user_config || {}

	@global_platform_config = global_platform_config || {}
	@global_config = global_config || {}

	@dist_platform_config = dist_platform_config || {}
	@dist_config = dist_config || {}
end