Class: ActsAsPreferenced::Preference
- Inherits:
-
Object
- Object
- ActsAsPreferenced::Preference
- Defined in:
- lib/acts_as_preferenced/preference.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#section ⇒ Object
readonly
Returns the value of attribute section.
Instance Method Summary collapse
-
#choices ⇒ Object
Options translations are stored under hardcoded :choices key in Preferences translation_scope.
-
#initialize(store, name, options = {}) ⇒ Preference
constructor
A new instance of Preference.
- #label ⇒ Object
- #value_valid?(value) ⇒ Boolean
Constructor Details
#initialize(store, name, options = {}) ⇒ Preference
Returns a new instance of Preference.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/acts_as_preferenced/preference.rb', line 6 def initialize( store, name, = {} ) .assert_valid_keys( :section, :label, :choices, :default ) @store, @name = store, name @section = [:section] @label = [:label] @choices = [:choices] @default = [:default] store.sections[@section] << self store.all_preferences << self end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
4 5 6 |
# File 'lib/acts_as_preferenced/preference.rb', line 4 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/acts_as_preferenced/preference.rb', line 4 def name @name end |
#section ⇒ Object (readonly)
Returns the value of attribute section.
4 5 6 |
# File 'lib/acts_as_preferenced/preference.rb', line 4 def section @section end |
Instance Method Details
#choices ⇒ Object
Options translations are stored under hardcoded :choices key in Preferences translation_scope. TODO: make it not hardcoded and make it work for other options than Array (eg. Range?) TODO: it should be cached
29 30 31 32 33 34 35 36 |
# File 'lib/acts_as_preferenced/preference.rb', line 29 def choices if @choices.is_a?( Array ) # TODO: I don't quite understand checking for an array here @choices.collect { |choice| choice.is_a?( Array ) ? choice : [I18n.t( choice, :scope => [@store.translation_scope, :choices] ), choice] } else @choices end end |
#label ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/acts_as_preferenced/preference.rb', line 17 def label if @store.translation_scope && @label.nil? I18n.t( @name, :scope => [@store.translation_scope, @section] ) else @label end end |
#value_valid?(value) ⇒ Boolean
38 39 40 41 42 43 44 45 |
# File 'lib/acts_as_preferenced/preference.rb', line 38 def value_valid?( value ) value = value.to_sym if value.is_a?( String ) if @choices return @choices.include?( value ) else return [true, false].include?( value ) end end |