Class: ComplexConfig::Provider
- Inherits:
-
Object
- Object
- ComplexConfig::Provider
show all
- Includes:
- Shortcuts, Tins::SexySingleton
- Defined in:
- lib/complex_config/provider.rb,
lib/complex_config/provider/shortcuts.rb
Defined Under Namespace
Modules: Shortcuts
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Shortcuts
#complex_config, #complex_config_with_env
Constructor Details
Returns a new instance of Provider.
12
13
14
15
|
# File 'lib/complex_config/provider.rb', line 12
def initialize
@plugins = Set.new
@deep_freeze = true
end
|
Instance Attribute Details
158
159
160
|
# File 'lib/complex_config/provider.rb', line 158
def env
@env || defined?(Rails) && Rails.env || ENV['RAILS_ENV'] || 'development'
end
|
#key(pathname = nil) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/complex_config/provider.rb', line 164
def key(pathname = nil)
key = [
@key,
read_key_from_file(pathname),
ENV['COMPLEX_CONFIG_KEY'],
ENV['RAILS_MASTER_KEY'],
].compact[0, 1].map(&:strip)
unless key.empty?
key.pack('H*')
end
end
|
Returns the value of attribute plugins.
22
23
24
|
# File 'lib/complex_config/provider.rb', line 22
def plugins
@plugins
end
|
Instance Method Details
96
97
98
|
# File 'lib/complex_config/provider.rb', line 96
def [](name)
config pathname(name), name
end
|
#add_plugin(plugin) ⇒ Object
24
25
26
27
|
# File 'lib/complex_config/provider.rb', line 24
def add_plugin(plugin)
plugins.add plugin
self
end
|
#apply_plugins(setting, id) ⇒ Object
40
41
42
43
44
45
46
47
|
# File 'lib/complex_config/provider.rb', line 40
def apply_plugins(setting, id)
plugins.find do |plugin|
catch :skip do
value = setting.instance_exec(id, &plugin) and return value
end
nil
end
end
|
#config(pathname, name = nil) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/complex_config/provider.rb', line 65
def config(pathname, name = nil)
datas = []
if File.exist?(pathname)
datas << IO.binread(pathname)
end
if enc_pathname = pathname.to_s + '.enc' and
File.exist?(enc_pathname) and my_key = key(pathname)
then
text = IO.binread(enc_pathname)
datas << ComplexConfig::Encryption.new(my_key).decrypt(text)
end
datas.empty? and raise ComplexConfig::ConfigurationFileMissing,
"configuration file #{pathname.inspect} is missing"
results = datas.map { |d| evaluate(pathname, d) }
hashes = results.map { |r| ::YAML.load(r, pathname) }
settings = ComplexConfig::Settings.build(name, hashes.shift)
hashes.each { |h| settings.attributes_update(h) }
if shared = settings.shared?
shared = shared.to_h
settings.each do |key, value|
if value.is_a? ComplexConfig::Settings
value.attributes_update(shared)
end
end
end
deep_freeze? and settings.deep_freeze
settings
rescue ::Psych::SyntaxError => e
raise ComplexConfig::ComplexConfigError.wrap(:ConfigurationSyntaxError, e)
end
|
#config_dir ⇒ Object
57
58
59
|
# File 'lib/complex_config/provider.rb', line 57
def config_dir
@config_dir || (defined?(Rails) && Rails.root || Pathname.pwd) + 'config'
end
|
#config_dir=(dir) ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/complex_config/provider.rb', line 49
def config_dir=(dir)
if dir.nil?
@config_dir = nil
else
@config_dir = Pathname.new(dir)
end
end
|
17
18
19
20
|
# File 'lib/complex_config/provider.rb', line 17
def configure_with(config)
config.configure(self)
flush_cache
end
|
#deep_freeze=(flag) ⇒ Object
29
30
31
32
33
34
|
# File 'lib/complex_config/provider.rb', line 29
def deep_freeze=(flag)
if @deep_freeze && !flag
mize_cache_clear
end
@deep_freeze = flag
end
|
#deep_freeze? ⇒ Boolean
36
37
38
|
# File 'lib/complex_config/provider.rb', line 36
def deep_freeze?
!!@deep_freeze
end
|
#evaluate(pathname, data) ⇒ Object
152
153
154
155
156
|
# File 'lib/complex_config/provider.rb', line 152
def evaluate(pathname, data)
erb = ::ERB.new(data)
erb.filename = pathname.to_s
erb.result
end
|
#flush_cache ⇒ Object
147
148
149
150
|
# File 'lib/complex_config/provider.rb', line 147
def flush_cache
mize_cache_clear
self
end
|
#pathname(name) ⇒ Object
61
62
63
|
# File 'lib/complex_config/provider.rb', line 61
def pathname(name)
config_dir + "#{name}.yml"
end
|
#proxy(env = nil) ⇒ Object
142
143
144
|
# File 'lib/complex_config/provider.rb', line 142
def proxy(env = nil)
ComplexConfig::Proxy.new(env)
end
|
#write_config(name, value, encrypt: false, store_key: false) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/complex_config/provider.rb', line 101
def write_config(name, value, encrypt: false, store_key: false)
config_pathname = pathname(name).to_s
key = case encrypt
when :random
SecureRandom.random_bytes(16)
when true
key(config_pathname)
when String
encrypt
end
hex_key = nil
settings = ComplexConfig::Settings[value]
if encrypt
key or raise ComplexConfig::EncryptionKeyInvalid,
"encryption key is missing"
key.size != 16 and raise ComplexConfig::EncryptionKeyInvalid,
"encryption keys has to be of 16 bytes lenght"
File.secure_write(config_pathname + '.enc') do |out|
out.puts ComplexConfig::Encryption.new(key).encrypt(settings.to_yaml)
end
hex_key = key.unpack('H*').first
if store_key
File.secure_write(config_pathname + '.key') do |out|
out.puts hex_key
end
end
else
File.secure_write(config_pathname) do |out|
out.puts settings.to_yaml
end
end
flush_cache
hex_key
end
|