Class: Staticd::Models::StaticdConfig

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/staticd/models/staticd_config.rb

Overview

Dynamic Staticd configuration.

Manage all configuration parameters for Staticd aimed to change during the application runtime.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ask_value?(name) ⇒ Boolean

Get the parameter boolean value associed with its value.

Convert boolean string into boolean value.

Example:

if Staticd::Models::StaticdConfig.ask_value?(:enable_god_mode)
  puts "God mode is enabled!"
else
  puts "God mode is disabled!"
end

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
# File 'lib/staticd/models/staticd_config.rb', line 55

def self.ask_value?(name)
  boolean_string = get_value(name)
  case boolean_string
  when "true" then true
  when "false" then false
  when nil then false
  else
    raise "Cannot convert a string into a boolean value"
  end
end

.get_value(name) ⇒ Object

Get a value for a parameter.

Return nil if no parameter with this name exist.

Example:

Staticd::Models::StaticdConfig.get_value(:foo)


40
41
42
43
# File 'lib/staticd/models/staticd_config.rb', line 40

def self.get_value(name)
  name = name.to_s
  (param = get(name)) ? param.value : nil
end

.set_value(name, value) ⇒ Object

Set a value for a parameter.

If the parameter exist, its value is updated otherwise the parameter is created with the privided value.

Example:

Staticd::Models::StaticdConfig.set_value(:foo, "bar")


23
24
25
26
27
28
29
30
31
32
# File 'lib/staticd/models/staticd_config.rb', line 23

def self.set_value(name, value)
  name = name.to_s
  value = value.to_s
  if (param = get(name))
    param.update(value: value)
  else
    create(name: name, value: value)
  end
  value
end

Instance Method Details

#to_sObject



66
67
68
# File 'lib/staticd/models/staticd_config.rb', line 66

def to_s
  "#{name}: #{value}"
end