Module: Preferred::Preferable

Extended by:
ActiveSupport::Concern
Defined in:
lib/preferred/preferable.rb

Instance Method Summary collapse

Instance Method Details

#clear_preferencesObject



51
52
53
# File 'lib/preferred/preferable.rb', line 51

def clear_preferences
  preference_hash.keys.each {|pref| preference_hash.delete pref} if preference_hash.is_a? Hash
end

#get_preference(name) ⇒ Object Also known as: preferred, prefers?



8
9
10
11
# File 'lib/preferred/preferable.rb', line 8

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

#has_preference!(name) ⇒ Object



35
36
37
# File 'lib/preferred/preferable.rb', line 35

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

#has_preference?(name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/preferred/preferable.rb', line 39

def has_preference?(name)
  respond_to? self.class.preference_getter_method(name)
end

#preference_default(name) ⇒ Object



25
26
27
28
# File 'lib/preferred/preferable.rb', line 25

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

#preference_description(name) ⇒ Object



30
31
32
33
# File 'lib/preferred/preferable.rb', line 30

def preference_description(name)
  has_preference! name
  send self.class.preference_description_getter_method(name)
end

#preference_type(name) ⇒ Object



20
21
22
23
# File 'lib/preferred/preferable.rb', line 20

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

#preferencesObject



43
44
45
46
47
48
49
# File 'lib/preferred/preferable.rb', line 43

def preferences
  prefs = {}
  methods.grep(/^prefers_.*\?$/).each do |pref_method|
    prefs[pref_method.to_s.gsub(/prefers_|\?/, '').to_sym] = send(pref_method)
  end
  prefs
end

#set_preference(name, value) ⇒ Object



15
16
17
18
# File 'lib/preferred/preferable.rb', line 15

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