From 63c3fd6aa25a2dfc5742d40a646be1b55912117c Mon Sep 17 00:00:00 2001 From: kou Date: Sun, 18 Jun 2006 13:42:43 +0000 Subject: [PATCH] * lib/rss/rss.rb: RSS::Element#initialize accepts initial attributes. * lib/rss/0.9.rb: ditto. * lib/rss/1.0.rb: ditto. * lib/rss/2.0.rb: ditto. * lib/rss/dublincore.rb: ditto. * lib/rss/image.rb: ditto. * lib/rss/taxonomy.rb: ditto. * lib/rss/trackback.rb: ditto. * lib/rss/utils.rb: added Utils.element_initialize_arguments? to detect backward compatibility initial arguments. * lib/rss/parser.rb: user initial attributes to initialize RSS::Element. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10317 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 18 +++++++ lib/rss/0.9.rb | 117 ++++++++++++++++++++++++++---------------- lib/rss/1.0.rb | 101 +++++++++++++++++++++++++----------- lib/rss/2.0.rb | 12 +++-- lib/rss/dublincore.rb | 10 ++-- lib/rss/image.rb | 24 ++++++--- lib/rss/parser.rb | 8 ++- lib/rss/rss.rb | 18 +++++-- lib/rss/taxonomy.rb | 21 +++++--- lib/rss/trackback.rb | 40 ++++++++++----- lib/rss/utils.rb | 7 +-- 11 files changed, 256 insertions(+), 120 deletions(-) diff --git a/ChangeLog b/ChangeLog index 058f35f3ce..b961cc752b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +Sun Jun 18 22:36:13 2006 Kouhei Sutou + + * lib/rss/rss.rb: RSS::Element#initialize accepts initial + attributes. + * lib/rss/0.9.rb: ditto. + * lib/rss/1.0.rb: ditto. + * lib/rss/2.0.rb: ditto. + * lib/rss/dublincore.rb: ditto. + * lib/rss/image.rb: ditto. + * lib/rss/taxonomy.rb: ditto. + * lib/rss/trackback.rb: ditto. + + * lib/rss/utils.rb: added Utils.element_initialize_arguments? to + detect backward compatibility initial arguments. + + * lib/rss/parser.rb: user initial attributes to initialize + RSS::Element. + Sun Jun 18 18:24:42 2006 Kouhei Sutou * lib/rss/converter.rb: use NKF for Uconv fallback. diff --git a/lib/rss/0.9.rb b/lib/rss/0.9.rb index a5009f59ec..f614eff0c8 100644 --- a/lib/rss/0.9.rb +++ b/lib/rss/0.9.rb @@ -149,10 +149,6 @@ module RSS install_model(name, occurs) end - def initialize() - super() - end - def to_s(need_convert=true, indent='') rv = tag(indent) do |next_indent| [ @@ -274,9 +270,13 @@ module RSS content_setup - def initialize(content=nil) - super() - self.content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.content = args[0] + end end end @@ -319,9 +319,13 @@ module RSS content_setup(:integer) - def initialize(content=nil) - super() - self.content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.content = args[0] + end end end @@ -344,15 +348,18 @@ module RSS install_model(name, "?") end - def initialize(url=nil, title=nil, link=nil, width=nil, height=nil, - description=nil) - super() - self.url = url - self.title = title - self.link = link - self.width = width - self.height = height - self.description = description + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.url = args[0] + self.title = args[1] + self.link = args[2] + self.width = args[3] + self.height = args[4] + self.description = args[5] + end end def to_s(need_convert=true, indent='') @@ -399,13 +406,17 @@ module RSS install_get_attribute(name, uri, required, type) end - def initialize(domain=nil, port=nil, path=nil, rp=nil, protocol=nil) - super() - self.domain = domain - self.port = port - self.path = path - self.registerProcedure = rp - self.protocol = protocol + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.domain = args[0] + self.port = args[1] + self.path = args[2] + self.registerProcedure = args[3] + self.protocol = args[4] + end end def to_s(need_convert=true, indent='') @@ -514,10 +525,14 @@ module RSS content_setup - def initialize(url=nil, content=nil) - super() - self.url = url - self.content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.url = args[0] + self.content = args[1] + end end private @@ -554,11 +569,15 @@ module RSS install_get_attribute(name, uri, required, type) end - def initialize(url=nil, length=nil, type=nil) - super() - self.url = url - self.length = length - self.type = type + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.url = args[0] + self.length = args[1] + self.type = args[2] + end end def to_s(need_convert=true, indent='') @@ -599,10 +618,14 @@ module RSS content_setup - def initialize(domain=nil, content=nil) - super() - self.domain = domain - self.content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.domain = args[0] + self.content = args[1] + end end private @@ -634,12 +657,16 @@ module RSS install_model(name, nil) end - def initialize(title=nil, description=nil, name=nil, link=nil) - super() - self.title = title - self.description = description - self.name = name - self.link = link + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.title = args[0] + self.description = args[1] + self.name = args[2] + self.link = args[3] + end end def to_s(need_convert=true, indent='') diff --git a/lib/rss/1.0.rb b/lib/rss/1.0.rb index 6e601d1a43..4438c40d33 100644 --- a/lib/rss/1.0.rb +++ b/lib/rss/1.0.rb @@ -107,9 +107,13 @@ module RSS install_get_attribute(name, uri, required) end - def initialize(resource=nil) - super() - self.resource = resource + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end def full_name @@ -148,9 +152,13 @@ module RSS install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(li=[]) - super() - @li = li + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + @li = args[0] if args[0] + end end def to_s(need_convert=true, indent='') @@ -208,9 +216,13 @@ module RSS install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(li=[]) - super() - @li = li + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + @li = args[0] if args[0] + end end def to_s(need_convert=true, indent='') @@ -287,9 +299,13 @@ module RSS install_model(tag, occurs) end - def initialize(about=nil) - super() - self.about = about + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] + end end def to_s(need_convert=true, indent='') @@ -358,9 +374,13 @@ module RSS install_get_attribute(name, uri, required) end - def initialize(resource=nil) - super() - self.resource = resource + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end def to_s(need_convert=true, indent='') @@ -395,9 +415,13 @@ module RSS install_get_attribute(name, uri, required) end - def initialize(resource=nil) - super() - self.resource = resource + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end def to_s(need_convert=true, indent='') @@ -432,9 +456,14 @@ module RSS install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(seq=Seq.new) - super() - @Seq = seq + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.Seq = args[0] + end + self.Seq ||= Seq.new end def to_s(need_convert=true, indent='') @@ -503,9 +532,13 @@ module RSS install_model(tag, occurs) end - def initialize(about=nil) - super() - self.about = about + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] + end end def to_s(need_convert=true, indent='') @@ -573,9 +606,13 @@ module RSS install_model(tag, occurs) end - def initialize(about=nil) - super() - self.about = about + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] + end end def to_s(need_convert=true, indent='') @@ -648,9 +685,13 @@ module RSS install_model(tag, occurs) end - def initialize(about=nil) - super() - self.about = about + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] + end end def to_s(need_convert=true, indent='') diff --git a/lib/rss/2.0.rb b/lib/rss/2.0.rb index aa8bee479a..6ba22805b0 100644 --- a/lib/rss/2.0.rb +++ b/lib/rss/2.0.rb @@ -131,10 +131,14 @@ EOT content_setup - def initialize(isPermaLink=nil, content=nil) - super() - self.isPermaLink = isPermaLink - self.content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.isPermaLink = args[0] + self.content = args[1] + end end alias_method :_PermaLink?, :PermaLink? diff --git a/lib/rss/dublincore.rb b/lib/rss/dublincore.rb index 646d1182d9..605b1f7e42 100644 --- a/lib/rss/dublincore.rb +++ b/lib/rss/dublincore.rb @@ -97,9 +97,13 @@ module RSS alias_method(:value, :content) alias_method(:value=, :content=) - def initialize(content=nil) - super() - self.content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.content = args[0] + end end def full_name diff --git a/lib/rss/image.rb b/lib/rss/image.rb index 035df680cb..9b86644ff7 100644 --- a/lib/rss/image.rb +++ b/lib/rss/image.rb @@ -75,10 +75,14 @@ module RSS alias height= image_height= alias height image_height - def initialize(about=nil, resource=nil) - super() - self.about = about - self.resource = resource + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] + self.resource = args[1] + end end def full_name @@ -179,10 +183,14 @@ module RSS alias image_size= size= alias image_size size - def initialize(about=nil, size=nil) - super() - self.about = about - self.size = size + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] + self.size = args[1] + end end def full_name diff --git a/lib/rss/parser.rb b/lib/rss/parser.rb index ce9fff0b38..9b9163dad2 100644 --- a/lib/rss/parser.rb +++ b/lib/rss/parser.rb @@ -377,8 +377,7 @@ module RSS check_ns(tag_name, prefix, ns, klass.required_uri) - args = [] - + attributes = {} klass.get_attributes.each do |a_name, a_uri, required| if a_uri.is_a?(String) or !a_uri.respond_to?(:include?) @@ -407,12 +406,11 @@ module RSS raise MissingAttributeError.new(tag_name, a_name) end - args << val + attributes[a_name] = val end previous = @last_element - next_element = klass.new(*args) - next_element.do_validate = @do_validate + next_element = klass.new(@do_validate, attributes) previous.instance_eval {set_next_element(tag_name, next_element)} @last_element = next_element @proc_stack.push Proc.new { |text, tags| diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index b1e1b55e4e..5900d5078c 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -489,6 +489,7 @@ EOC alias_method "\#{$POSTMATCH}?", name end GET_ATTRIBUTES << [name, uri, required] + add_need_initialize_variable(disp_name) end def self.def_corresponded_attr_writer(name, type=nil, disp_name=name) @@ -552,10 +553,10 @@ EOC attr_accessor :do_validate - def initialize(do_validate=true) + def initialize(do_validate=true, attrs={}) @converter = nil @do_validate = do_validate - initialize_variables + initialize_variables(attrs) end def tag_name @@ -605,9 +606,18 @@ EOC end private - def initialize_variables + def initialize_variables(attrs) + normalized_attrs = {} + attrs.each do |key, value| + normalized_attrs[key.to_s] = value + end self.class.need_initialize_variables.each do |variable_name| - instance_eval("@#{variable_name} = nil") + value = normalized_attrs[variable_name.to_s] + if value + __send__("#{variable_name}=", value) + else + instance_eval("@#{variable_name} = nil") + end end initialize_have_children_elements @content = "" if self.class.have_content? diff --git a/lib/rss/taxonomy.rb b/lib/rss/taxonomy.rb index 5b7d33821d..70a9d442cd 100644 --- a/lib/rss/taxonomy.rb +++ b/lib/rss/taxonomy.rb @@ -68,9 +68,14 @@ module RSS install_must_call_validator('rdf', ::RSS::RDF::URI) - def initialize(bag=Bag.new) - super() - @Bag = bag + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.Bag = args[0] + end + self.Bag ||= Bag.new end def full_name @@ -155,9 +160,13 @@ module RSS install_get_attribute("about", ::RSS::RDF::URI, true) install_text_element("#{TAXO_PREFIX}_link") - def initialize(about=nil) - super() - @about = about + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.about = args[0] + end end def full_name diff --git a/lib/rss/trackback.rb b/lib/rss/trackback.rb index d782925c0b..32e431c674 100644 --- a/lib/rss/trackback.rb +++ b/lib/rss/trackback.rb @@ -136,9 +136,13 @@ module RSS alias_method(:value, :resource) alias_method(:value=, :resource=) - def initialize(resource=nil) - super() - @resource = resource + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end def full_name @@ -186,9 +190,13 @@ module RSS alias_method(:value, :resource) alias_method(:value=, :resource=) - def initialize(resource=nil) - super() - @resource = resource + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.resource = args[0] + end end def full_name @@ -245,9 +253,13 @@ module RSS alias_method(:value, :content) alias_method(:value=, :content=) - def initialize(content=nil) - super() - @content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.content = args[0] + end end def full_name @@ -278,9 +290,13 @@ module RSS alias_method(:value, :content) alias_method(:value=, :content=) - def initialize(content=nil) - super() - @content = content + def initialize(*args) + if Utils.element_initialize_arguments?(args) + super + else + super() + self.content = args[0] + end end def full_name diff --git a/lib/rss/utils.rb b/lib/rss/utils.rb index 0286becf00..031ff3072b 100644 --- a/lib/rss/utils.rb +++ b/lib/rss/utils.rb @@ -1,7 +1,5 @@ module RSS - module Utils - module_function def to_class_name(name) name.split(/_/).collect do |part| @@ -26,6 +24,9 @@ module RSS klass.new(value) end end - end + def element_initialize_arguments?(args) + [true, false].include?(args[0]) and args[1].is_a?(Hash) + end + end end