Class: Preferences::PreferenceDefinition
- Inherits:
-
Object
- Object
- Preferences::PreferenceDefinition
- Defined in:
- lib/preferences/preference_definition.rb
Overview
Represents the definition of a preference for a particular model
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
The data type for the content stored in this preference type.
Instance Method Summary collapse
-
#default_value(group = nil) ⇒ Object
The default value to use for the preference in case none have been previously defined.
-
#initialize(name, *args) ⇒ PreferenceDefinition
constructor
:nodoc:.
-
#name ⇒ Object
The name of the preference.
-
#number? ⇒ Boolean
Determines whether column backing this preference stores numberic values.
-
#query(value) ⇒ Object
Typecasts the value to true/false depending on the type of preference.
-
#type_cast(value) ⇒ Object
Typecasts the value based on the type of preference that was defined.
Constructor Details
#initialize(name, *args) ⇒ PreferenceDefinition
:nodoc:
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/preferences/preference_definition.rb', line 7 def initialize(name, *args) #:nodoc: = args. .assert_valid_keys(:default, :group_defaults) @type = args.first ? args.first.to_sym : :boolean # Create a column that will be responsible for typecasting @column = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, [:default], @type == :any ? nil : @type.to_s) @group_defaults = ([:group_defaults] || {}).inject({}) do |defaults, (group, default)| defaults[group.is_a?(Symbol) ? group.to_s : group] = type_cast(default) defaults end end |
Instance Attribute Details
#type ⇒ Object (readonly)
The data type for the content stored in this preference type
5 6 7 |
# File 'lib/preferences/preference_definition.rb', line 5 def type @type end |
Instance Method Details
#default_value(group = nil) ⇒ Object
The default value to use for the preference in case none have been previously defined
29 30 31 |
# File 'lib/preferences/preference_definition.rb', line 29 def default_value(group = nil) @group_defaults.include?(group) ? @group_defaults[group] : @column.default end |
#name ⇒ Object
The name of the preference
23 24 25 |
# File 'lib/preferences/preference_definition.rb', line 23 def name @column.name end |
#number? ⇒ Boolean
Determines whether column backing this preference stores numberic values
34 35 36 |
# File 'lib/preferences/preference_definition.rb', line 34 def number? @column.number? end |
#query(value) ⇒ Object
Typecasts the value to true/false depending on the type of preference
46 47 48 49 50 51 52 53 54 |
# File 'lib/preferences/preference_definition.rb', line 46 def query(value) if !(value = type_cast(value)) false elsif number? !value.zero? else !value.blank? end end |
#type_cast(value) ⇒ Object
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.
41 42 43 |
# File 'lib/preferences/preference_definition.rb', line 41 def type_cast(value) @type == :any ? value : @column.type_cast(value) end |