Sha256: 0fba2fbd43aa5b31d98c878c531380c7eb1df4695fe14ba373ff4d231714d6a4
Contents?: true
Size: 1.26 KB
Versions: 2
Compression:
Stored size: 1.26 KB
Contents
module Preferences # Represents the definition of a preference for a particular model class PreferenceDefinition def initialize(name, *args) #:nodoc: options = args.extract_options! options.assert_valid_keys(:default) @type = args.first ? args.first.to_s : 'boolean' # Create a column that will be responsible for typecasting @column = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], @type == 'any' ? nil : @type) end # The name of the preference def name @column.name end # The default value to use for the preference in case none have been # previously defined def default_value @column.default end # Typecasts the value based on the type of preference that was defined. # This uses ActiveRecord's typecast functionality so the same rules for # typecasting a model's columns apply here. def type_cast(value) @type == 'any' ? value : @column.type_cast(value) end # Typecasts the value to true/false depending on the type of preference def query(value) if !(value = type_cast(value)) false elsif @column.number? !value.zero? else !value.blank? end end end end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
pluginaweek-preferences-0.3.1 | lib/preferences/preference_definition.rb |
preferences-0.3.1 | lib/preferences/preference_definition.rb |