From e32bb17992b2886872158c7c9345588a5d08c660 Mon Sep 17 00:00:00 2001 From: JamesFerguson Date: Wed, 13 Jul 2011 08:35:20 +1000 Subject: [PATCH] Cover element ... :attributes => {...} in the README --- .gitignore | 1 + README.md | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/.gitignore b/.gitignore index f8dffe3..e79ab55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ doc/* pkg/* tags .rvmrc +.idea diff --git a/README.md b/README.md index 51e006f..05d5fe1 100644 --- a/README.md +++ b/README.md @@ -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 + + + tag:all-the-episodes.heroku.com,2005:/tv_shows + + + TV Shows + 2011-07-10T06:52:27Z + + +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 `` contained another element that also contained a `` (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`.