Module: Spree::Preferences::Preferable

Extended by:
ActiveSupport::Concern
Included in:
Base, Configuration
Defined in:
lib/spree/preferences/preferable.rb

Instance Method Summary collapse

Instance Method Details

#default_preferencesHash{Symbol => Object}

Returns Default for all preferences defined on this class

Returns:

  • (Hash{Symbol => Object})

    Default for all preferences defined on this class



96
97
98
99
100
101
102
# File 'lib/spree/preferences/preferable.rb', line 96

def default_preferences
  Hash[
    defined_preferences.map do |preference|
      [preference, preference_default(preference)]
    end
  ]
end

#defined_preferencesArray<Symbol>

Returns All preferences defined on this class

Returns:

  • (Array<Symbol>)

    All preferences defined on this class



91
92
93
# File 'lib/spree/preferences/preferable.rb', line 91

def defined_preferences
  self.class.defined_preferences
end

#get_preference(name) ⇒ Object

Get a preference

Parameters:

  • name (#to_sym)

    name of preference

Returns:

  • (Object)

    The value of preference name



51
52
53
54
# File 'lib/spree/preferences/preferable.rb', line 51

def get_preference(name)
  has_preference! name
  send self.class.preference_getter_method(name)
end

#has_preference!(name) ⇒ Object

Raises an exception if the name preference is not defined on this class

Parameters:

  • name (#to_sym)

    name of preference



80
81
82
# File 'lib/spree/preferences/preferable.rb', line 80

def has_preference!(name)
  raise NoMethodError.new "#{name} preference not defined" unless has_preference? name
end

#has_preference?(name) ⇒ Boolean

Returns if preference exists on this class

Parameters:

  • name (#to_sym)

    name of preference

Returns:

  • (Boolean)

    if preference exists on this class



86
87
88
# File 'lib/spree/preferences/preferable.rb', line 86

def has_preference?(name)
  defined_preferences.include?(name.to_sym)
end

#preference_default(name) ⇒ Object

Returns The default for preference name

Parameters:

  • name (#to_sym)

    name of preference

Returns:

  • (Object)

    The default for preference name



73
74
75
76
# File 'lib/spree/preferences/preferable.rb', line 73

def preference_default(name)
  has_preference! name
  send self.class.preference_default_getter_method(name)
end

#preference_type(name) ⇒ Symbol

Returns The type of preference name

Parameters:

  • name (#to_sym)

    name of preference

Returns:

  • (Symbol)

    The type of preference name



66
67
68
69
# File 'lib/spree/preferences/preferable.rb', line 66

def preference_type(name)
  has_preference! name
  send self.class.preference_type_getter_method(name)
end

#set_preference(name, value) ⇒ Object

Set a preference

Parameters:

  • name (#to_sym)

    name of preference

  • value (Object)

    new value for preference name



59
60
61
62
# File 'lib/spree/preferences/preferable.rb', line 59

def set_preference(name, value)
  has_preference! name
  send self.class.preference_setter_method(name), value
end