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

#initialize ⇒ Provider

Returns a new instance of Provider.



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

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.



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

def deep_freeze=(value)
  @deep_freeze = value
end

#env ⇒ Object



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

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

#plugins ⇒ Object (readonly)

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

Instance Method Details

#[](name) ⇒ Object



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

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

#add_plugin(plugin) ⇒ Object



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

def add_plugin(plugin)
  plugins.add plugin
  self
end

#apply_plugins(setting, id) ⇒ Object



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

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



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

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



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

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

#config_dir=(dir) ⇒ Object



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

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

#configure_with(config) ⇒ Object



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

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

#deep_freeze? ⇒ Boolean

Returns:

  • (Boolean)


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

def deep_freeze?
  !!@deep_freeze
end

#evaluate(pathname) ⇒ Object



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

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

#flush_cache ⇒ Object



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

def flush_cache
  memoize_cache_clear
  self
end

#pathname(name) ⇒ Object



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

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

#proxy(env = nil) ⇒ Object



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

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