Class: TrkDatatables::Preferences

Inherits:
Object
  • Object
show all
Defined in:
lib/trk_datatables/preferences.rb

Constant Summary collapse

KEY_IN_PREFERENCES =
"trk_datatables"

Instance Method Summary collapse

Constructor Details

#initialize(holder, field, class_name) ⇒ Preferences

Returns a new instance of Preferences.



4
5
6
7
8
# File 'lib/trk_datatables/preferences.rb', line 4

def initialize(holder, field, class_name)
  @holder = holder
  @field = field
  @class_name = class_name
end

Instance Method Details

#get(key, check_value = nil) ⇒ Object

Get the key from holder Use check_value proc to ignore wrong format. This is usefull when you change format and you do not want to clear all existing values



13
14
15
16
17
18
19
# File 'lib/trk_datatables/preferences.rb', line 13

def get(key, check_value = nil)
  return unless @holder

  result = @holder.send(@field).dig KEY_IN_PREFERENCES, @class_name, key
  return result if check_value.nil?
  result if check_value.call result
end

#set(key, value) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/trk_datatables/preferences.rb', line 21

def set(key, value)
  return unless @holder

  h = {KEY_IN_PREFERENCES => {@class_name => {key => value}}}
  @holder.send(:"#{@field}=", {}) if @holder.send(@field).nil?
  @holder.send(@field).deep_merge! h
  @holder.save!
end