Class: SystemSettings::Configurator

Inherits:
Object
  • Object
show all
Defined in:
app/models/system_settings/configurator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Configurator

Returns a new instance of Configurator.



21
22
23
24
25
26
27
28
29
# File 'app/models/system_settings/configurator.rb', line 21

def initialize(&block)
  @items = []
  return unless block_given?
  if block.arity == 1
    yield self
  else
    instance_exec(&block)
  end
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



19
20
21
# File 'app/models/system_settings/configurator.rb', line 19

def items
  @items
end

Class Method Details

.from_file(path) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'app/models/system_settings/configurator.rb', line 4

def from_file(path)
  path_str = path.to_s
  raise SystemSettings::Errors::SettingsReadError, "#{path_str} file does not exist" unless File.exist?(path_str)
  raise SystemSettings::Errors::SettingsReadError, "#{path_str} file not readable" unless File.readable?(path_str)
  file_content = File.read(path_str)
  new.tap do |obj|
    obj.instance_eval(file_content, path_str, 1)
  end
end

.purgeObject



14
15
16
# File 'app/models/system_settings/configurator.rb', line 14

def purge
  new.purge
end

Instance Method Details

#boolean(name, value: nil, description: nil, &blk) ⇒ Object



47
48
49
# File 'app/models/system_settings/configurator.rb', line 47

def boolean(name, value: nil, description: nil, &blk)
  add(name, SystemSettings::BooleanSetting, value: value, description: description, &blk)
end

#integer(name, value: nil, description: nil, &blk) ⇒ Object



39
40
41
# File 'app/models/system_settings/configurator.rb', line 39

def integer(name, value: nil, description: nil, &blk)
  add(name, SystemSettings::IntegerSetting, value: value, description: description, &blk)
end

#integer_list(name, value: nil, description: nil, &blk) ⇒ Object



43
44
45
# File 'app/models/system_settings/configurator.rb', line 43

def integer_list(name, value: nil, description: nil, &blk)
  add(name, SystemSettings::IntegerListSetting, value: value || [], description: description, &blk)
end

#persistObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/system_settings/configurator.rb', line 51

def persist
  if settings_table_exists?
    SystemSettings::Setting.transaction do
      @items.each do |entry|
        persisted_record = entry[:class].find_by(name: entry[:name])
        if persisted_record
          persisted_record.update_attributes!(description: entry[:description])
        else
          entry[:class].create!(name: entry[:name], value: entry[:value], description: entry[:description])
        end
      end
    end
    true
  else
    warn "SystemSettings: Settings table has not been created!"
    false
  end
end

#purgeObject



70
71
72
73
74
75
76
77
# File 'app/models/system_settings/configurator.rb', line 70

def purge
  if settings_table_exists?
    SystemSettings::Setting.delete_all
    true
  else
    false
  end
end

#string(name, value: nil, description: nil, &blk) ⇒ Object



31
32
33
# File 'app/models/system_settings/configurator.rb', line 31

def string(name, value: nil, description: nil, &blk)
  add(name, SystemSettings::StringSetting, value: value, description: description, &blk)
end

#string_list(name, value: nil, description: nil, &blk) ⇒ Object



35
36
37
# File 'app/models/system_settings/configurator.rb', line 35

def string_list(name, value: nil, description: nil, &blk)
  add(name, SystemSettings::StringListSetting, value: value || [], description: description, &blk)
end