Class: Centroid::Config
- Inherits:
-
Object
show all
- Defined in:
- lib/centroid.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config) ⇒ Config
Returns a new instance of Config.
7
8
9
10
11
12
13
14
|
# File 'lib/centroid.rb', line 7
def initialize(config)
if config.is_a?(Hash)
@raw_config = config
else
@raw_config = JSON.parse(config)
validate_unique_keys!
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(attrib) ⇒ Object
16
17
18
|
# File 'lib/centroid.rb', line 16
def method_missing(attrib, *)
self[attrib]
end
|
Instance Attribute Details
#raw_config ⇒ Object
Returns the value of attribute raw_config.
5
6
7
|
# File 'lib/centroid.rb', line 5
def raw_config
@raw_config
end
|
Class Method Details
.from_file(filename) ⇒ Object
77
78
79
80
|
# File 'lib/centroid.rb', line 77
def self.from_file(filename)
str_json = File.read(filename)
self.new(str_json)
end
|
Instance Method Details
#[](key) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/centroid.rb', line 36
def [](key)
raise KeyError.new("Centroid::Config instance does not contain key: #{key}") unless has_key?(key)
value = find_value(key)
if value.is_a?(Hash)
Config.new(value)
elsif value.is_a?(Array)
value.map { |e| e.is_a?(Hash) ? Config.new(e) : e }
else
value
end
end
|
#each ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/centroid.rb', line 24
def each
return enum_for :each unless block_given?
raw_config.each do |key, value|
if value.is_a?(Hash)
yield key, Config.new(value)
else
yield key, value
end
end
end
|
#for_environment(env) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/centroid.rb', line 60
def for_environment(env)
env_json = raw_config[env]
all_key = actual_key("all")
if all_key.nil?
config = Config.new(env_json)
else
all_json = raw_config[all_key]
config = Config.new(deep_merge(all_json, env_json))
end
if !config.has_key?('environment')
config.raw_config['environment'] = env
end
config
end
|
#has_key?(key) ⇒ Boolean
Also known as:
include?
50
51
52
|
# File 'lib/centroid.rb', line 50
def has_key?(key)
!!actual_key(key)
end
|
#respond_to_missing?(method_name, include_all) ⇒ Boolean
20
21
22
|
# File 'lib/centroid.rb', line 20
def respond_to_missing?(method_name, include_all)
has_key?(method_name)
end
|
#to_s ⇒ Object
56
57
58
|
# File 'lib/centroid.rb', line 56
def to_s
JSON.dump(raw_config)
end
|