Class: Setting::NameGenerator

Inherits:
Setting
  • Object
show all
Defined in:
app/models/setting/name_generator.rb

Constant Summary collapse

NAME_GENERATOR_MUTEX =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_defaultsObject



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

def self.load_defaults
  # Check the table exists
  return unless super

  Setting.transaction do
    [
      self.set('name_generator_type', N_("Type of name generator"), 'OFF', N_("Generator type")),
      self.set('name_generator_seed', N_("Initial LFSR seed value (do not change)"), -1, N_("Generator seed")),
      self.set('name_generator_register', N_("Current LFSR register (change with care, can cause name conflicts)"), -1, N_("Generator register")),
    ].compact.each { |s| self.create s.update(:category => "Setting::NameGenerator")}
  end

  true
end

.next_generated_nameObject



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

def self.next_generated_name
  NAME_GENERATOR_MUTEX.synchronize do
    generator = ForemanNameGenerator::RandomGenerator.new
    setting = Setting::NameGenerator.find_by_name 'name_generator_register'
    result = generator.generate(setting.value)
    Rails.logger.debug "Register moved to #{result[0]}, generated name: #{result[1]} #{result[2]}"
    setting.value = result.shift
    setting.save!
    result
  end
end

Instance Method Details

#validate_name_generator_seed(record) ⇒ Object



21
22
23
24
25
# File 'app/models/setting/name_generator.rb', line 21

def validate_name_generator_seed(record)
  if record.value && record.value < 1 && record.value != -1
    record.errors[:base] << _("Generator seed value must be greater than zero or -1")
  end
end