defines pluralized format methods on Collection objects

This commit is contained in:
Jessica Lynn Suttles 2013-02-18 11:57:59 -08:00
parent 3ddb91d492
commit b3ed1207c6
2 changed files with 50 additions and 35 deletions

View file

@ -52,13 +52,19 @@ module Microformats2
unless respond_to?(mn) unless respond_to?(mn)
self.class.class_eval { attr_accessor mn } self.class.class_eval { attr_accessor mn }
end end
unless respond_to?(mn.pluralize)
self.class.class_eval { attr_accessor mn.pluralize }
end
end end
def set_value(mn, value) def set_value(mn, value)
if current = send(mn) unless current = send(mn)
send("#{mn}=", value)
end
if current = send(mn.pluralize)
current << value current << value
else else
send("#{mn}=", [value]) send("#{mn.pluralize}=", [value])
end end
end end
end end

View file

@ -16,58 +16,61 @@ describe Microformats2::Collection do
JSON.parse(@collection.to_json).should == JSON.parse(json) JSON.parse(@collection.to_json).should == JSON.parse(json)
end end
end end
describe "#card" do describe "'.h-card'" do
it "returns array of HCard objects" do it "assigns all cards to Collection#cards" do
@collection.card.first.should be_kind_of HCard @collection.cards.first.should be_kind_of HCard
end
it "assigns the first card to Collection#card" do
@collection.card.should be_kind_of HCard
end end
end end
describe "'.h-card .p-name'" do describe "'.h-card .p-name'" do
it "assigns all names to HCard#names" do it "assigns all names to HCard#names" do
@collection.first.names.map(&:to_s).should == ["Jessica Lynn Suttles"] @collection.card.names.map(&:to_s).should == ["Jessica Lynn Suttles"]
end end
it "assigns the first name to HCard#name" do it "assigns the first name to HCard#name" do
@collection.first.name.to_s.should == "Jessica Lynn Suttles" @collection.card.name.to_s.should == "Jessica Lynn Suttles"
end end
it "HCard#name is a Property::Text" do it "HCard#name is a Property::Text" do
@collection.first.name.should be_kind_of Microformats2::Property::Text @collection.card.name.should be_kind_of Microformats2::Property::Text
end end
end end
describe "'.h-card .p-url'" do describe "'.h-card .p-url'" do
it "assigns all urls to HCard#urls" do it "assigns all urls to HCard#urls" do
urls = ["http://flickr.com/jlsuttles", "http://twitter.com/jlsuttles"] urls = ["http://flickr.com/jlsuttles", "http://twitter.com/jlsuttles"]
@collection.first.urls.map(&:to_s).should == urls @collection.card.urls.map(&:to_s).should == urls
end end
it "assigns then first url to HCard#url" do it "assigns then first url to HCard#url" do
@collection.first.url.to_s.should == "http://flickr.com/jlsuttles" @collection.card.url.to_s.should == "http://flickr.com/jlsuttles"
end end
it "HCard#url is a Property::Url" do it "HCard#url is a Property::Url" do
@collection.first.url.should be_kind_of Microformats2::Property::Url @collection.card.url.should be_kind_of Microformats2::Property::Url
end end
end end
describe "'.h-card .p-bday'" do describe "'.h-card .p-bday'" do
it "assigns all bdays to HCard#bdays" do it "assigns all bdays to HCard#bdays" do
@collection.first.bdays.map(&:to_s).should == ["1990-10-15"] @collection.card.bdays.map(&:to_s).should == ["1990-10-15"]
end end
it "assigns the first bday to HCard#bday" do it "assigns the first bday to HCard#bday" do
@collection.first.bday.to_s.should == "1990-10-15" @collection.card.bday.to_s.should == "1990-10-15"
end end
it "HCard#bday is a Property::DateTime" do it "HCard#bday is a Property::DateTime" do
@collection.first.bday.should be_kind_of Microformats2::Property::DateTime @collection.card.bday.should be_kind_of Microformats2::Property::DateTime
end end
it "assigns DateTime object to Property::DateTime#value" do it "assigns DateTime object to Property::DateTime#value" do
@collection.first.bday.value.should be_kind_of DateTime @collection.card.bday.value.should be_kind_of DateTime
@collection.first.bday.value.to_s.should == "1990-10-15T00:00:00+00:00" @collection.card.bday.value.to_s.should == "1990-10-15T00:00:00+00:00"
end end
end end
describe "'.h-card .p-content'" do describe "'.h-card .p-content'" do
it "assigns all contents to HCard#contents" do it "assigns all contents to HCard#contents" do
@collection.first.contents.map(&:to_s).should == ["<p>Vegan. Cat lover. Coder.</p>"] @collection.card.contents.map(&:to_s).should == ["<p>Vegan. Cat lover. Coder.</p>"]
end end
it "assigns the first content to HCard#content" do it "assigns the first content to HCard#content" do
@collection.first.content.to_s.should == "<p>Vegan. Cat lover. Coder.</p>" @collection.card.content.to_s.should == "<p>Vegan. Cat lover. Coder.</p>"
end end
it "HCard#content is a Property::Embedded" do it "HCard#content is a Property::Embedded" do
@collection.first.contents.first.should be_kind_of Microformats2::Property::Embedded @collection.card.contents.first.should be_kind_of Microformats2::Property::Embedded
end end
end end
end end
@ -84,31 +87,34 @@ describe Microformats2::Collection do
JSON.parse(@collection.to_json).should == JSON.parse(json) JSON.parse(@collection.to_json).should == JSON.parse(json)
end end
end end
describe "#card" do describe "'.h-card'" do
it "returns array of HCard objects" do it "assigns all cards to Collection#cards" do
@collection.card.first.should be_kind_of HCard @collection.cards.first.should be_kind_of HCard
end
it "assigns the first card to Collection#card" do
@collection.card.should be_kind_of HCard
end end
end end
describe "'.h-card .p-name'" do describe "'.h-card .p-name'" do
it "assigns all names to HCard#names" do it "assigns all names to HCard#names" do
@collection.first.names.map(&:to_s).should == ["jlsuttles"] @collection.card.names.map(&:to_s).should == ["jlsuttles"]
end end
it "assigns the first name to HCard#name" do it "assigns the first name to HCard#name" do
@collection.first.name.to_s.should == "jlsuttles" @collection.card.name.to_s.should == "jlsuttles"
end end
it "HCard#name is a Property::Text" do it "HCard#name is a Property::Text" do
@collection.first.name.should be_kind_of Microformats2::Property::Text @collection.card.name.should be_kind_of Microformats2::Property::Text
end end
end end
describe "'.h-card .p-name .p-nickname'" do describe "'.h-card .p-name .p-nickname'" do
it "assigns all nicknames to HCard#nicknames" do it "assigns all nicknames to HCard#nicknames" do
@collection.first.nicknames.map(&:to_s).should == ["jlsuttles"] @collection.card.nicknames.map(&:to_s).should == ["jlsuttles"]
end end
it "assigns the first nickname to HCard#nickname" do it "assigns the first nickname to HCard#nickname" do
@collection.first.nickname.to_s.should == "jlsuttles" @collection.card.nickname.to_s.should == "jlsuttles"
end end
it "HCard#nickname is a Property::Text" do it "HCard#nickname is a Property::Text" do
@collection.first.nickname.should be_kind_of Microformats2::Property::Text @collection.card.nickname.should be_kind_of Microformats2::Property::Text
end end
end end
end end
@ -125,23 +131,26 @@ describe Microformats2::Collection do
JSON.parse(@collection.to_json).should == JSON.parse(json) JSON.parse(@collection.to_json).should == JSON.parse(json)
end end
end end
describe "#entry" do describe "'.h-entry'" do
it "returns array of HEntry objects" do it "assigns all entrys to Collection#entrys" do
@collection.entry.first.should be_kind_of HEntry @collection.entries.first.should be_kind_of HEntry
end
it "assigns the first entry to Collection#entry" do
@collection.entry.should be_kind_of HEntry
end end
end end
describe "'.h-card .p-author.h-card'" do describe "'.h-card .p-author.h-card'" do
it "assigns all authors to HCard#authors" do it "assigns all authors to HCard#authors" do
@collection.first.authors.map(&:to_s).should == ["Jessica Lynn Suttles"] @collection.entry.authors.map(&:to_s).should == ["Jessica Lynn Suttles"]
end end
it "assigns the first author to HCard#author" do it "assigns the first author to HCard#author" do
@collection.first.author.to_s.should == "Jessica Lynn Suttles" @collection.entry.author.to_s.should == "Jessica Lynn Suttles"
end end
it "HCard#author is a Property::Text" do it "HCard#author is a Property::Text" do
@collection.first.author.should be_kind_of Microformats2::Property::Text @collection.entry.author.should be_kind_of Microformats2::Property::Text
end end
it "assigns HCard to Property::Text#formats" do it "assigns HCard to Property::Text#formats" do
@collection.first.author.formats.first.should be_kind_of HCard @collection.entry.author.formats.first.should be_kind_of HCard
end end
end end
end end