Class: Dunlop::UserSettings

Inherits:
Object
  • Object
show all
Defined in:
app/services/dunlop/user_settings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, storage) ⇒ UserSettings

Returns a new instance of UserSettings.



3
4
5
# File 'app/services/dunlop/user_settings.rb', line 3

def initialize(record, storage)
  @record, @storage = record, storage
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



2
3
4
# File 'app/services/dunlop/user_settings.rb', line 2

def record
  @record
end

#storageObject (readonly)

Returns the value of attribute storage.



2
3
4
# File 'app/services/dunlop/user_settings.rb', line 2

def storage
  @storage
end

Instance Method Details

#get(*path_spec) ⇒ Object



7
8
9
10
11
12
13
14
# File 'app/services/dunlop/user_settings.rb', line 7

def get(*path_spec)
  current_settings_level = storage
  Array.wrap(path_spec).each do |level|
    current_settings_level = current_settings_level[level.to_s]
    return nil unless current_settings_level
  end
  current_settings_level
end

#set(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/dunlop/user_settings.rb', line 16

def set(*args)
  value = args.pop
  if value.is_a?(Hash) and args.empty? # settings.set(a: 4) assignment
    value.each do |k, v|
      set(k, v)
    end
  else
    value = value.deep_stringify_keys if value.respond_to?(:deep_stringify_keys)
    args = args.first if args.first.is_a?(Array)
    current_settings_level = storage
    Array.wrap(args)[0..-2].each do |level|
      current_settings_level[level.to_s] ||= {}
      current_settings_level = current_settings_level[level.to_s]
    end
    current_settings_level[args.last.to_s] = value
    record.settings_storage = storage
  end
  value
end

#set!(*args) ⇒ Object



36
37
38
39
# File 'app/services/dunlop/user_settings.rb', line 36

def set!(*args)
  set(*args)
  record.save
end