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

def initialize
  @plugins = Set.new
end

Instance Attribute Details

#env ⇒ Object



64
65
66
# File 'lib/complex_config/provider.rb', line 64

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

#plugins ⇒ Object (readonly)

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

#root ⇒ Object



58
59
60
# File 'lib/complex_config/provider.rb', line 58

def root
  @root || defined?(Rails) && Rails.root || Pathname.pwd
end

Instance Method Details

#[](name) ⇒ Object



42
43
44
# File 'lib/complex_config/provider.rb', line 42

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

#add_plugin(plugin) ⇒ Object



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

def add_plugin(plugin)
  @plugins.add plugin
  self
end

#apply_plugins(setting, id) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/complex_config/provider.rb', line 20

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



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

def config(pathname)
  result = evaluate(pathname)
  ComplexConfig::Settings[::YAML.load(result, pathname)]
rescue ::Errno::ENOENT => e
  raise ComplexConfig::ComplexConfigError.wrap(:ConfigurationFileMissing, e)
rescue ::Psych::SyntaxError => e
  raise ComplexConfig::ComplexConfigError.wrap(:ConfigurationSyntaxError, e)
end

#evaluate(pathname) ⇒ Object



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

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

#pathname(name) ⇒ Object



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

def pathname(name)
  root + "config/#{name}.yml" % name
end