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:
Initial functioning version - still no unit tests or polish
Brennan Dunn (author)
Mon Mar 24 11:29:47 -0700 2008
commit  0e28ad8afa39d57bbd523a2c4a95e41245a0e7b9
tree    e7d60d0e9f5d9e177945f5e21e89f1dc3173187a
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -0,0 +1,20 @@
0
+Copyright (c) 2008 [name of plugin creator]
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
...
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -0,0 +1,13 @@
0
+PreferenceFu
0
+============
0
+
0
+Introduction goes here.
0
+
0
+
0
+Example
0
+=======
0
+
0
+Example goes here.
0
+
0
+
0
+Copyright (c) 2008 [name of plugin creator], released under the MIT license
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
0
@@ -0,0 +1,22 @@
0
+require 'rake'
0
+require 'rake/testtask'
0
+require 'rake/rdoctask'
0
+
0
+desc 'Default: run unit tests.'
0
+task :default => :test
0
+
0
+desc 'Test the preference_fu plugin.'
0
+Rake::TestTask.new(:test) do |t|
0
+ t.libs << 'lib'
0
+ t.pattern = 'test/**/*_test.rb'
0
+ t.verbose = true
0
+end
0
+
0
+desc 'Generate documentation for the preference_fu plugin.'
0
+Rake::RDocTask.new(:rdoc) do |rdoc|
0
+ rdoc.rdoc_dir = 'rdoc'
0
+ rdoc.title = 'PreferenceFu'
0
+ rdoc.options << '--line-numbers' << '--inline-source'
0
+ rdoc.rdoc_files.include('README')
0
+ rdoc.rdoc_files.include('lib/**/*.rb')
0
+end
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+# Include hook code here
0
+
0
+ActiveRecord::Base.class_eval { include PreferenceFu }
0
\ No newline at end of file
...
 
...
1
0
@@ -0,0 +1 @@
0
+# Install hook code here
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
0
@@ -0,0 +1,208 @@
0
+module PreferenceFu
0
+
0
+ module ClassMethods
0
+
0
+ def has_preferences(*options)
0
+ config = { :column => 'preferences' }
0
+
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
+ end
0
+ end
0
+
0
+ idx = 0; @@preference_options = {}
0
+ options.each do |pref|
0
+ @@preference_options[2**idx] = { :key => pref.to_sym, :default => false }
0
+ idx += 1
0
+ end
0
+
0
+ class_eval <<-EOV
0
+
0
+ def preferences_column
0
+ '#{config[:column]}'
0
+ end
0
+
0
+ EOV
0
+
0
+ end
0
+
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
+ if idx
0
+ preference_options[idx][:default] = default
0
+ end
0
+ end
0
+
0
+ def preference_options
0
+ @@preference_options
0
+ end
0
+
0
+ end
0
+
0
+ module InstanceMethods
0
+
0
+ def preferences
0
+ @preferences_object ||= Preferences.new(read_attribute(preferences_column.to_sym), self)
0
+ end
0
+
0
+ end
0
+
0
+
0
+ class Preferences
0
+
0
+ attr_accessor :instance, :options
0
+
0
+ def initialize(prefs, instance)
0
+ @instance = instance
0
+ @options = instance.class.preference_options
0
+
0
+ # setup defaults if prefs is nil
0
+ if prefs.nil?
0
+ @options.each do |idx, hsh|
0
+ instance_variable_set("@#{hsh[:key]}", hsh[:default])
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
+ else
0
+ raise(ArgumentError, "Input must be numeric")
0
+ end
0
+
0
+ end
0
+
0
+ def [](key)
0
+ instance_variable_get("@#{key}")
0
+ end
0
+
0
+ def []=(key, value)
0
+ idx, hsh = lookup(key)
0
+ instance_variable_set("@#{key}", value)
0
+ update_permissions
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
+ end
0
+ end
0
+
0
+ private
0
+
0
+ def update_permissions
0
+ instance.write_attribute(instance.preferences_column, self.to_i)
0
+ end
0
+
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
+ def self.included(receiver)
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
...
 
 
 
 
...
1
2
3
4
0
@@ -0,0 +1,4 @@
0
+# desc "Explaining what the task does"
0
+# task :preference_fu do
0
+# # Task goes here
0
+# end
...
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
0
@@ -0,0 +1,8 @@
0
+require 'test/unit'
0
+
0
+class PreferenceFuTest < Test::Unit::TestCase
0
+ # Replace this with your real tests.
0
+ def test_this_plugin
0
+ flunk
0
+ end
0
+end
...
 
...
1
0
@@ -0,0 +1 @@
0
+# Uninstall hook code here

Comments

    No one has commented yet.