Class: Anyway::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/anyway/config.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



23
24
25
# File 'lib/anyway/config.rb', line 23

def initialize
  load
end

Class Attribute Details

.config_attributesObject (readonly)

Returns the value of attribute config_attributes.



4
5
6
# File 'lib/anyway/config.rb', line 4

def config_attributes
  @config_attributes
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



4
5
6
# File 'lib/anyway/config.rb', line 4

def defaults
  @defaults
end

Class Method Details

.attr_config(*args, **hargs) ⇒ Object



6
7
8
9
10
# File 'lib/anyway/config.rb', line 6

def attr_config(*args,**hargs)
  @defaults = hargs.dup.with_indifferent_access
  @config_attributes = args+hargs.keys
  attr_accessor *@config_attributes
end

.config_name(val = nil) ⇒ Object



12
13
14
15
# File 'lib/anyway/config.rb', line 12

def config_name(val = nil)
  return (@config_name = val.to_s) unless val.nil?
  @config_name ||= extract_name
end

Instance Method Details

#clearObject



33
34
35
36
37
38
# File 'lib/anyway/config.rb', line 33

def clear
  self.class.config_attributes.each do |attr|
    self.send("#{attr}=", nil)
  end
  self
end

#loadObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/anyway/config.rb', line 40

def load
  # first, copy defaults
  config = self.class.defaults.deep_dup
  config_name = self.class.config_name

  # then load from YAML if any
  config_path = Rails.root.join("config","#{config_name}.yml")
  if File.file? config_path
    config.deep_merge! (YAML.load_file(config_path)[Rails.env] || {})
  end

  # then load from Rails secrets
  unless Rails.application.try(:secrets).nil?
    config.deep_merge! (Rails.application.secrets.send(config_name)||{})
  end

  # and then load from env
  config.deep_merge! (Anyway.env.send(config_name) || {})

  config.each do |key, val| 
    self.send("#{key}=",val)
  end
end

#reloadObject



27
28
29
30
31
# File 'lib/anyway/config.rb', line 27

def reload
  clear
  load
  self
end