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(kernel_class: Kernel, &block) ⇒ Configurator

Returns a new instance of Configurator.



27
28
29
30
31
32
33
34
35
36
# File 'app/models/system_settings/configurator.rb', line 27

def initialize(kernel_class: Kernel, &block)
  @items = []
  @kernel_class = kernel_class
  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.



25
26
27
# File 'app/models/system_settings/configurator.rb', line 25

def items
  @items
end

Class Method Details

.from_file(file_name, kernel_class: Kernel) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/system_settings/configurator.rb', line 6

def from_file(file_name, kernel_class: Kernel)
  file_name = file_name.to_path if file_name.respond_to?(:to_path)
  raise SystemSettings::Errors::SettingsReadError, "The file name must either be a String or implement #to_path" unless file_name.is_a?(String)
  raise SystemSettings::Errors::SettingsReadError, "#{file_name} file does not exist" unless File.exist?(file_name)
  raise SystemSettings::Errors::SettingsReadError, "#{file_name} file not readable" unless File.readable?(file_name)
  SystemSettings.instrument("system_settings.from_file", path: file_name) do |payload|
    file_content = File.read(file_name)
    new(kernel_class: kernel_class).tap do |obj|
      obj.instance_eval(file_content, file_name, 1)
      payload[:items] = obj.items
    end
  end
end

.purgeObject



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

def purge
  new.purge
end

Instance Method Details

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



54
55
56
# File 'app/models/system_settings/configurator.rb', line 54

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

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



58
59
60
# File 'app/models/system_settings/configurator.rb', line 58

def decimal(name, value: nil, description: nil, &blk)
  add(name, SystemSettings::DecimalSetting, value: value, description: description, &blk)
end

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



62
63
64
# File 'app/models/system_settings/configurator.rb', line 62

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

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



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

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



50
51
52
# File 'app/models/system_settings/configurator.rb', line 50

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

#persist(only: []) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/system_settings/configurator.rb', line 66

def persist(only: [])
  SystemSettings.instrument("system_settings.persist", items: @items) do |payload|
    if settings_table_exists?
      SystemSettings::Setting.transaction do
        if only.empty?
          @items.each { |item| create_or_update_item(item) }
        else
          only.each do |wanted_name|
            item = @items.find { |i| i[:name] == wanted_name } || begin
              loaded_names = @items.empty? ? "(none)" : @items.map{ |i| i[:name] }.join("\n")
              message = <<~MESSAGE.strip
                Couldn't persist system setting #{wanted_name}. There are no items by this name. Could it be a typo?

                Configurator has loaded following items:
                #{loaded_names}
              MESSAGE
              raise(SystemSettings::Errors::NotLoadedError, message)
            end
            create_or_update_item(item)
          end
        end
      end
      payload[:success] = true
    else
      warn "SystemSettings: Settings table has not been created!"
      payload[:success] = false
    end
  end
end

#purgeObject



96
97
98
99
100
101
102
103
104
105
# File 'app/models/system_settings/configurator.rb', line 96

def purge
  SystemSettings.instrument("system_settings.purge") do |payload|
    if settings_table_exists?
      SystemSettings::Setting.delete_all
      payload[:success] = true
    else
      payload[:success] = false
    end
  end
end

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



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

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



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

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