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

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.



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

def deep_freeze=(value)
  @deep_freeze = value
end

#envObject



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

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

#pluginsObject (readonly)

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

#rootObject



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

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

Instance Method Details

#[](name) ⇒ Object



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

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

#add_plugin(plugin) ⇒ Object



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

def add_plugin(plugin)
  plugins.add plugin
  self
end

#apply_plugins(setting, id) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/complex_config/provider.rb', line 27

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



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

def config(pathname)
  result = evaluate(pathname)
  ComplexConfig::Settings[::YAML.load(result, pathname)].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

#deep_freeze?Boolean

Returns:

  • (Boolean)


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

def deep_freeze?
  !!@deep_freeze
end

#evaluate(pathname) ⇒ Object



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

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

#flush_cacheObject



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

def flush_cache
  memoize_cache_clear
  self
end

#pathname(name) ⇒ Object



36
37
38
# File 'lib/complex_config/provider.rb', line 36

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