Skip to content

Commit

Permalink
Cover element ... :attributes => {...} in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesFerguson committed Jul 12, 2011
1 parent e67f18c commit e32bb17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ doc/*
pkg/*
tags
.rvmrc
.idea
34 changes: 34 additions & 0 deletions README.md
Expand Up @@ -219,6 +219,40 @@ Attributes are absolutely the same as `element` or `has_many`
Again, you can omit the tag if the attribute accessor symbol matches the name of the attribute.


### Attributes On Elements

<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
<id>tag:all-the-episodes.heroku.com,2005:/tv_shows</id>
<link rel="alternate" type="text/html" href="http://all-the-episodes.heroku.com"/>
<link rel="self" type="application/atom+xml" href="http://all-the-episodes.heroku.com/tv_shows.atom"/>
<title>TV Shows</title>
<updated>2011-07-10T06:52:27Z</updated>
</feed>

In this case you would need to map an element to a new Link class just to access its attributes, except that there is an alternate syntax. Instead of

class Feed
# ....
has_many :links, Link, :tag => 'link', :xpath => '.'
end

class Link
include HappyMapper

attribute :rel, String
attribute :type, String
attribute :href, String
end

You can drop the Link class and simply replace the has_many on Feed with

element :link, String, :single => false, :attributes => { :rel => String, :type => String, :href => String }

You can omit the :single => false for elements that only occur once under their parent.

This syntax is most appropriate for elements (with attributes but no content) that only occur at only one level of the heirarchy. If `<feed>` contained another element that also contained a `<link>` (as most atom feeds do) it would be DRY-er to use the first syntax, with a separate `Link` class.


## Class composition

Our address has a country and that country element has a code. Up until this point we neglected it as we declared a `country` as being a `String`.
Expand Down

0 comments on commit e32bb17

Please sign in to comment.