Class: Fulmar::Domain::Service::ConfigurationService
- Inherits:
-
Object
- Object
- Fulmar::Domain::Service::ConfigurationService
show all
- Includes:
- Singleton
- Defined in:
- lib/fulmar/domain/service/configuration_service.rb
Overview
Loads and prepares the configuration from the yaml file TODO: Clone target configuration when used as a parameter to another service so an environment change won’t affect it Not Sure if that is actually a god idea
Constant Summary
collapse
- FULMAR_FILE =
'Fulmarfile'
- FULMAR_CONFIGURATION =
'FulmarConfiguration'
- FULMAR_CONFIGURATION_DIR =
'Fulmar'
- DEPLOYMENT_CONFIG_FILE =
'deployment.yml'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ConfigurationService.
19
20
21
22
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 19
def initialize
@environment = nil
@target = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
51
52
53
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 51
def method_missing(name)
environment(name) if configuration[:environments][name]
end
|
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
17
18
19
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 17
def environment
@environment
end
|
#target ⇒ Object
Returns the value of attribute target.
17
18
19
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 17
def target
@target
end
|
Instance Method Details
#[](id) ⇒ Object
Allow access of configuration via array/hash access methods (read access)
25
26
27
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 25
def [](id)
ready? ? configuration[:environments][@environment][@target][id] : nil
end
|
#[]=(id, value) ⇒ Object
Allow access of configuration via array/hash access methods (write access)
30
31
32
33
34
35
36
37
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 30
def []=(id, value)
if ready?
configuration[:environments][@environment][@target][id] = value
else
fail 'Environment or target not set. Please set both variables via configuration.environment = \'xxx\' / '\
'configuration.target = \'yyy\''
end
end
|
#any? ⇒ Boolean
88
89
90
91
92
93
94
95
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 88
def any?
if block_given?
each { |_env, _target, data| return true if yield(data) }
false
else
configuration[:environments].any?
end
end
|
#base_path ⇒ Object
43
44
45
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 43
def base_path
@base_path ||= lookup_base_path
end
|
#config_files ⇒ Object
106
107
108
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 106
def config_files
Dir.glob(File.join(base_path, FULMAR_CONFIGURATION_DIR, '*.config.yml')).sort
end
|
#configuration ⇒ Object
47
48
49
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 47
def configuration
@config ||= load_configuration
end
|
#each ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 80
def each
configuration[:environments].each_key do |env|
configuration[:environments][env].each_pair do |target, data|
yield(env, target, data)
end
end
end
|
#feature?(feature) ⇒ Boolean
70
71
72
73
74
75
76
77
78
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 70
def feature?(feature)
return configuration[:features][feature] unless configuration[:features][feature].nil?
case feature
when :database
configuration[:features][:database] = any? { |data| data[:type] == 'maria' }
else
false
end
end
|
#merge(other) ⇒ Object
Merge another configuration into the currently active one Useful for supplying a default configuration, as values are not overwritten. Hashes are merged.
101
102
103
104
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 101
def merge(other)
return unless @environment && @target
configuration[:environments][@environment][@target] = other.deep_merge(configuration[:environments][@environment][@target])
end
|
#ready? ⇒ Boolean
63
64
65
66
67
68
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 63
def ready?
return false if @environment.nil? || @target.nil?
fail 'Environment is invalid' if configuration[:environments][@environment].nil?
fail 'Target is invalid' if configuration[:environments][@environment][@target].nil?
true
end
|
#to_hash ⇒ Object
39
40
41
|
# File 'lib/fulmar/domain/service/configuration_service.rb', line 39
def to_hash
ready? ? configuration[:environments][@environment][@target] : configuration
end
|