dr_nic_magic_models 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +2 -2
- data/lib/dr_nic_magic_models/version.rb +1 -1
- data/scripts/http-access2-2.0.6.gem +0 -0
- data/scripts/rubyforge-orig +217 -0
- data/scripts/txt2html +65 -0
- data/website/index.html +88 -3
- data/website/index.txt +65 -1
- metadata +10 -8
- data/lib/new_base.rb +0 -105
data/Rakefile
CHANGED
@@ -86,7 +86,7 @@ end
|
|
86
86
|
|
87
87
|
# Create compressed packages
|
88
88
|
|
89
|
-
dist_dirs = [ "lib", "test", "examples", "website" ]
|
89
|
+
dist_dirs = [ "lib", "test", "examples", "website", "scripts"]
|
90
90
|
|
91
91
|
spec = Gem::Specification.new do |s|
|
92
92
|
s.name = PKG_NAME
|
@@ -99,7 +99,7 @@ spec = Gem::Specification.new do |s|
|
|
99
99
|
s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
|
100
100
|
end
|
101
101
|
|
102
|
-
s.add_dependency('activerecord', '
|
102
|
+
s.add_dependency('activerecord', '>= 1.14.3' + PKG_BUILD)
|
103
103
|
|
104
104
|
s.require_path = 'lib'
|
105
105
|
s.autorequire = 'dr_nic_magic_models'
|
Binary file
|
@@ -0,0 +1,217 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# RENAME THIS to rubyorge, and change username/password below to your rubyforge details.
|
4
|
+
# Set rubyforge file to Ignore on Subversion.
|
5
|
+
# libs
|
6
|
+
#
|
7
|
+
$VERBOSE = nil
|
8
|
+
require "getoptlong"
|
9
|
+
require "enumerator"
|
10
|
+
require "http-access2"
|
11
|
+
#
|
12
|
+
# config
|
13
|
+
#
|
14
|
+
config = {
|
15
|
+
"username" => "username", # this must be your username
|
16
|
+
"password" => "password", # this must be your password
|
17
|
+
|
18
|
+
"group_id" => 1953, # see source of http://rubyforge.org/frs/admin/qrs.php?package=&group_id=1953
|
19
|
+
|
20
|
+
"package_ids" => { # configure shortcuts for your packages here
|
21
|
+
"compositekeys" => 2327,
|
22
|
+
},
|
23
|
+
|
24
|
+
"uri" => "http://rubyforge.org",
|
25
|
+
"cookie_jar" => File::join(File::expand_path("~"), ".rubyforge.cookie_jar"),
|
26
|
+
}
|
27
|
+
|
28
|
+
opts =
|
29
|
+
GetoptLong::new(
|
30
|
+
[ "--username" , "-u" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
31
|
+
[ "--password" , "-p" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
32
|
+
[ "--cookie_jar" , "-c" , GetoptLong::REQUIRED_ARGUMENT ]
|
33
|
+
).enum_for.inject({}){|h,kv| h.update Hash[*kv]}
|
34
|
+
|
35
|
+
username = opts['username'] || config['username']
|
36
|
+
password = opts['password'] || config['password']
|
37
|
+
cookie_jar = opts["cookie_jar"] || config['cookie_jar']
|
38
|
+
#
|
39
|
+
# setup
|
40
|
+
#
|
41
|
+
mode = ARGV.shift
|
42
|
+
abort "#{ $0 } [login|create_package (package_name)|add_package (package_id release_name release.ext)]" unless mode
|
43
|
+
|
44
|
+
page, form, method = nil
|
45
|
+
extheader = {}
|
46
|
+
|
47
|
+
case mode
|
48
|
+
|
49
|
+
when %r/login/
|
50
|
+
page = "/account/login.php"
|
51
|
+
method = "post_content"
|
52
|
+
|
53
|
+
form = {
|
54
|
+
"return_to" => "",
|
55
|
+
"form_loginname" => username,
|
56
|
+
"form_pw" => password,
|
57
|
+
"login" => "Login"
|
58
|
+
}
|
59
|
+
|
60
|
+
when %r/create_package/
|
61
|
+
page = "/frs/admin/index.php"
|
62
|
+
method = "post_content"
|
63
|
+
|
64
|
+
opts =
|
65
|
+
GetoptLong::new(
|
66
|
+
[ "--group_id" , "-g" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
67
|
+
[ "--is_private" , "-P" , GetoptLong::REQUIRED_ARGUMENT ]
|
68
|
+
).enum_for.inject({}){|h,kv| h.update Hash[*kv]}
|
69
|
+
|
70
|
+
package_name = ARGV.shift
|
71
|
+
abort "#{ $0 } package_name" unless package_name
|
72
|
+
|
73
|
+
group_id = opts["group_id"] || config["group_id"]
|
74
|
+
is_public = opts["is_private"] ? 0 : 1
|
75
|
+
|
76
|
+
form = {
|
77
|
+
"group_id" => group_id,
|
78
|
+
"package_name" => package_name,
|
79
|
+
"func" => "add_package",
|
80
|
+
"is_public" => is_public,
|
81
|
+
"submit" => "Create This Package",
|
82
|
+
}
|
83
|
+
|
84
|
+
when %r/add_release/
|
85
|
+
page = "/frs/admin/qrs.php"
|
86
|
+
method = "post_content"
|
87
|
+
|
88
|
+
opts =
|
89
|
+
GetoptLong::new(
|
90
|
+
[ "--group_id" , "-g" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
91
|
+
[ "--release_date" , "-r" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
92
|
+
[ "--type_id" , "-t" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
93
|
+
[ "--processor_id" , "-P" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
94
|
+
[ "--release_nots" , "-n" , GetoptLong::REQUIRED_ARGUMENT ] ,
|
95
|
+
[ "--release_changes" , "-a" , GetoptLong::REQUIRED_ARGUMENT ]
|
96
|
+
).enum_for.inject({}){|h,kv| h.update Hash[*kv]}
|
97
|
+
|
98
|
+
package_id, release_name, userfile, ignored = ARGV
|
99
|
+
abort "#{ $0 } package_name" unless
|
100
|
+
package_id and release_name and userfile
|
101
|
+
|
102
|
+
package_id = config["package_ids"][package_id] unless
|
103
|
+
package_id =~ %r/^\d+$/
|
104
|
+
|
105
|
+
group_id = opts["group_id"] || config["group_id"]
|
106
|
+
release_date = opts["release_date"] || Time::now.strftime('%Y-%m-%d %H:%M')
|
107
|
+
|
108
|
+
type_id = opts['type_id'] || userfile[%r|\.[^\./]+$|]
|
109
|
+
type_id = {
|
110
|
+
".deb" => 1000,
|
111
|
+
".rpm" => 2000,
|
112
|
+
".zip" => 3000,
|
113
|
+
".bz2" => 3100,
|
114
|
+
".gz" => 3110,
|
115
|
+
".src.zip" => 5000,
|
116
|
+
".src.bz2" => 5010,
|
117
|
+
".src.gz" => 5020,
|
118
|
+
".src.rpm" => 5100,
|
119
|
+
".src" => 5900,
|
120
|
+
".jpg" => 8000,
|
121
|
+
".txt" => 8100,
|
122
|
+
".text" => 8100,
|
123
|
+
".htm" => 8200,
|
124
|
+
".html" => 8200,
|
125
|
+
".pdf" => 8300,
|
126
|
+
".oth" => 9999,
|
127
|
+
".ebuild" => 1300,
|
128
|
+
".exe" => 1100,
|
129
|
+
".dmg" => 1200,
|
130
|
+
".tar.gz" => 5000,
|
131
|
+
".tgz" => 5000,
|
132
|
+
".gem" => 1400,
|
133
|
+
".pgp" => 8150,
|
134
|
+
".sig" => 8150,
|
135
|
+
}[type_id]
|
136
|
+
|
137
|
+
processor_id = opts['processor_id'] || 'Any'
|
138
|
+
processor_id = {
|
139
|
+
"i386" => 1000,
|
140
|
+
"IA64" => 6000,
|
141
|
+
"Alpha" => 7000,
|
142
|
+
"Any" => 8000,
|
143
|
+
"PPC" => 2000,
|
144
|
+
"MIPS" => 3000,
|
145
|
+
"Sparc" => 4000,
|
146
|
+
"UltraSparc" => 5000,
|
147
|
+
"Other" => 9999,
|
148
|
+
}[processor_id]
|
149
|
+
|
150
|
+
release_notes = opts['release_notes'] || nil
|
151
|
+
release_notes = open(release_notes) if release_notes
|
152
|
+
|
153
|
+
release_changes = opts['release_changes'] || nil
|
154
|
+
release_changes = open(release_changes) if release_changes
|
155
|
+
|
156
|
+
userfile = open(userfile)
|
157
|
+
|
158
|
+
preformatted = '1'
|
159
|
+
|
160
|
+
form = {
|
161
|
+
"group_id" => group_id,
|
162
|
+
"package_id" => package_id,
|
163
|
+
"release_name" => release_name,
|
164
|
+
"release_date" => release_date,
|
165
|
+
"type_id" => type_id,
|
166
|
+
"processor_id" => processor_id,
|
167
|
+
"preformatted" => preformatted,
|
168
|
+
"userfile" => userfile,
|
169
|
+
"submit" => "Release File"
|
170
|
+
}
|
171
|
+
|
172
|
+
boundary = Array::new(8){ "%2.2d" % rand(42) }.join('__')
|
173
|
+
extheader['content-type'] = "multipart/form-data; boundary=___#{ boundary }___"
|
174
|
+
|
175
|
+
else
|
176
|
+
abort "#{ $0 } login create_package add_release"
|
177
|
+
|
178
|
+
end
|
179
|
+
#
|
180
|
+
# http transaction
|
181
|
+
#
|
182
|
+
client = HTTPAccess2::Client::new ENV['HTTP_PROXY']
|
183
|
+
client.debug_dev = STDERR if ENV['DEBUG']
|
184
|
+
|
185
|
+
client.set_cookie_store cookie_jar
|
186
|
+
|
187
|
+
# fixes http-access2 bug
|
188
|
+
client.redirect_uri_callback = lambda do |res|
|
189
|
+
page = res.header['location'].first
|
190
|
+
page = page =~ %r/http/ ? page : "#{ config['uri'] }/#{ page }"
|
191
|
+
page
|
192
|
+
end
|
193
|
+
|
194
|
+
response = client.send "#{ method }", "#{ config['uri'] }/#{ page }", form, extheader
|
195
|
+
|
196
|
+
client.save_cookie_store
|
197
|
+
|
198
|
+
# fixes http-access2 bug
|
199
|
+
BEGIN {
|
200
|
+
require "http-access2"
|
201
|
+
module WebAgent::CookieUtils
|
202
|
+
def domain_match(host, domain)
|
203
|
+
case domain
|
204
|
+
when /\d+\.\d+\.\d+\.\d+/
|
205
|
+
return (host == domain)
|
206
|
+
when '.'
|
207
|
+
return true
|
208
|
+
when /^\./
|
209
|
+
#return tail_match?(domain, host)
|
210
|
+
return tail_match?(host, domain)
|
211
|
+
else
|
212
|
+
return (host == domain)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
}
|
data/scripts/txt2html
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'redcloth'
|
4
|
+
require 'syntax/convertors/html'
|
5
|
+
require 'erb'
|
6
|
+
require '../lib/dr_nic_magic_models/version.rb'
|
7
|
+
|
8
|
+
version = DrNicMagicModels::VERSION::STRING
|
9
|
+
download = 'http://rubyforge.org/projects/magicmodels'
|
10
|
+
|
11
|
+
class Fixnum
|
12
|
+
def ordinal
|
13
|
+
# teens
|
14
|
+
return 'th' if (10..19).include?(self % 100)
|
15
|
+
# others
|
16
|
+
case self % 10
|
17
|
+
when 1: return 'st'
|
18
|
+
when 2: return 'nd'
|
19
|
+
when 3: return 'rd'
|
20
|
+
else return 'th'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Time
|
26
|
+
def pretty
|
27
|
+
return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_syntax(syntax, source)
|
32
|
+
return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
|
33
|
+
end
|
34
|
+
|
35
|
+
if ARGV.length >= 1
|
36
|
+
src, template = ARGV
|
37
|
+
template ||= 'template.rhtml'
|
38
|
+
else
|
39
|
+
puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
|
40
|
+
exit!
|
41
|
+
end
|
42
|
+
|
43
|
+
template = ERB.new(File.open(template).read)
|
44
|
+
|
45
|
+
title = nil
|
46
|
+
body = nil
|
47
|
+
File.open(src) do |fsrc|
|
48
|
+
title_text = fsrc.readline
|
49
|
+
body_text = fsrc.read
|
50
|
+
syntax_items = []
|
51
|
+
body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
|
52
|
+
ident = syntax_items.length
|
53
|
+
element, syntax, source = $1, $2, $3
|
54
|
+
syntax_items << "<#{element} class=\"syntax\">#{convert_syntax(syntax, source)}</#{element}>"
|
55
|
+
"syntax-temp-#{ident}"
|
56
|
+
}
|
57
|
+
title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
|
58
|
+
body = RedCloth.new(body_text).to_html
|
59
|
+
body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
|
60
|
+
end
|
61
|
+
stat = File.stat(src)
|
62
|
+
created = stat.ctime
|
63
|
+
modified = stat.mtime
|
64
|
+
|
65
|
+
$stdout << template.result(binding)
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>Dr Nic’s Magic Models</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/magicmodels"; return false'>
|
35
35
|
Get Version
|
36
|
-
<a href="http://rubyforge.org/projects/magicmodels" class="numbers">0.7.
|
36
|
+
<a href="http://rubyforge.org/projects/magicmodels" class="numbers">0.7.1</a>
|
37
37
|
</div>
|
38
38
|
<blockquote>
|
39
39
|
<strong>conjure</strong> 1. To create events that appear to be magical<br />
|
@@ -269,10 +269,95 @@ demonstrated above) to have the brilliantly simple support that Rails/ActiveReco
|
|
269
269
|
<span class="punct">>></span> <span class="ident">person</span><span class="punct">.</span><span class="ident">groups</span>
|
270
270
|
<span class="punct">=></span> <span class="punct">[<</span><span class="constant">Group</span><span class="punct">:</span><span class="number">0x39047e0</span> <span class="attribute">@attributes</span><span class="punct">={"</span><span class="string">name</span><span class="punct">"=>"</span><span class="string">Magic Models Forum</span><span class="punct">",</span> <span class="punct">"</span><span class="string">id</span><span class="punct">"=>"</span><span class="string">1</span><span class="punct">",</span> <span class="punct">"</span><span class="string">description</span><span class="punct">"=></span><span class="constant">nil</span><span class="punct">}>]</span>
|
271
271
|
<span class="punct">>></span> <span class="ident">group</span><span class="punct">.</span><span class="ident">people</span>
|
272
|
-
<span class="punct">=></span>
|
272
|
+
<span class="punct">=></span> <span class="punct">[<</span><span class="constant">Person</span><span class="punct">:</span><span class="number">0x3c33580</span> <span class="attribute">@attributes</span><span class="punct">={"</span><span class="string">lastname</span><span class="punct">"=>"</span><span class="string">Williams</span><span class="punct">",</span> <span class="punct">"</span><span class="string">firstname</span><span class="punct">"=>"</span><span class="string">Nic</span><span class="punct">",</span>
|
273
|
+
<span class="punct">"</span><span class="string">id</span><span class="punct">"=>"</span><span class="string">1</span><span class="punct">",</span> <span class="punct">"</span><span class="string">email</span><span class="punct">"=>"</span><span class="string">drnicwilliams@gmail.com</span><span class="punct">"}>]</span>
|
273
274
|
</pre></p>
|
275
|
+
|
276
|
+
|
277
|
+
<h2>Drum roll…</h2>
|
278
|
+
|
279
|
+
|
280
|
+
<p>Ladies and gentlemen. For my final feat of magical mastery, I’ll ask you to do
|
281
|
+
something you’ve never done before. This illusion is akin to the <a href="http://www.toytent.com/Posters/985.html">floating lady</a>
|
282
|
+
illusion that has been passed down through generations of magicians.</p>
|
283
|
+
|
284
|
+
|
285
|
+
<p>Exit your console session.</p>
|
286
|
+
|
287
|
+
|
288
|
+
<span class="caps">DELETE</span> your three model classes: <code>person.rb, group.rb, and membership.rb</code> from the
|
289
|
+
<code>app/models</code> folder. (You can always get them back via the model generator… be fearless!)
|
290
|
+
|
291
|
+
<p>Re-launch your console.</p>
|
292
|
+
|
293
|
+
|
294
|
+
<p><strong>drums are still rolling…</strong></p>
|
295
|
+
|
296
|
+
|
297
|
+
<p>Be prepared to applaud loudly…</p>
|
298
|
+
|
299
|
+
|
300
|
+
<p><pre class="syntax">
|
301
|
+
<span class="punct">>></span> <span class="constant">Person</span>
|
302
|
+
<span class="punct">=></span> <span class="constant">Person</span>
|
303
|
+
</pre></p>
|
304
|
+
|
305
|
+
|
306
|
+
<p>You applaud loudly, but watch for more…</p>
|
307
|
+
|
308
|
+
|
309
|
+
<p><pre class="syntax">
|
310
|
+
<span class="punct">>></span> <span class="ident">person</span> <span class="punct">=</span> <span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="number">1</span><span class="punct">)</span>
|
311
|
+
<span class="punct">=></span> <span class="punct"><</span><span class="constant">Person</span><span class="punct">:</span><span class="number">0x3958930</span> <span class="attribute">@attributes</span><span class="punct">={"</span><span class="string">lastname</span><span class="punct">"=>"</span><span class="string">Williams</span><span class="punct">",</span> <span class="punct">"</span><span class="string">firstname</span><span class="punct">"=>"</span><span class="string">Nic</span><span class="punct">",</span>
|
312
|
+
<span class="punct">"</span><span class="string">id</span><span class="punct">"=>"</span><span class="string">1</span><span class="punct">",</span> <span class="punct">"</span><span class="string">email</span><span class="punct">"=>"</span><span class="string">drnicwilliams@gmail.com</span><span class="punct">"}></span>
|
313
|
+
<span class="punct">>></span> <span class="ident">person</span><span class="punct">.</span><span class="ident">memberships</span>
|
314
|
+
<span class="punct">=></span> <span class="punct">[<</span><span class="constant">Membership</span><span class="punct">:</span><span class="number">0x393a000</span> <span class="attribute">@attributes</span><span class="punct">={"</span><span class="string">group_id</span><span class="punct">"=>"</span><span class="string">1</span><span class="punct">",</span> <span class="punct">"</span><span class="string">id</span><span class="punct">"=>"</span><span class="string">1</span><span class="punct">",</span> <span class="punct">"</span><span class="string">person_id</span><span class="punct">"=>"</span><span class="string">1</span><span class="punct">"}>]</span>
|
315
|
+
<span class="punct">>></span> <span class="ident">person</span><span class="punct">.</span><span class="ident">groups</span>
|
316
|
+
<span class="punct">=></span> <span class="punct">[<</span><span class="constant">Group</span><span class="punct">:</span><span class="number">0x390df60</span> <span class="attribute">@attributes</span><span class="punct">={"</span><span class="string">name</span><span class="punct">"=>"</span><span class="string">Magic Models Forum</span><span class="punct">",</span> <span class="punct">"</span><span class="string">id</span><span class="punct">"=>"</span><span class="string">1</span><span class="punct">",</span> <span class="punct">"</span><span class="string">description</span><span class="punct">"=></span><span class="constant">nil</span><span class="punct">}>]</span>
|
317
|
+
</pre></p>
|
318
|
+
|
319
|
+
|
320
|
+
<h2>Tada!</h2>
|
321
|
+
|
322
|
+
|
323
|
+
<p>The end.</p>
|
324
|
+
|
325
|
+
|
326
|
+
<h2>Dr Nic’s Blog</h2>
|
327
|
+
|
328
|
+
|
329
|
+
<p><a href="http://www.drnicwilliams.com">http://www.drnicwilliams.com</a> – for future announcements and
|
330
|
+
other stories and things.</p>
|
331
|
+
|
332
|
+
|
333
|
+
<h2>Articles about Magic Models</h2>
|
334
|
+
|
335
|
+
|
336
|
+
<ul>
|
337
|
+
<li><a href="http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/">Announcement</a></li>
|
338
|
+
<li><a href="http://drnicwilliams.com/2006/08/10/bts-magic-models-class-creation/"><span class="caps">BTS</span> – Class creation</a></li>
|
339
|
+
</ul>
|
340
|
+
|
341
|
+
|
342
|
+
<h2>Forum</h2>
|
343
|
+
|
344
|
+
|
345
|
+
<p><a href="http://groups.google.com/group/magicmodels">http://groups.google.com/group/magicmodels</a></p>
|
346
|
+
|
347
|
+
|
348
|
+
<h2>Licence</h2>
|
349
|
+
|
350
|
+
|
351
|
+
<p>This code is free to use under the terms of the <span class="caps">MIT</span> licence.</p>
|
352
|
+
|
353
|
+
|
354
|
+
<h2>Contact</h2>
|
355
|
+
|
356
|
+
|
357
|
+
<p>Comments are welcome. Send an email to <a href="mailto:drnicwilliams@gmail.com">Dr Nic Williams</a>
|
358
|
+
or via his blog at <a href="http://www.drnicwilliams.com">http://www.drnicwilliams.com</a></p>
|
274
359
|
<p class="coda">
|
275
|
-
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>,
|
360
|
+
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 10th August 2006<br>
|
276
361
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
277
362
|
</p>
|
278
363
|
</div>
|
data/website/index.txt
CHANGED
@@ -200,5 +200,69 @@ The final association trick is a ripper. Automatic generation of <code>has_many
|
|
200
200
|
>> person.groups
|
201
201
|
=> [<Group:0x39047e0 @attributes={"name"=>"Magic Models Forum", "id"=>"1", "description"=>nil}>]
|
202
202
|
>> group.people
|
203
|
-
=>
|
203
|
+
=> [<Person:0x3c33580 @attributes={"lastname"=>"Williams", "firstname"=>"Nic",
|
204
|
+
"id"=>"1", "email"=>"drnicwilliams@gmail.com"}>]
|
204
205
|
</pre>
|
206
|
+
|
207
|
+
h2. Drum roll...
|
208
|
+
|
209
|
+
Ladies and gentlemen. For my final feat of magical mastery, I'll ask you to do
|
210
|
+
something you've never done before. This illusion is akin to the "floating lady":http://www.toytent.com/Posters/985.html
|
211
|
+
illusion that has been passed down through generations of magicians.
|
212
|
+
|
213
|
+
Exit your console session.
|
214
|
+
|
215
|
+
DELETE your three model classes: <code>person.rb, group.rb, and membership.rb</code> from the
|
216
|
+
<code>app/models</code> folder. (You can always get them back via the model generator... be fearless!)
|
217
|
+
|
218
|
+
Re-launch your console.
|
219
|
+
|
220
|
+
*drums are still rolling...*
|
221
|
+
|
222
|
+
Be prepared to applaud loudly...
|
223
|
+
|
224
|
+
<pre syntax="ruby">
|
225
|
+
>> Person
|
226
|
+
=> Person
|
227
|
+
</pre>
|
228
|
+
|
229
|
+
You applaud loudly, but watch for more...
|
230
|
+
|
231
|
+
<pre syntax="ruby">
|
232
|
+
>> person = Person.find(1)
|
233
|
+
=> <Person:0x3958930 @attributes={"lastname"=>"Williams", "firstname"=>"Nic",
|
234
|
+
"id"=>"1", "email"=>"drnicwilliams@gmail.com"}>
|
235
|
+
>> person.memberships
|
236
|
+
=> [<Membership:0x393a000 @attributes={"group_id"=>"1", "id"=>"1", "person_id"=>"1"}>]
|
237
|
+
>> person.groups
|
238
|
+
=> [<Group:0x390df60 @attributes={"name"=>"Magic Models Forum", "id"=>"1", "description"=>nil}>]
|
239
|
+
</pre>
|
240
|
+
|
241
|
+
h2. Tada!
|
242
|
+
|
243
|
+
The end.
|
244
|
+
|
245
|
+
h2. Dr Nic's Blog
|
246
|
+
|
247
|
+
"http://www.drnicwilliams.com":http://www.drnicwilliams.com - for future announcements and
|
248
|
+
other stories and things.
|
249
|
+
|
250
|
+
h2. Articles about Magic Models
|
251
|
+
|
252
|
+
* "Announcement":http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/
|
253
|
+
* "BTS - Class creation":http://drnicwilliams.com/2006/08/10/bts-magic-models-class-creation/
|
254
|
+
|
255
|
+
|
256
|
+
h2. Forum
|
257
|
+
|
258
|
+
"http://groups.google.com/group/magicmodels":http://groups.google.com/group/magicmodels
|
259
|
+
|
260
|
+
h2. Licence
|
261
|
+
|
262
|
+
This code is free to use under the terms of the MIT licence.
|
263
|
+
|
264
|
+
h2. Contact
|
265
|
+
|
266
|
+
Comments are welcome. Send an email to "Dr Nic Williams":mailto:drnicwilliams@gmail.com
|
267
|
+
or via his blog at "http://www.drnicwilliams.com":http://www.drnicwilliams.com
|
268
|
+
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: dr_nic_magic_models
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.7.
|
7
|
-
date: 2006-08-
|
6
|
+
version: 0.7.1
|
7
|
+
date: 2006-08-12 00:00:00 +02:00
|
8
8
|
summary: Invisible validations, assocations and Active Record models themselves!
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -32,21 +32,20 @@ files:
|
|
32
32
|
- install.rb
|
33
33
|
- README
|
34
34
|
- CHANGELOG
|
35
|
-
- lib/module.rb
|
36
35
|
- lib/dr_nic_magic_models
|
37
36
|
- lib/dr_nic_magic_models.rb
|
38
|
-
- lib/new_base.rb
|
39
37
|
- lib/base.rb
|
38
|
+
- lib/module.rb
|
40
39
|
- lib/dr_nic_magic_models/associations.rb
|
41
40
|
- lib/dr_nic_magic_models/schema.rb
|
42
41
|
- lib/dr_nic_magic_models/validations.rb
|
43
42
|
- lib/dr_nic_magic_models/version.rb
|
44
43
|
- test/connections
|
45
44
|
- test/fixtures
|
46
|
-
- test/abstract_unit.rb
|
47
45
|
- test/dummy_test.rb
|
48
|
-
- test/invisible_model_classes_test.rb
|
49
46
|
- test/env_test.rb
|
47
|
+
- test/invisible_model_classes_test.rb
|
48
|
+
- test/abstract_unit.rb
|
50
49
|
- test/invisible_model_access_test.rb
|
51
50
|
- test/invisible_model_assoc_test.rb
|
52
51
|
- test/connections/native_mysql
|
@@ -58,13 +57,16 @@ files:
|
|
58
57
|
- test/fixtures/fun_users.yml
|
59
58
|
- test/fixtures/db_definitions/mysql.sql
|
60
59
|
- test/fixtures/db_definitions/mysql.drop.sql
|
61
|
-
- website/index.txt
|
62
60
|
- website/index.html
|
63
61
|
- website/javascripts
|
64
62
|
- website/stylesheets
|
65
63
|
- website/template.rhtml
|
64
|
+
- website/index.txt
|
66
65
|
- website/javascripts/rounded_corners_lite.inc.js
|
67
66
|
- website/stylesheets/screen.css
|
67
|
+
- scripts/rubyforge-orig
|
68
|
+
- scripts/txt2html
|
69
|
+
- scripts/http-access2-2.0.6.gem
|
68
70
|
test_files: []
|
69
71
|
|
70
72
|
rdoc_options:
|
@@ -84,7 +86,7 @@ dependencies:
|
|
84
86
|
version_requirement:
|
85
87
|
version_requirements: !ruby/object:Gem::Version::Requirement
|
86
88
|
requirements:
|
87
|
-
- - "
|
89
|
+
- - ">="
|
88
90
|
- !ruby/object:Gem::Version
|
89
91
|
version: 1.14.3
|
90
92
|
version:
|
data/lib/new_base.rb
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
# Only needs to be an Include module for the generated classes, not for all ARs
|
2
|
-
module ActiveRecord
|
3
|
-
class Base
|
4
|
-
class << self
|
5
|
-
alias old_new new
|
6
|
-
def new(*args)
|
7
|
-
obj = old_new(*args)
|
8
|
-
unless self.methods.include? 'generate_validations'
|
9
|
-
self.send(:include, DrNicMagicModels::Validations)
|
10
|
-
self.generate_validations
|
11
|
-
end
|
12
|
-
obj
|
13
|
-
end
|
14
|
-
alias old_allocate allocate
|
15
|
-
def allocate
|
16
|
-
obj = old_allocate
|
17
|
-
unless self.methods.include? 'generate_validations'
|
18
|
-
self.send(:include, DrNicMagicModels::Validations)
|
19
|
-
self.generate_validations
|
20
|
-
end
|
21
|
-
obj
|
22
|
-
end
|
23
|
-
|
24
|
-
cattr_accessor :known_unknowns
|
25
|
-
|
26
|
-
# Returns the AssociationReflection object for the named +aggregation+ (use the symbol). Example:
|
27
|
-
# Account.reflect_on_association(:owner) # returns the owner AssociationReflection
|
28
|
-
# Invoice.reflect_on_association(:line_items).macro # returns :has_many
|
29
|
-
def reflect_on_association(association)
|
30
|
-
puts "Known association? #{association.to_sym.inspect} -> #{reflections[association]}"
|
31
|
-
reflections[association].is_a?(AssociationReflection) ? reflections[association] : nil
|
32
|
-
end
|
33
|
-
|
34
|
-
def assess_unknown_method(instance, method, *args, &block)
|
35
|
-
result = find_belongs_to instance, method, *args, &block
|
36
|
-
result = find_has_some instance, method, *args, &block if not result
|
37
|
-
result = find_has_some_indirect instance, method, *args, &block if not result
|
38
|
-
result
|
39
|
-
end
|
40
|
-
|
41
|
-
def add_known_unknown(method)
|
42
|
-
known_unknowns ||= {}
|
43
|
-
known_unknowns[method] = true
|
44
|
-
end
|
45
|
-
|
46
|
-
def unknown_method?(method)
|
47
|
-
known_unknowns.nil? or known_unknowns.include? method
|
48
|
-
end
|
49
|
-
|
50
|
-
def find_belongs_to(instance, method, *args, &block)
|
51
|
-
foreign_key = self.columns.select {|column| column.name == method.to_s.foreign_key}.first
|
52
|
-
return add_belongs_to(instance, method, *args, &block) if foreign_key
|
53
|
-
end
|
54
|
-
|
55
|
-
def add_belongs_to(instance, method, *args, &block)
|
56
|
-
self.send 'belongs_to', method
|
57
|
-
instance.send(method, *args, &block)
|
58
|
-
end
|
59
|
-
|
60
|
-
def find_has_some(instance, method, *args, &block)
|
61
|
-
klass = Module.const_get method.to_s.downcase.singularize.camelize rescue nil
|
62
|
-
foreign_key = klass.columns.select {|column| column.name == self.name.foreign_key}.first if klass
|
63
|
-
return add_has_some(instance, method, *args, &block) if foreign_key
|
64
|
-
end
|
65
|
-
|
66
|
-
def add_has_some(instance, method, *args, &block)
|
67
|
-
_method = method.to_s
|
68
|
-
association = _method.singularize == _method ? 'has_one' : 'has_many'
|
69
|
-
self.send association, method
|
70
|
-
instance.send(instance, method, *args, &block)
|
71
|
-
end
|
72
|
-
|
73
|
-
def find_has_some_indirect(instance, method, *args, &block)
|
74
|
-
klass = Module.const_get method.to_s.downcase.singularize.camelize rescue return
|
75
|
-
join_table = nil
|
76
|
-
self.connection.tables.each do |table|
|
77
|
-
unless [self.table_name, klass.table_name].include? table
|
78
|
-
columns = self.connection.columns(table).map(&:name)
|
79
|
-
join_table = table if columns.include?(self.to_s.foreign_key) and columns.include?(klass.to_s.foreign_key)
|
80
|
-
end
|
81
|
-
break if join_table
|
82
|
-
end
|
83
|
-
return add_has_some_through(instance, join_table, method, *args, &block) if join_table
|
84
|
-
end
|
85
|
-
|
86
|
-
def add_has_some_through(instance, join_table, method, *args, &block)
|
87
|
-
puts "self.class.send 'has_many', #{method}, :through => #{join_table.inspect}"
|
88
|
-
self.send 'has_many', method, :through => join_table.to_sym
|
89
|
-
instance.send(method, *args, &block)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
class_eval do
|
94
|
-
alias :normal_method_missing :method_missing
|
95
|
-
def method_missing(method, *args, &block)
|
96
|
-
if unknown_method? method
|
97
|
-
self.class.assess_unknown_method(self, method, *args, &block)
|
98
|
-
end
|
99
|
-
add_known_unknown method
|
100
|
-
normal_method_missing(method, *args, &block)
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|