public
Description: Allows the storage of a number of boolean fields with just one table column
Homepage: http://www.brennandunn.com
Clone URL: git://github.com/brennandunn/preference_fu.git
Search Repo:
Added Enumerable support
Brennan Dunn (author)
Mon Mar 24 11:51:16 -0700 2008
commit  d9933c19f7fa232d30efa4d6c583bdbbe5584e85
tree    28f3bfdc0f39250b145f7dbd0889e4928bf639c0
parent  0e28ad8afa39d57bbd523a2c4a95e41245a0e7b9
...
47
48
49
 
 
 
 
50
51
52
53
54
 
 
55
56
57
...
73
74
75
 
 
 
 
 
 
 
 
 
 
76
77
78
...
83
84
85
 
 
 
 
 
 
 
86
87
88
...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
 
210
...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
...
99
100
101
102
103
104
105
106
107
108
109
110
111
...
135
136
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
139
140
0
@@ -47,11 +47,17 @@ module PreferenceFu
0
       @preferences_object ||= Preferences.new(read_attribute(preferences_column.to_sym), self)
0
     end
0
     
0
+ def preferences=(prefs)
0
+ preferences.store(prefs)
0
+ end
0
+
0
   end
0
   
0
   
0
   class Preferences
0
     
0
+ include Enumerable
0
+
0
     attr_accessor :instance, :options
0
     
0
     def initialize(prefs, instance)
0
@@ -73,6 +79,16 @@ module PreferenceFu
0
       
0
     end
0
     
0
+ def each
0
+ @options.each_value do |hsh|
0
+ yield hsh[:key], self[hsh[:key]]
0
+ end
0
+ end
0
+
0
+ def size
0
+ @options.size
0
+ end
0
+
0
     def [](key)
0
       instance_variable_get("@#{key}")
0
     end
0
@@ -83,6 +99,13 @@ module PreferenceFu
0
       update_permissions
0
     end
0
     
0
+ # used for mass assignment of preferences, such as a hash from params
0
+ def store(prefs)
0
+ prefs.each do |key, value|
0
+ self[key] = value
0
+ end if prefs.respond_to?(:each)
0
+ end
0
+
0
     def to_i
0
       @options.inject(0) do |bv, (idx, hsh)|
0
         bv |= instance_variable_get("@#{hsh[:key]}") ? idx : 0
0
@@ -112,97 +135,4 @@ module PreferenceFu
0
     receiver.extend ClassMethods
0
     receiver.send :include, InstanceMethods
0
   end
0
-end
0
-
0
-# module PreferenceFu
0
-#
0
-# def has_preferences(*attrs)
0
-#
0
-# class_eval do
0
-#
0
-# Preferences.set_options = *attrs
0
-#
0
-# composed_of :preferences, :class_name => 'PreferenceFu::Preferences'
0
-#
0
-# end
0
-#
0
-# end
0
-#
0
-# class Preferences
0
-#
0
-# cattr_accessor :options
0
-#
0
-# class << self
0
-#
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
-# idx += 1
0
-# end
0
-#
0
-# @@options
0
-# end
0
-#
0
-# end
0
-#
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
-# end
0
-# end
0
-#
0
-# if prefs.is_a?(Hash)
0
-# prefs.each do |key, value|
0
-# instance_variable_set("@#{key}", true) if is_true(value)
0
-# end
0
-# elsif prefs.is_a?(Numeric)
0
-# @@options.each do |idx, hsh|
0
-# instance_variable_set("@#{hsh[:key]}", (prefs & idx) != 0 ? true : false)
0
-# end
0
-# end
0
-#
0
-# end
0
-#
0
-# def [](key)
0
-# # true if key exists and is true
0
-# instance_variable_get("@#{key}")
0
-# end
0
-#
0
-# def set(key, value)
0
-# # composed_of is immutable, and thus can only update with a new object
0
-# # idx, hsh = lookup(key)
0
-# # if idx
0
-# # @@options[idx][:value] = value
0
-# # instance_variable_set("@#{key}", value)
0
-# # end
0
-# Preferences.new(:download_stuff => true)
0
-# end
0
-#
0
-# def to_hash
0
-# @@options
0
-# end
0
-#
0
-# def preferences
0
-# @@options.inject(0) do |bv, (idx, hsh)|
0
-# bv |= instance_variable_get("@#{hsh[:key]}") ? idx : 0
0
-# end
0
-# end
0
-#
0
-# private
0
-# def is_true(value)
0
-# case value
0
-# when true, 1, /1|y|yes/i then true
0
-# else false
0
-# end
0
-# end
0
-#
0
-# def lookup(key)
0
-# @@options.find { |idx, hsh| hsh[:key] == key.to_sym }
0
-# end
0
-#
0
-# end
0
-#
0
-# end
0
\ No newline at end of file
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.