github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

robmckinnon / morph

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 10
    • 3
  • Source
  • Commits
  • Network (3)
  • Issues (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Morph makes it easy to emerge Ruby class definitions at runtime. Mix with Hpricot for screen scrapping fun. — Read more

  cancel

http://github.com/robmckinnon/morph

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

updated README with from_tsv() and from_xml() examples 
robmckinnon (author)
Tue Feb 09 04:52:48 -0800 2010
commit  43387d58270493a2e36ea911dd6e044a05bd343c
tree    9598806964c006461857d9d567a3f245a68ac91b
parent  164143c5c6b9acc2818901e504c220fd32ec4b59
morph /
name age
history
message
file .gitignore Mon Mar 24 08:52:15 -0700 2008 ignore config [robmckinnon]
file CHANGELOG Tue Feb 09 04:05:49 -0800 2010 added from_tsv() method [robmckinnon]
file LICENSE Sun Mar 23 17:15:24 -0700 2008 complying with echoe conventions [robmckinnon]
file Manifest Sat Mar 14 04:53:07 -0700 2009 from_hash() creates classes in Morph namespace ... [robmckinnon]
file README Tue Feb 09 04:52:48 -0800 2010 updated README with from_tsv() and from_xml() e... [robmckinnon]
file README.rdoc Tue Feb 09 04:52:48 -0800 2010 updated README with from_tsv() and from_xml() e... [robmckinnon]
file Rakefile Mon Feb 08 07:18:11 -0800 2010 added from_xml(); updated for active_support; f... [robmckinnon]
directory examples/ Wed Jun 11 05:49:27 -0700 2008 put popularity and authority around the right way [robmckinnon]
file init.rb Mon Mar 16 15:56:12 -0700 2009 can pass namespace to from_hash method [robmckinnon]
directory lib/ Tue Feb 09 04:37:08 -0800 2010 from_xml() now returns array if root element co... [robmckinnon]
directory spec/ Tue Feb 09 04:52:48 -0800 2010 updated README with from_tsv() and from_xml() e... [robmckinnon]
README.rdoc

Morph allows you to emerge class definitions via calling assignment methods; mix with Hpricot for screen scrapping fun.

Morph creating classes +from_xml()+

Here’s example code showing Morph playing with XML:

 require 'rubygems'; require 'morph'

 xml = %Q[<?xml version="1.0" encoding="UTF-8"?>
 <councils type="array">
   <council code='1'>
     <name>Aberdeen City Council</name>
   </council>
   <council code='2'>
     <name>Allerdale Borough Council</name>
   </council>
 </councils>]

 councils = Morph.from_xml(xml)
 # => [#<Morph::Council @code="1", @name="Aberdeen City Council">,
       #<Morph::Council @code="2", @name="Allerdale Borough Council">]

 councils.first.name
 # => "Aberdeen City Council"

Morph creating classes +from_tsv()+

Here’s example code showing Morph playing with TSV (tab-separated values):

 require 'rubygems'; require 'morph'

 tsv = %Q[name\tparty\nTed Roe\tred\nAli Davidson\tblue\nSue Smith\tgreen]

 people = Morph.from_tsv(tsv, 'person')
 # => [#<Morph::Person @name="Ted Roe", @party="red">,
       #<Morph::Person @name="Ali Davidson", @party="blue">,
       #<Morph::Person @name="Sue Smith", @party="green">]

 people.last.party
 # => "green"

Morph playing with Hpricot

Here’s example code showing Morph playing with Hpricot:

 require 'rubygems'; require 'hpricot'; require 'open-uri'
 require 'morph'

 class Hubbit
   include Morph  # allows class to morph

   def initialize 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)  # morph magic happening here!
     end
   end
 end

 def Hubbit name; Hubbit.new name; end

The model emerges from the data. Let’s start by looking up ‘why’:

 why = Hubbit 'why'

What new methods do we have?

 Hubbit.morph_methods # => ["email", "email=", "name", "name="]

Ah-ha, so we have a name attribute now:

 why.name # => "why the lucky stiff"

Let’s add some of why’s projects:

 why.projects = %w[shoes hacketyhack camping hoodwinkd hpricot
                  markaby mousehole parkplace poignant sandbox]

That why’s a productive fellow! Note new accessor methods have been added:

 Hubbit.morph_methods
 # => ["email", "email=", "name", "name=", "projects", "projects="]

Let’s do some more morphing:

 dhh = Hubbit 'dhh'

Do we have more methods now?

 Hubbit.morph_methods
 # => ["blog", "blog=", "company", "company=", "email", "email=",
 #    "location", "location=" "name", "name=", "projects", "projects="]

