public
Description: Pottery, same as Morph, plus ability to persist instances to a database; requires Morph and Soup gems.
Homepage: http://github.com/robmckinnon/pottery
Clone URL: git://github.com/robmckinnon/pottery.git
pottery / README
100644 64 lines (36 sloc) 1.33 kb
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
Pottery allows you to emerge class definitions via calling assignment methods and
persist instances to a database; requires Morph and Soup gems.
 
[Note API subject to change]
 
== Pottery example
 
Here's example code showing Pottery playing with Hpricot:
 
> sqlite3 soup.db
 
> irb
 
 require 'rubygems'
 require 'pottery'; require 'hpricot'; require 'open-uri'
 
 class Hubbit
   include Pottery
 
   def initialize name=nil
     if name
       doc = Hpricot open("http://github.com/#{name}")
 
       (doc/'label').collect do |node|
         label = node.inner_text
         value = node.next_sibling.inner_text.strip
 
         morph(label, value)
       end
       morph(:id_name, name)
     end
   end
 end
 
 
The model emerges from the data. Let's start by looking up 'why':
 
 why = Hubbit.new 'why'
 
What new methods do we have?
 
 Hubbit.morph_methods # => ["email", "email=", "followers", "followers=",
    "id_name", "id_name=", "member_since", "member_since=", "name", "name=",
    "public_repos", "public_repos="]
 
Ah-ha, so we have a name attribute now:
 
 why.name #=> "why the lucky stiff"
 
 
Let's save why for later
 
 why.save
 
 
Ok, now it's later, let's restore why from the database
 
 why = Hubbit.restore('why') #=> <Hubbit @id_name="why", @name="why the lucky stiff" ...>
 
 
 
See LICENSE for the terms of this software.