Sha256: c83fde1cb8e6a5d3b4ba3df8c7c8c004f0aaf8fd7be8743acfcc9d5ecd41c6e8

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module UserPreferences
  class API
    def initialize(category, scope)
      @category = category
      @scope = scope.where(category: category)
    end

    def all
      serialized_preferences
    end

    def get(name)
      serialized_preferences[name]
    end

    def set(hash)
      hash_setter do
        hash.each do |name, value|
          find_or_init_preference(name).update_value!(value)
        end
        reload
      end
    end

    def reload
      @_saved_preferences = nil
      all
    end

    private

    def serialized_preferences
      default_preferences.merge Hash[saved_preferences.map { |p| [p.name.to_sym, p.value] }]
    end

    def default_preferences
      @_category_defaults ||= UserPreferences.defaults(@category)
    end

    def saved_preferences
      @_saved_preferences ||= @scope.select([:id, :category, :name, :value, :user_id])
    end

    def find_or_init_preference(name)
      @scope.find_or_initialize_by(name: name, category: @category)
    end

    def hash_setter(&block)
      ActiveRecord::Base.transaction do
        result = true
        begin
          yield
        rescue ActiveRecord::RecordInvalid
          result = false
        end
        result
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
user_preferences-1.0.2 lib/user_preferences/api.rb
user_preferences-1.0.1 lib/user_preferences/api.rb
user_preferences-1.0.0 lib/user_preferences/api.rb