Module: PreferenceFu::ClassMethods
- Defined in:
- lib/preference_fu.rb
Instance Method Summary collapse
- #has_preferences(*prefs) ⇒ Object
- #instantiate_with_preferences(*args) ⇒ Object
- #set_default_preference(key, default) ⇒ Object
Instance Method Details
#has_preferences(*prefs) ⇒ Object
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/preference_fu.rb', line 10 def has_preferences(*prefs) alias_method_chain :initialize, :preferences class_eval do class << self alias_method_chain :instantiate, :preferences attr_accessor :preference_options end end = prefs. preference_names=prefs-.keys @config = { :column => 'preferences' }.merge() self. = {} prefs.each_with_index do |pref, idx| self.[2**idx] = { :key => pref.to_sym, :default => false } end preference_names.each do |pref| define_method(pref.to_sym) {self.prefs[pref.to_sym]} define_method((pref.to_s+'?').to_sym) {send pref.to_sym} define_method((pref.to_s+'=').to_sym) {|a| self.prefs[pref.to_sym]=a} end class << self define_method(:preferences_column) { @config[:column] } end end |
#instantiate_with_preferences(*args) ⇒ Object
49 50 51 52 53 |
# File 'lib/preference_fu.rb', line 49 def instantiate_with_preferences(*args) record = instantiate_without_preferences(*args) record.prefs record end |
#set_default_preference(key, default) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/preference_fu.rb', line 41 def set_default_preference(key, default) raise ArgumentError.new("Default value must be boolean") unless [true, false].include?(default) idx = .find { |idx, hsh| hsh[:key] == key.to_sym }.first rescue nil if idx [idx][:default] = default end end |