Skip to content

Commit

Permalink
Support initialization of Atom::Generator from hash and block.
Browse files Browse the repository at this point in the history
  • Loading branch information
seangeo committed Apr 21, 2008
1 parent 1db1fcf commit ccb819c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -2,6 +2,7 @@

* Remove useless variable warning. (Sam Roberts)
* Support initialization of Atom::Source from Hash and block.
* Support initialization of Atom::Generator from Hash and block.

== 0.3.3 2008-04-09

Expand Down
17 changes: 13 additions & 4 deletions lib/atom.rb
Expand Up @@ -61,16 +61,25 @@ def initialize
class Generator
include Xml::Parseable

attr_reader :name
attr_accessor :name
attribute :uri, :version

# Initialize a new Generator.
#
# +xml+:: An XML::Reader object.
#
def initialize(xml)
@name = xml.read_string.strip
parse(xml, :once => true)
def initialize(o = nil)
case o
when XML::Reader
@name = o.read_string.strip
parse(o, :once => true)
when Hash
o.each do |k, v|
self.send("#{k.to_s}=", v)
end
end

yield(self) if block_given?
end
end

Expand Down
23 changes: 22 additions & 1 deletion spec/atom_spec.rb
Expand Up @@ -1153,5 +1153,26 @@
source.title.should == 'title'
source.id.should == 'sourceid'
end
end
end

describe Atom::Generator do
it "should create an empty generator" do
lambda { Atom::Generator.new }.should_not raise_error
end

it "should create from a hash" do
source = Atom::Generator.new(:name => 'generator', :uri => 'http://generator')
source.name.should == 'generator'
source.uri.should == 'http://generator'
end

it "should create from a block" do
source = Atom::Generator.new do |source|
source.name = 'generator'
source.uri = 'http://generator'
end
source.name.should == 'generator'
source.uri.should == 'http://generator'
end
end
end

0 comments on commit ccb819c

Please sign in to comment.