Class: Perennial::Settings

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

Constant Summary collapse

@@configuration =
Perennial::Nash.new
@@verbose =
false
@@log_level =
:info
@@daemon =
false
@@default_settings_path =
nil
@@lookup_key_path =
["default"]

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



113
114
115
116
# File 'lib/perennial/settings.rb', line 113

def method_missing(name, *args, &blk)
  self.class.setup
  @@configuration.send(name, *args, &blk)
end

Class Method Details

.daemon?Boolean

Returns:

  • (Boolean)


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

def daemon?
  !!@@daemon
end

.default_settings_pathObject



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

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

.default_settings_path=(value) ⇒ Object



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

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

.library_rootObject



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

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

.library_root=(path) ⇒ Object



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

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

.lookup_key_pathObject



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

def lookup_key_path
  @@lookup_key_path ||= []
end

.lookup_key_path=(value) ⇒ Object



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

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

.method_missing(name, *args, &blk) ⇒ Object



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

def method_missing(name, *args, &blk)
  self.setup! unless self.setup?
  @@configuration.send(name, *args, &blk)
end

.respond_to?(name, rec = nil) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/perennial/settings.rb', line 99

def respond_to?(name, rec = nil)
  true
end

.rootObject



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

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

.root=(path) ⇒ Object



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

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

.setup(options = {}) ⇒ Object



62
63
64
# File 'lib/perennial/settings.rb', line 62

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

.setup!(options = {}) ⇒ Object



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

def setup!(options = {})
  @@configuration ||= Perennial::Nash.new
  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
  # Finally, normalize settings
  @@configuration = @@configuration.normalized
  @@setup = true
end

.setup?Boolean

Returns:

  • (Boolean)


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

def setup?
  @@setup ||= false
end

.to_hashObject



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

def to_hash
  @@configuration.to_hash
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)


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

def verbose?
  !!@@verbose
end

Instance Method Details

#respond_to?(name, rec = nil) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/perennial/settings.rb', line 118

def respond_to?(name, rec = nil)
  true
end