Module: Preferred::PreferableClassMethods

Defined in:
lib/preferred/preferable_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#preference(name, type, *args) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/preferred/preferable_class_methods.rb', line 3

def preference(name, type, *args)
  options = args.extract_options!
  options.assert_valid_keys(:default, :description)
  default = options[:default]
  description = options[:description] || name

  # cache_key will be nil for new objects, then if we check if there
  # is a pending preference before going to default
  define_method preference_getter_method(name) do
    if self.preference_hash && preference_hash.key?(name.to_s)
      convert_preference_value(preference_hash[name.to_s]["value"], preference_hash[name.to_s]["type"])
    else
      send self.class.preference_default_getter_method(name)
    end
  end

  define_method preference_setter_method(name) do |value|
    value = convert_preference_value(value, type)
    self.preference_hash = {} if preference_hash.nil?
    self.preference_hash[name.to_s] = {
      value: value,
      type: type
    }
  end

  define_method preference_default_getter_method(name) do
    default
  end

  define_method preference_type_getter_method(name) do
    type
  end

  define_method preference_description_getter_method(name) do
    description
  end
end

#preference_default_getter_method(name) ⇒ Object



57
58
59
# File 'lib/preferred/preferable_class_methods.rb', line 57

def preference_default_getter_method(name)
  "preferred_#{name}_default".to_sym
end

#preference_description_getter_method(name) ⇒ Object



65
66
67
# File 'lib/preferred/preferable_class_methods.rb', line 65

def preference_description_getter_method(name)
  "preferred_#{name}_description".to_sym
end

#preference_getter_method(name) ⇒ Object



49
50
51
# File 'lib/preferred/preferable_class_methods.rb', line 49

def preference_getter_method(name)
  "preferred_#{name}".to_sym
end

#preference_setter_method(name) ⇒ Object



53
54
55
# File 'lib/preferred/preferable_class_methods.rb', line 53

def preference_setter_method(name)
   "preferred_#{name}=".to_sym
end

#preference_type_getter_method(name) ⇒ Object



61
62
63
# File 'lib/preferred/preferable_class_methods.rb', line 61

def preference_type_getter_method(name)
  "preferred_#{name}_type".to_sym
end

#remove_preference(name) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/preferred/preferable_class_methods.rb', line 41

def remove_preference(name)
  remove_method preference_getter_method(name) if method_defined? preference_getter_method(name)
  remove_method preference_setter_method(name) if method_defined? preference_setter_method(name)
  remove_method preference_default_getter_method(name) if method_defined? preference_default_getter_method(name)
  remove_method preference_type_getter_method(name) if method_defined? preference_type_getter_method(name)
  remove_method preference_description_getter_method(name) if method_defined? preference_description_getter_method(name)
end