Module: Flavors::Preferences::ClassMethods

Defined in:
lib/flavors/preferences/preferences.rb

Instance Method Summary collapse

Instance Method Details

#preference(name, options = {}, &callback) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flavors/preferences/preferences.rb', line 9

def preference(name, options = {}, &callback)
  has_many :preferences, :as => :prefered, :class_name => "::Flavors::Preference"

  define_method(name) do
    read_preference(name, options[:default])
  end

  define_method("#{name}?") do
    read_preference(name, options[:default])
  end

  define_method("#{name}=") do |value|
    write_preference(name, value)
    callback.call(self, value) if callback
  end

  (@@preferences ||= Set.new).add name
end

#reflections_of_preferencesObject



28
29
30
# File 'lib/flavors/preferences/preferences.rb', line 28

def reflections_of_preferences
  @@preferences.select { |name| self.new.respond_to? name }
end