+ Adding New Entry Types

why the lucky stiff why at hobix.com
Fri Sep 17 00:57:09 EDT 2004


=== Your Hobix Daily Tip: Adding New Entry Types ===

Hobix comes with two types of basic content.  The Hobix::Entry and the 
Hobix::LinkList.

The Hobix::Entry class represents the standard blog post.  We uncovered 
this guy a few days ago:
<http://rubyforge.org/pipermail/hobix-is-the-way/2004-September/000012.html>

The Hobix::LinkList is a subclass of Hobix::Entry which is designed to 
hold an ordered mapping of links.  I may discuss this class more another 
time if it really interests anyone.  There's not much too it.

We're going to talk about duck typing and how it relates to Hobix 
content.  Ultimately, it boils down to this:

   If something acts like an entry, Hobix uses it like an entry.

=== The ArtPrint class ===

Let's say you do graphic design or art or something.  And you want a 
portion of your site dedicated to your art.  You basically want to 
upload a large image to your server of an art piece and store some 
metadata for each image.

Start by designing a YAML document to store the data in:

   --- !example.org,2004/artPrint
   title: Ironing Board At Dusk
   by: Herschel Mondragon
   created: 2004-02-05
   image: ironing-board-print.jpg
   note: I love how the mother is running down the stairs
     screaming and you can only see the father's legs.
     The ironing board is serene.  It's such a statement
     on my childhood and growing up with an ironing board
     in the house.

You'll save this file in your `entries' folder, perhaps in a category 
which contains all of your art pieces.

=== ArtPrint, Be an Entry ===

Now, let's add a class to get the ArtPrint to act like an Entry.  We'll 
need to mimick all of the basic Hobix::Entry's fields and methods.  You 
can refer to `ri Hobix::Entry' for a list of all public properties and 
methods.

module Example
class ArtPrint
     attr_accessor :id, :link, :title, :by, :created, :image,
                   :note, :modified, :issued, :created

     # Definition map for outputting YAML.  Used by +to_yaml+,
     # see +ToYamlExtras+ module.
     def to_yaml_property_map
         [
             ['@title', :req],
             ['@by', :req],
             ['@created', :opt],
             ['@modified', :opt],
             ['@image', :req],
             ['@note', :opt]
         ]
     end

     # Converts the image information into a RedCloth
     # string for display.
     def content
         RedCloth.new <<-RED
           !#{ image }(#{ title })!

           p(artPrintData). by #{ by }
           on #{ created.strftime( "%m/%d/%Y" ) }

           p(artPrintNote). #{ note }
         RED
     end

     # Force an author
     def author
        "why"
     end

     # YAML type for output
     def to_yaml_type
         "!example.org,2004/artPrint"
     end
end
end

YAML::add_domain_type( 'example.org,2004', 'artPrint' ) do |type, val|
     YAML::object_maker( Example::ArtPrint, val )
end

This class definition can be stored in your blog's `local' directory (if 
it doesn't exist: create it) in a file called `artprint.rb'.  Then, just 
add the line to your require list:

   requires:
   - hobix/out/rss
   - hobix/out/atom
   - local/artprint

Flexible content types are so handy.  I'm glad we had this talk.

_why



More information about the Hobix-is-the-way mailing list