Class: Skyline::UserPreference

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/skyline/user_preference.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(key) ⇒ Object



18
19
20
21
22
23
# File 'app/models/skyline/user_preference.rb', line 18

def get(key)
  user_preferences = self.find(:all, :conditions => "`#{self.table_name}`.`key` LIKE \"#{key}.%\"")
  
  return nil unless user_preferences.any?
  user_preferences.size == 1 && user_preferences.first.key == "#{key}." ? user_preferences.first.value : expand_preferences(user_preferences, key)
end

.has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/skyline/user_preference.rb', line 25

def has_key?(key)
  self.exists?(["`#{self.table_name}`.`key` LIKE ?", "#{key}.%"])
end

.remove_key(key) ⇒ Object



29
30
31
# File 'app/models/skyline/user_preference.rb', line 29

def remove_key(key)
  self.delete_all("`#{self.table_name}`.`key` LIKE '#{key}.%'")
end

.set(key, value) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
# File 'app/models/skyline/user_preference.rb', line 7

def set(key, value)
  raise ArgumentError.new("Cannot put hash into a value") if !parent_is_hash?(key)
  prefs = flatten_preferences({key => value})
  
  prefs.each do |k, v|
    joined_key = "#{k.join('.')}."
    self.delete_all("`#{self.table_name}`.`key` LIKE '#{joined_key}%'")
    self.create(:key => joined_key, :encoded_value => v.to_yaml)
  end
end

Instance Method Details

#valueObject

return unserialized value



66
67
68
# File 'app/models/skyline/user_preference.rb', line 66

def value()
  return ::YAML.parse(self.encoded_value).transform
end