Class: ActsAsPreferenced::Preference

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_preferenced/preference.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {} )
  options.assert_valid_keys( :section, :label, :choices, :default )
  @store, @name = store, name
  @section = options[:section]
  @label = options[:label]
  @choices = options[:choices]
  @default = options[:default]
  store.sections[@section] << self
  store.all_preferences << self
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



4
5
6
# File 'lib/acts_as_preferenced/preference.rb', line 4

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/acts_as_preferenced/preference.rb', line 4

def name
  @name
end

#sectionObject (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

#choicesObject

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

#labelObject



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

Returns:

  • (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