Class: ComplexConfig::Provider

Inherits:
Object
  • Object
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

#initializeProvider

Returns a new instance of Provider.



11
12
13
14
# File 'lib/complex_config/provider.rb', line 11

def initialize
  @plugins     = Set.new
  @deep_freeze = true
end

Instance Attribute Details

#deep_freeze=(value) ⇒ Object (writeonly)

Sets the attribute deep_freeze

Parameters:

  • value

    the value to set the attribute deep_freeze to.



28
29
30
# File 'lib/complex_config/provider.rb', line 28

def deep_freeze=(value)
  @deep_freeze = value
end

#envObject



118
119
120
# File 'lib/complex_config/provider.rb', line 118

def env
  @env || defined?(Rails) && Rails.env || ENV['RAILS_ENV'] || 'development'
end

#key(pathname = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/complex_config/provider.rb', line 124

def key(pathname = nil)
  key = [
    @key,
    read_key_from_file(pathname),
    ENV['RAILS_MASTER_KEY']
  ].compact[0, 1]
  unless key.empty?
    key.pack('H*')
  end
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



21
22
23
# File 'lib/complex_config/provider.rb', line 21

def plugins
  @plugins
end

Instance Method Details

#[](name) ⇒ Object



91
92
93
# File 'lib/complex_config/provider.rb', line 91

def [](name)
  config pathname(name), name
end

#add_plugin(plugin) ⇒ Object



23
24
25
26
# File 'lib/complex_config/provider.rb', line 23

def add_plugin(plugin)
  plugins.add plugin
  self
end

#apply_plugins(setting, id) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/complex_config/provider.rb', line 34

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



59
60
61
62
63
64
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
# File 'lib/complex_config/provider.rb', line 59

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.first)
  hashes[1..-1]&.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_dirObject



51
52
53
# File 'lib/complex_config/provider.rb', line 51

def config_dir
  @config_dir || (defined?(Rails) && Rails.root || Pathname.pwd) + 'config'
end

#config_dir=(dir) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/complex_config/provider.rb', line 43

def config_dir=(dir)
  if dir.nil?
    @config_dir = nil
  else
    @config_dir = Pathname.new(dir)
  end
end

#configure_with(config) ⇒ Object



16
17
18
19
# File 'lib/complex_config/provider.rb', line 16

def configure_with(config)
  config.configure(self)
  flush_cache
end

#deep_freeze?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/complex_config/provider.rb', line 30

def deep_freeze?
  !!@deep_freeze
end

#evaluate(pathname, data) ⇒ Object



112
113
114
115
116
# File 'lib/complex_config/provider.rb', line 112

def evaluate(pathname, data)
  erb = ::ERB.new(data)
  erb.filename = pathname.to_s
  erb.result
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/complex_config/provider.rb', line 96

def exist?(name)
  !!config(pathname(name), name)
rescue ComplexConfig::ConfigurationFileMissing
  false
end

#flush_cacheObject



107
108
109
110
# File 'lib/complex_config/provider.rb', line 107

def flush_cache
  mize_cache_clear
  self
end

#pathname(name) ⇒ Object



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

def pathname(name)
  config_dir + "#{name}.yml"
end

#proxy(env = nil) ⇒ Object



102
103
104
# File 'lib/complex_config/provider.rb', line 102

def proxy(env = nil)
  ComplexConfig::Proxy.new(env)
end