So, a new company method has appeared:

 dhh.company #=> "37signals"

Morph making sample Active Record line via script_generate

Time to generate an Active Record model? Get a sample script line like this:

 Hubbit.script_generate
 # => "ruby script/destroy rspec_model Hubbit;
 #    ruby script/generate rspec_model Hubbit blog:string company:string
 #        email:string location:string name:string projects:string"

or specify the generator:

 Hubbit.script_generate :generator => 'model'
 # => "ruby script/destroy model Hubbit;
 #     ruby script/generate model Hubbit blog:string company:string
 #         email:string location:string name:string projects:string"

You’ll have to edit this as it currently sets all data types to be string, and doesn’t understand associations.

Morph setting hash of attributes via morph

 class Order; include Morph; end
 order = Order.new

How about adding a hash of attribute values?

 order.morph :drink => 'tea', :spoons_of_sugar => 2, :milk => 'prefer soya thanks'

Looks like we got ‘em:

 order.drink  # => "tea"
 order.spoons_of_sugar  # => 2
 order.milk  # => "prefer soya thanks"

Morph obtaining hash of attributes via morph_attributes

Create an item:

 class Item; include Morph; end
 item = Item.new
 item.morph :name => 'spinach', :cost => 0.50

Now an order:

 class Order; include Morph; end
 order = Order.new
 order.no = 123
 order.items = [item]

Want to retrieve all that as a nested hash of values? No problem:

 order.morph_attributes
 # => {:items=>[{:name=>"spinach", :cost=>0.5}], :no=>123}

Last bits

See examples/ directory for some example code. See LICENSE for the terms of this software.

 .                                                     ,
 .                                                 ?7+~::+II~
 .                                                ?7:     ,:+7
 .                             777IIII777?        7:         :?7
 .                          =I=           I:      7?          ,+7
 .                         I?         ,,   77      7:           :I
 .                        =  ?7777   77  7   7      7+,          :7
 .                       7   777777 ~77+=77  I+      I?          ,7
 .                      :7  77  ~77  I   I7   7       ?:          ?
 .                      I   77   7,  7    7   :I       I          ?
 .                      7   ?77=7~    77777    7      ~+          ,+
 .                      7~                     7  :I7?~            7
 .                      =?                     7 ?I    ~I77=       I=
 .                       7    ?          :,   7  I7777,     7       7
 .                        ?    777?~~7777+    7              7~      7
 .                        ?7    ,777777=,   ,7                7      ,7
 .                          7=      ,      =7                 7:      7
 .                            +7         :7                    7      ,I
 .                             :7        ?~                   7?       7
 .                              7         7              ~II7~,        7
 .                              7         7  ,  =7777777?+,,,         I=
 .                            :7,          ~==,                       7
 .                       II~,,                                     77~
 .                    ,I?                                      +777
 .                   7+,                                 ~7777:
 .                 ==                               :77
 .               :7:                              ,7I
 .             7I                                 7
 .            I          ,7,                      7
 .          =7          77=7                      7
 .        ,7          7I   7                      7
 .        I,        I7     7                      7
 .       ?,       ,7       7,                     7
 .       7       7~        7,                     7
 .       7      ,7I        7                      7
 .       =+       =7       7                      ~=
 .        =7        7,     7                       7
 .         ,7,       ~7IIII7+,                     7
 .           +:              II                    I
 .            ?7              I?                   +~
 .              II,           +I                    7
 .                ~7          ,I                    7
 .                  7=        ~7                    7
 .                   ?7,     ~7+                    ?~
 .                     ~7777I=                      ,7
 .                         7:                        7
 .                         I                         7
 .                         I          ,:77I          7
 .                         I          :7             I
 .                         I                         =~
 .                         7               ,         ,7
 .                         +,         7    :         ,7
 .                          +         7    +          7
 .                          +         7    +         ,7
 .                          7         I    ?         ,7
 .                          7         +:   7         ,7
 .                          7         =+   7         ,7
 .                          7         :I   I         ,7
 .                          7         :I   7          7
 .                          7         :I   I          7
 .                          I,        ,7   I:         7
 .                          =+        ,7    ?         7
 .                          :?,       ,7    7,        7
 .                          I:        ,7    7,        ?
 .                         :7         ,7    7,        ,
 .                        +I,         :     ?         ,=
 .                       +=           ~     =~         7
 .                    :II,,           =      I         ?
 .                =I=                 ?      7,        :7
 .              II~                   I      7,         ,II
 .            7~                      ~7     7            ,=7
 .            =                       =7     I,             ::
 .            77II?==?II777777777777777      7~              7
 .                                            77+,,          7:
 .                                               777777+:,~777
 .
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server