Class: Perennial::Settings

Inherits:
Object show all
Defined in:
lib/perennial/settings.rb

Constant Summary collapse

@@verbose =
false
@@log_level =
:info
@@daemon =
false

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



90
91
92
93
# File 'lib/perennial/settings.rb', line 90

def [](key)
  self.setup
  return self.configuration[key.to_sym]
end

.[]=(key, value) ⇒ Object



95
96
97
98
99
# File 'lib/perennial/settings.rb', line 95

def []=(key, value)
  self.setup
  self.configuration[key.to_sym] = value
  return value
end

.daemon?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/perennial/settings.rb', line 14

def daemon?
  !!@@daemon
end

.default_settings_pathObject



42
43
44
# File 'lib/perennial/settings.rb', line 42

def default_settings_path
  @@default_settings_path ||= (root / "config" / "settings.yml")
end

.default_settings_path=(value) ⇒ Object



46
47
48
49
# File 'lib/perennial/settings.rb', line 46

def default_settings_path=(value)
  @@default_settings_path = value
  setup! if setup?
end

.library_rootObject



34
35
36
# File 'lib/perennial/settings.rb', line 34

def library_root
  @@library_root ||= File.expand_path(File.dirname(__FILE__) / ".." / "..")
end

.library_root=(path) ⇒ Object



30
31
32
# File 'lib/perennial/settings.rb', line 30

def library_root=(path)
  @@library_root = File.expand_path(path.to_str)
end

.lookup_key_pathObject



51
52
53
# File 'lib/perennial/settings.rb', line 51

def lookup_key_path
  @@lookup_key_path ||= ["default"]
end

.lookup_key_path=(value) ⇒ Object



55
56
57
# File 'lib/perennial/settings.rb', line 55

def lookup_key_path=(value)
  @@lookup_key_path = value
end

.rootObject



26
27
28
# File 'lib/perennial/settings.rb', line 26

def root
  @@root ||= File.expand_path(File.dirname(__FILE__) / ".." / "..")
end

.root=(path) ⇒ Object



22
23
24
# File 'lib/perennial/settings.rb', line 22

def root=(path)
  @@root = File.expand_path(path.to_str)
end

.setup(options = {}) ⇒ Object



59
60
61
# File 'lib/perennial/settings.rb', line 59

def setup(options = {})
  self.setup!(options) unless setup?
end

.setup!(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/perennial/settings.rb', line 63

def setup!(options = {})
  @@configuration = {}
  settings_file = self.default_settings_path
  if File.exist?(settings_file)
    loaded_yaml = YAML.load(File.read(settings_file))
    @@configuration.merge!(lookup_settings_from(loaded_yaml))
  end
  @@configuration.merge! options
  @@configuration.symbolize_keys!
  # Generate a module 
  mod = generate_settings_accessor_mixin
  extend  mod
  include mod
  @@setup = true
end

.setup?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/perennial/settings.rb', line 38

def setup?
  @@setup ||= false
end

.to_hashObject



101
102
103
# File 'lib/perennial/settings.rb', line 101

def to_hash
  self.configuration.dup
end

.update!(attributes = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/perennial/settings.rb', line 79

def update!(attributes = {})
  return if attributes.blank?
  settings_file = self.default_settings_path
  settings = File.exist?(settings_file) ? YAML.load(File.read(settings_file)) : {}
  namespaced_settings = lookup_settings_from(settings)
  namespaced_settings.merge! attributes.stringify_keys
  File.open(settings_file, "w+") { |f| f.write(settings.to_yaml) }
  setup!
  return true
end

.verbose?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/perennial/settings.rb', line 18

def verbose?
  !!@@verbose
end