Class: ComplexConfig::Provider

Inherits:
Object
  • Object
show all
Includes:
Tins::SexySingleton
Defined in:
lib/complex_config/provider.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProvider



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

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

Instance Attribute Details

#deep_freeze=(value) ⇒ Object (writeonly)

Sets the attribute deep_freeze



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

def deep_freeze=(value)
  @deep_freeze = value
end

#envObject



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

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

#pluginsObject (readonly)

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

Instance Method Details

#[](name) ⇒ Object



70
71
72
# File 'lib/complex_config/provider.rb', line 70

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

#add_plugin(plugin) ⇒ Object



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

def add_plugin(plugin)
  plugins.add plugin
  self
end

#apply_plugins(setting, id) ⇒ Object



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

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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/complex_config/provider.rb', line 58

def config(pathname, name = nil)
  result = evaluate(pathname)
  hash = ::YAML.load(result, pathname)
  ComplexConfig::Settings.build(name, hash).tap do |settings|
    deep_freeze? and settings.deep_freeze
  end
rescue ::Errno::ENOENT => e
  raise ComplexConfig::ComplexConfigError.wrap(:ConfigurationFileMissing, e)
rescue ::Psych::SyntaxError => e
  raise ComplexConfig::ComplexConfigError.wrap(:ConfigurationSyntaxError, e)
end

#config_dirObject



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

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

#config_dir=(dir) ⇒ Object



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

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

#configure_with(config) ⇒ Object



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

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

#deep_freeze?Boolean



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

def deep_freeze?
  !!@deep_freeze
end

#evaluate(pathname) ⇒ Object



84
85
86
87
88
89
# File 'lib/complex_config/provider.rb', line 84

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

#flush_cacheObject



79
80
81
82
# File 'lib/complex_config/provider.rb', line 79

def flush_cache
  mize_cache_clear
  self
end

#pathname(name) ⇒ Object



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

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

#proxy(env = nil) ⇒ Object



75
76
77
# File 'lib/complex_config/provider.rb', line 75

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