Class: RailsSettings::Default

Inherits:
Hash
  • Object
show all
Defined in:
lib/rails-settings/default.rb

Defined Under Namespace

Classes: MissingKey

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefault

Returns a new instance of Default.



33
34
35
36
37
38
# File 'lib/rails-settings/default.rb', line 33

def initialize
  content = open(self.class.source_path).read
  hash = content.empty? ? {} : YAML.load(ERB.new(content).result).to_hash
  hash = hash[Sinatra::Application.environment.to_s] || {}
  replace hash
end

Class Method Details

.[](key) ⇒ Object



20
21
22
23
24
25
# File 'lib/rails-settings/default.rb', line 20

def [](key)
  # foo.bar.dar Nested fetch value
  return instance[key] if instance.key?(key)
  keys = key.to_s.split(".")
  instance.dig(*keys)
end

.enabled?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/rails-settings/default.rb', line 8

def enabled?
  source_path && File.exist?(source_path)
end

.instanceObject



27
28
29
30
# File 'lib/rails-settings/default.rb', line 27

def instance
  return @instance if defined? @instance
  @instance = new
end

.source(value = nil) ⇒ Object



12
13
14
# File 'lib/rails-settings/default.rb', line 12

def source(value = nil)
  @source ||= value
end

.source_pathObject



16
17
18
# File 'lib/rails-settings/default.rb', line 16

def source_path
  @source || "config/app.yml"
end