Sha256: abc7a07913db1ed943af138992f83a5e97718a2174765017b949af551fab85aa

Contents?: true

Size: 889 Bytes

Versions: 16

Compression:

Stored size: 889 Bytes

Contents

module TrkDatatables
  class Preferences
    KEY_IN_PREFERENCES = :trk_datatables
    def initialize(holder, field, class_name)
      @holder = holder
      @field = field
      @class_name = class_name
    end

    # 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
    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?
      return result if check_value.call result
    end

    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
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
trk_datatables-0.2.15 lib/trk_datatables/preferences.rb
trk_datatables-0.2.14 lib/trk_datatables/preferences.rb
trk_datatables-0.2.13 lib/trk_datatables/preferences.rb
trk_datatables-0.2.12 lib/trk_datatables/preferences.rb
trk_datatables-0.2.11 lib/trk_datatables/preferences.rb
trk_datatables-0.2.10 lib/trk_datatables/preferences.rb
trk_datatables-0.2.9 lib/trk_datatables/preferences.rb
trk_datatables-0.2.8 lib/trk_datatables/preferences.rb
trk_datatables-0.2.7 lib/trk_datatables/preferences.rb
trk_datatables-0.2.6 lib/trk_datatables/preferences.rb
trk_datatables-0.2.5 lib/trk_datatables/preferences.rb
trk_datatables-0.2.4 lib/trk_datatables/preferences.rb
trk_datatables-0.2.3 lib/trk_datatables/preferences.rb
trk_datatables-0.2.2 lib/trk_datatables/preferences.rb
trk_datatables-0.2.1 lib/trk_datatables/preferences.rb
trk_datatables-0.1.24 lib/trk_datatables/preferences.rb