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



93
94
95
# File 'lib/complex_config/provider.rb', line 93

def env
  @env || defined?(Rails) && Rails.env || ENV['RAILS_ENV'] || 'development'
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



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

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

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



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) ⇒ Object



86
87
88
89
90
91
# File 'lib/complex_config/provider.rb', line 86

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

#flush_cacheObject



81
82
83
84
# File 'lib/complex_config/provider.rb', line 81

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



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

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