Class: AppConstants
- Inherits:
-
Object
show all
- Defined in:
- lib/app_constants.rb
Constant Summary
collapse
- @@config_path =
Object.const_defined?(:Rails) ? "#{Rails.root}/config/constants.yml" : nil
- @@environment =
Object.const_defined?(:Rails) ? Rails.env : 'test'
- @@raise_error_on_missing =
if true this will raise an error if method you seek isn’t in constants_hash
false
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(constants_hash) ⇒ AppConstants
Returns a new instance of AppConstants.
72
73
74
|
# File 'lib/app_constants.rb', line 72
def initialize(constants_hash)
@constants_hash = constants_hash
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/app_constants.rb', line 33
def method_missing(method, *args)
fail_if_constant_missing(method.to_s)
if constants_hash[method.to_s].nil?
""
elsif constants_hash[method.to_s].is_a?(Hash)
AppConstants.new(constants_hash[method.to_s])
else
constants_hash[method.to_s].freeze
end
end
|
Instance Attribute Details
#constants_hash ⇒ Object
Returns the value of attribute constants_hash.
71
72
73
|
# File 'lib/app_constants.rb', line 71
def constants_hash
@constants_hash
end
|
Class Method Details
.config_path ⇒ Object
9
10
11
|
# File 'lib/app_constants.rb', line 9
def self.config_path
@@config_path
end
|
.config_path=(path) ⇒ Object
13
14
15
|
# File 'lib/app_constants.rb', line 13
def self.config_path=(path)
@@config_path = path
end
|
.environment ⇒ Object
17
18
19
|
# File 'lib/app_constants.rb', line 17
def self.environment
@@environment
end
|
.environment=(environment) ⇒ Object
21
22
23
|
# File 'lib/app_constants.rb', line 21
def self.environment=(environment)
@@environment = environment
end
|
.load! ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/app_constants.rb', line 51
def self.load!
raise ArgumentError.new("No config file path specified. Use 'AppConstants.config_path = PATH' to set it up") if @@config_path.nil?
constants_config = YAML::load(pre_process_constants_file)
fail_if_environment_missing(@@environment, constants_config)
constants_hash = constants_config[@@environment] || {}
@@instance = AppConstants.new(constants_hash)
end
|
.method_missing(method, *args) ⇒ Object
29
30
31
|
# File 'lib/app_constants.rb', line 29
def self.method_missing(method, *args)
@@instance.send(method).is_a?(Hash) ? AppConstants.new(@@instance.constants_hash[method.to_s]) : @@instance.send(method)
end
|
.pre_process_constants_file ⇒ Object
66
67
68
69
|
# File 'lib/app_constants.rb', line 66
def self.pre_process_constants_file
template = File.open(@@config_path).read
ERB.new(template).result
end
|
.raise_error_on_missing=(true_false) ⇒ Object
25
26
27
|
# File 'lib/app_constants.rb', line 25
def self.raise_error_on_missing=(true_false)
@@raise_error_on_missing = true_false
end
|