class UserPreferences::PreferenceDefinition

Attributes

category[R]
name[R]

Public Class Methods

new(definition, category, name) click to toggle source
# File lib/user_preferences/preference_definition.rb, line 5
def initialize(definition, category, name)
  @definition = definition
  @category = category
  @name = name
end

Public Instance Methods

binary?() click to toggle source
# File lib/user_preferences/preference_definition.rb, line 19
def binary?
  !@definition.is_a?(Hash)
end
default() click to toggle source
# File lib/user_preferences/preference_definition.rb, line 23
def default
  binary? ? @definition : @definition[:default]
end
lookup(index) click to toggle source
# File lib/user_preferences/preference_definition.rb, line 27
def lookup(index)
  permitted_values[index] if index
end
permitted_values() click to toggle source
# File lib/user_preferences/preference_definition.rb, line 11
def permitted_values
  if binary?
    result = [false, true]
  else
    @definition[:values]
  end
end
to_db(value) click to toggle source
# File lib/user_preferences/preference_definition.rb, line 31
def to_db(value)
  value = to_bool(value) if binary?
  permitted_values.index(value)
end

Private Instance Methods

to_bool(value) click to toggle source
# File lib/user_preferences/preference_definition.rb, line 38
def to_bool(value)
  return true if value == 1
  return true if value == true || value =~ (/^(true|t|yes|y|1)$/i)

  return false if value == 0
  return false if value == false || value.blank? || value =~ (/^(false|f|no|n|0)$/i)
  raise ArgumentError.new("invalid value for Boolean: \"#{value}\"")
end