Class: Releaf::Settings::Register

Inherits:
Object
  • Object
show all
Includes:
Releaf::Service
Defined in:
app/lib/releaf/settings/register.rb

Instance Method Summary collapse

Instance Method Details

#allowed_keysObject


21
22
23
# File 'app/lib/releaf/settings/register.rb', line 21

def allowed_keys
  [:key, :default, :type, :description]
end

#callObject


5
6
7
8
9
# File 'app/lib/releaf/settings/register.rb', line 5

def call
  settings.each do|item|
    register(normalize(item))
  end
end

#normalize(item) ⇒ Object

Raises:


11
12
13
14
15
16
17
18
19
# File 'app/lib/releaf/settings/register.rb', line 11

def normalize(item)
  item[:type] = (item[:type] || :text).to_sym
  dissallowed_keys = item.keys - allowed_keys

  raise Releaf::Error, "Unsupported settings type: #{item[:type]}" unless settings_class.supported_types.include?(item[:type])
  raise Releaf::Error, "Dissallowed settings keys: #{dissallowed_keys}" if dissallowed_keys.present?

  item
end

#register(item) ⇒ Object


25
26
27
28
# File 'app/lib/releaf/settings/register.rb', line 25

def register(item)
  settings_class.registry.update(item[:key] => item)
  settings_class[item[:key]] = item[:default] if write_default?(item)
end

#settings_classObject


42
43
44
# File 'app/lib/releaf/settings/register.rb', line 42

def settings_class
  Releaf::Settings
end

#table_exists?Boolean

Returns:

  • (Boolean)

34
35
36
37
38
39
40
# File 'app/lib/releaf/settings/register.rb', line 34

def table_exists?
  begin
    settings_class.table_exists?
  rescue ActiveRecord::NoDatabaseError
    false
  end
end

#write_default?(item) ⇒ Boolean

Returns:

  • (Boolean)

30
31
32
# File 'app/lib/releaf/settings/register.rb', line 30

def write_default?(item)
  table_exists? && !settings_class.find_by(var: item[:key]).present?
end