0
+ def has_preferences(*options)
0
+ config = { :column => 'preferences' }
0
+ %w(options instance).each do |reserved|
0
+ if options.include?(reserved.to_sym)
0
+ raise ArgumentError.new("Cannot use reserved key '#{reserved}' as a preference")
0
+ idx = 0; @@preference_options = {}
0
+ options.each do |pref|
0
+ @@preference_options[2**idx] = { :key => pref.to_sym, :default => false }
0
+ def preferences_column
0
+ def set_default_preference(key, default)
0
+ raise ArgumentError.new("Default value must be boolean") unless [true, false].include?(default)
0
+ idx = preference_options.find { |idx, hsh| hsh[:key] == key.to_sym }.first rescue nil
0
+ preference_options[idx][:default] = default
0
+ def preference_options
0
+ module InstanceMethods
0
+ @preferences_object ||= Preferences.new(read_attribute(preferences_column.to_sym), self)
0
+ attr_accessor :instance, :options
0
+ def initialize(prefs, instance)
0
+ @options = instance.class.preference_options
0
+ # setup defaults if prefs is nil
0
+ @options.each do |idx, hsh|
0
+ instance_variable_set("@#{hsh[:key]}", hsh[:default])
0
+ elsif prefs.is_a?(Numeric)
0
+ @options.each do |idx, hsh|
0
+ instance_variable_set("@#{hsh[:key]}", (prefs & idx) != 0 ? true : false)
0
+ raise(ArgumentError, "Input must be numeric")
0
+ instance_variable_get("@#{key}")
0
+ idx, hsh = lookup(key)
0
+ instance_variable_set("@#{key}", value)
0
+ @options.inject(0) do |bv, (idx, hsh)|
0
+ bv |= instance_variable_get("@#{hsh[:key]}") ? idx : 0
0
+ def update_permissions
0
+ instance.write_attribute(instance.preferences_column, self.to_i)
0
+ when true, 1, /1|y|yes/i then true
0
+ @options.find { |idx, hsh| hsh[:key] == key.to_sym }
0
+ def self.included(receiver)
0
+ receiver.extend ClassMethods
0
+ receiver.send :include, InstanceMethods
0
+# def has_preferences(*attrs)
0
+# Preferences.set_options = *attrs
0
+# composed_of :preferences, :class_name => 'PreferenceFu::Preferences'
0
+# cattr_accessor :options
0
+# def set_options=(hsh)
0
+# idx = 0; @@options = {}
0
+# hsh.each do |pref, default|
0
+# @@options[2**idx] = { :key => pref.to_sym, :default => default }
0
+# attr_reader pref.to_sym
0
+# def initialize(prefs)
0
+# if prefs.nil? or prefs.is_a?(Hash)
0
+# @@options.each do |idx, hsh|
0
+# instance_variable_set("@#{hsh[:key]}", hsh[:default])
0
+# prefs.each do |key, value|
0
+# instance_variable_set("@#{key}", true) if is_true(value)
0
+# elsif prefs.is_a?(Numeric)
0
+# @@options.each do |idx, hsh|
0
+# instance_variable_set("@#{hsh[:key]}", (prefs & idx) != 0 ? true : false)
0
+# # true if key exists and is true
0
+# instance_variable_get("@#{key}")
0
+# # composed_of is immutable, and thus can only update with a new object
0
+# # idx, hsh = lookup(key)
0
+# # @@options[idx][:value] = value
0
+# # instance_variable_set("@#{key}", value)
0
+# Preferences.new(:download_stuff => true)
0
+# @@options.inject(0) do |bv, (idx, hsh)|
0
+# bv |= instance_variable_get("@#{hsh[:key]}") ? idx : 0
0
+# when true, 1, /1|y|yes/i then true
0
+# @@options.find { |idx, hsh| hsh[:key] == key.to_sym }
0
\ No newline at end of file
Comments
No one has commented yet.