From LAMP to DARC

October 19, 2005 § Leave a comment

You’ve heard of LAMP?

Meet
its 21st-century cousin: DARC

? Darwin
? Apache
? Rails
?
CoreData

This is especially cool when
combined with microformats. Click [Read more] for a very rough
demo script. You can also download the DARCdemo source code.

On Mac OS X
10.4:

A.
Setup

1. Install Xcode Developer Tools
2. Install Ruby on Rails.
3. If not yet installed, add SQLite3 database adaptor (e.g., via GEMS)
4. For hCard demo, install Firefox + greasemonkey + microformat-find

B.
Build Model

1. In Xcode, create a new Core Data Document-based Cocoa project,
“DARCedit”
2. Construct a model containing two entities:
users
and
events
3. For
users,
add attributes from hCard (e.g., “fn”,
“street_address”)
4. For
events,
add attributes from hCalendar (e.g., “summary”,
“dtstart”)
5. Optionally, create a many-to-many relationship
between them
6. Open MyDocument.nib, and add a Tab view with
“Users” and “Events” panes
7. Select, then option-drag each entity from the
model view into its respective tab pane
8. Build. run, and
test

C. Create
Project

1. From an appropriate
directory, type:

a. $ rails DARCdemo
b. $ cd
DARCdemo

2. From
DARCedit.app:

a. Populate a few database entries in each
table
b. Save as “DARCdemo/db/darc.sql” (an SQLite3
database)

3. Edit config/database.yml
(e.g., using TextMate)

? development:?ÊÊadapter:
sqlite3?ÊÊdbfile: db/darc.sql
? Trim other fields
? Note: “dbfile” not
“database”

4. Here’s where you’d edit
Apache to setup a fastcgi connection, but we’ll ignore that for
now.

D. Create
Objects

1. Create model objects
(note initial Capital)

a. $ ./script/generate model User
b. $ ./script/generate model Event
c.
2. Map to
CoreData’s weird naming scheme. In
app/models/{event,user}.rb,
add:
a. set_table_name “ZEVENTS” # or
ZUSERS
b. set_primary_key
“Z_PK”

3. Generate scaffold
code

a. $ ./script/generate scaffold
User
b. $ ./script/generate scaffold Event # can
overwrite stylesheet (‘Y’)
c. Note: does
not
generate Edit/New scaffolds, due to unconventional primary key (fixed in
download)

4. Run project

a. $ (./script/server&)
&&
b. > sleep 10 &&
c. > open -a FireFox
http://localhost:3000/users
d. Click “show” to see detailed list
view
e. $ killall
ruby
to stop (often not needed; just refresh
to pick up changes)
f. WARNING: “Edit” and “New” don’t work (by
default) and
will corrupt the
database

E. Add
Microformats

1. Patch
ActiveRecord to restore original column names

a. Add “require ‘active_record_patch’ ” to
config/environment.rb
b. Create file “config/active_record_patch.rb”
containing:
? class
ActiveRecord::ConnectionAdapters::Column
? def css_name
? human_name[1..-1].gsub(/
/,’-‘)
? end
?
end

2. Edit
app/views/users/
list.rhtml

?
Change
: <% for user in @users
%>\n<tr >
? ->
To
: <% for user in @users %>\n<tr
class=”vcard”>
?
Change
: <td><%=h
user.send(column.name)
%></td>
? ->
To:
<td
class=”<%= column.css_name
%>”
><%=h
user.send(column.name)
%></td>
? Repeat for
app/views/events/list.rhtml
using
“class=vevent”

3.
Edit
app/views/users/
show.rhtml

? Wrap the for loop in: <dl class=”xoxo
vcard“>
# ‘vevent’ for events
? Replace the <p> with <dt> and
a <dd> containing the class:
? <dt><%= column.human_name
%>:</dt>
? <dd class=”<%= column.human_name
%>”><%=h
@
user.send(column.name)
%></dd>
? Repeat for
app/views/events/list.rhtml
using
@event
and
“class=vevent”

4.
Display in Firefox (if GreaseMonkey installed)

a. $ open -a FireFox
http://localhost:3000/users
b. Click on the “>>>hcard”
graphic
c. Select “show vcard as text”
d. Go back then click “Show” link
e. Click on this “>>>hcard”
graphic
f. Now select “home” under “Show in Google
Maps”
g. Go back and repeat using “Add to Address
Book”
h. Do the same with: $ open -a FireFox
http://localhost:3000/events

5. Extract
XOXO datastructures

? Extract and save xoxo.py from microformats.org
? To see the internal data structures,
uncomment this line (near the end):
#print
>>sys.stderr, structs
? $ python xoxo.py
http://localhost:3000/users/show/1 > test.html
? $ open -e test.html # data was extracted
into Python then re-constituted as HTML
? This technique can be used to get
XML-like parsing out of ‘mere’ HTML

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

What’s this?

You are currently reading From LAMP to DARC at iHack, therefore iBlog.

meta

%d bloggers like this: