Class: Perennial::Settings
Constant Summary collapse
- @@verbose =
false- @@log_level =
:info- @@daemon =
false- @@default_settings_path =
nil- @@lookup_key_path =
["default"]
Class Method Summary collapse
- .[](key) ⇒ Object
- .[]=(key, value) ⇒ Object
- .daemon? ⇒ Boolean
- .default_settings_path ⇒ Object
- .default_settings_path=(value) ⇒ Object
- .library_root ⇒ Object
- .library_root=(path) ⇒ Object
- .lookup_key_path ⇒ Object
- .lookup_key_path=(value) ⇒ Object
- .root ⇒ Object
- .root=(path) ⇒ Object
- .setup(options = {}) ⇒ Object
- .setup!(options = {}) ⇒ Object
- .setup? ⇒ Boolean
- .to_hash ⇒ Object
- .update!(attributes = {}) ⇒ Object
- .verbose? ⇒ Boolean
Class Method Details
.[](key) ⇒ Object
92 93 94 95 |
# File 'lib/perennial/settings.rb', line 92 def [](key) self.setup return self.configuration[key.to_sym] end |
.[]=(key, value) ⇒ Object
97 98 99 100 101 |
# File 'lib/perennial/settings.rb', line 97 def []=(key, value) self.setup self.configuration[key.to_sym] = value return value end |
.daemon? ⇒ Boolean
16 17 18 |
# File 'lib/perennial/settings.rb', line 16 def daemon? !!@@daemon end |
.default_settings_path ⇒ Object
44 45 46 |
# File 'lib/perennial/settings.rb', line 44 def default_settings_path @@default_settings_path || (root / "config" / "settings.yml") end |
.default_settings_path=(value) ⇒ Object
48 49 50 51 |
# File 'lib/perennial/settings.rb', line 48 def default_settings_path=(value) @@default_settings_path = value setup! if setup? end |
.library_root ⇒ Object
36 37 38 |
# File 'lib/perennial/settings.rb', line 36 def library_root @@library_root ||= File.(File.dirname(__FILE__) / ".." / "..") end |
.library_root=(path) ⇒ Object
32 33 34 |
# File 'lib/perennial/settings.rb', line 32 def library_root=(path) @@library_root = File.(path.to_str) end |
.lookup_key_path ⇒ Object
53 54 55 |
# File 'lib/perennial/settings.rb', line 53 def lookup_key_path @@lookup_key_path ||= [] end |
.lookup_key_path=(value) ⇒ Object
57 58 59 |
# File 'lib/perennial/settings.rb', line 57 def lookup_key_path=(value) @@lookup_key_path = value end |
.root ⇒ Object
28 29 30 |
# File 'lib/perennial/settings.rb', line 28 def root @@root ||= File.(File.dirname(__FILE__) / ".." / "..") end |
.root=(path) ⇒ Object
24 25 26 |
# File 'lib/perennial/settings.rb', line 24 def root=(path) @@root = File.(path.to_str) end |
.setup(options = {}) ⇒ Object
61 62 63 |
# File 'lib/perennial/settings.rb', line 61 def setup( = {}) self.setup!() unless setup? end |
.setup!(options = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/perennial/settings.rb', line 65 def setup!( = {}) @@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! @@configuration.symbolize_keys! # Generate a module mod = generate_settings_accessor_mixin extend mod include mod @@setup = true end |
.setup? ⇒ Boolean
40 41 42 |
# File 'lib/perennial/settings.rb', line 40 def setup? @@setup ||= false end |
.to_hash ⇒ Object
103 104 105 |
# File 'lib/perennial/settings.rb', line 103 def to_hash self.configuration.dup end |
.update!(attributes = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/perennial/settings.rb', line 81 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
20 21 22 |
# File 'lib/perennial/settings.rb', line 20 def verbose? !!@@verbose end |