Class: ComplexConfig::Provider
- Inherits:
-
Object
- Object
- ComplexConfig::Provider
- Includes:
- Tins::SexySingleton
- Defined in:
- lib/complex_config/provider.rb
Instance Attribute Summary collapse
-
#deep_freeze ⇒ Object
writeonly
Sets the attribute deep_freeze.
- #env ⇒ Object
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_plugin(plugin) ⇒ Object
- #apply_plugins(setting, id) ⇒ Object
- #config(pathname, name = nil) ⇒ Object
- #config_dir ⇒ Object
- #config_dir=(dir) ⇒ Object
- #configure_with(config) ⇒ Object
- #deep_freeze? ⇒ Boolean
- #evaluate(pathname) ⇒ Object
- #flush_cache ⇒ Object
-
#initialize ⇒ Provider
constructor
A new instance of Provider.
- #pathname(name) ⇒ Object
- #proxy(env = nil) ⇒ Object
Constructor Details
#initialize ⇒ Provider
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 |
#env ⇒ Object
91 92 93 |
# File 'lib/complex_config/provider.rb', line 91 def env @env || defined?(Rails) && Rails.env || ENV['RAILS_ENV'] || 'development' end |
#plugins ⇒ Object (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_dir ⇒ Object
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_cache ⇒ Object
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 |