From 80861909e66342872332c47e57d8d42e68b0759c Mon Sep 17 00:00:00 2001 From: kou Date: Sat, 9 Apr 2005 03:09:52 +0000 Subject: * lib/rss: backoported from HEAD. * lib/rss: refactored. - gave a name to 'x'. - undef_method -> remove_method for avoiding a warning in ruby 1.6. * lib/rss/parser.rb: @@setter -> @@setters. * lib/rss/parser.rb (RSS::BaseListener.register_uri) (RSS::BaseListener.uri_registered?) (RSS::BaseListener.install_get_text_element): swapped the first argument and the second argument. * lib/rss/taxonomy.rb: swapped the first argument and the second argument for RSS::BaseListener.install_get_text_element. * lib/rss/image.rb: ditto. * lib/rss/syndication.rb: ditto. * lib/rss/dublincore.rb: ditto. * lib/rss/parser.rb: ditto. * lib/rss/1.0.rb: ditto. * lib/rss/2.0.rb: ditto. * lib/rss/0.9.rb: ditto. * lib/rss/content.rb: ditto. * lib/rss/parser.rb (RSS::BaseListener.install_setter) (RSS::BaseListener.register_uri): changed fallback way. * lib/rss/parser.rb: added class name registry for complex model elements. (ex. have childlen elements, have some attributes and a child element and so on.) * lib/rss/dublincore.rb: supported multiple Dublin Core items. * lib/rss/maker/dublincore.rb: ditto. * lib/rss/maker/image.rb: supproted new Dublin Core API. * lib/rss/maker/base.rb: added default current_element implementation. * lib/rss/trackback.rb (RSS::TrackBackUtils.new_with_value_if_need): moved to RSS::Utils. * lib/rss/utils.rb (RSS::Utils.new_with_value_if_need): moved from RSS::TrackBackUtils. * lib/rss/maker/image.rb: fixed invalid argument of add_need_initialize_variable bug. * lib/rss/maker/trackback.rb: ditto. * lib/rss/rss.rb (Hash#merge): added for ruby 1.6. * lib/rss/rss.rb (RSS::BaseModel.date_writer): changed to accept nil for date value. * test/test_dublincore.rb: added tests for plural accessor and multiple Dublin Core items. * test/test_setup_maker_1.0.rb: fixed swapped actual and expected values. * test/rss/rss-assertions.rb (assert_multiple_dublin_core): added an assertion for testing multiple Dublin Core items. * test/rss/test_maker_dc.rb (test_rss10_multiple): added a test for making multiple Dublin Core items. * test/rss/test_maker_dc.rb (test_date): added a test for #date= and #dc_date=. * sample/rss/tdiary_plugin/rss-recent.rb: new option: @options['rss-recent.use-image-link']: use image as link instread of text if available. * sample/rss/tdiary_plugin/rss-recent.rb (RSS_RECENT_VERSION): 0.0.5 -> 0.0.6. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@8284 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 87 ++++++++++++++++++++ lib/rss/0.9.rb | 100 ++++++++++++----------- lib/rss/1.0.rb | 50 ++++++------ lib/rss/2.0.rb | 40 ++++----- lib/rss/content.rb | 13 +-- lib/rss/dublincore.rb | 143 +++++++++++++++++++++++++++------ lib/rss/image.rb | 6 +- lib/rss/maker/base.rb | 8 +- lib/rss/maker/dublincore.rb | 142 +++++++++++++++++++++++++++++--- lib/rss/maker/image.rb | 15 +++- lib/rss/maker/trackback.rb | 2 +- lib/rss/parser.rb | 54 ++++++++----- lib/rss/rss.rb | 81 +++++++++++-------- lib/rss/syndication.rb | 17 ++-- lib/rss/taxonomy.rb | 12 +-- lib/rss/trackback.rb | 26 +++--- lib/rss/utils.rb | 7 ++ sample/rss/tdiary_plugin/rss-recent.rb | 42 +++++++--- test/rss/rss-assertions.rb | 10 +++ test/rss/test_dublincore.rb | 72 ++++++++++++++--- test/rss/test_maker_dc.rb | 74 ++++++++++++++++- test/rss/test_setup_maker_1.0.rb | 4 +- 22 files changed, 748 insertions(+), 257 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7f23f7d0a3..bffb70856a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,90 @@ +Sat Apr 9 11:59:57 2005 Kouhei Sutou + + * lib/rss: backoported from HEAD. + + * lib/rss: refactored. + - gave a name to 'x'. + - undef_method -> remove_method for avoiding a warning in ruby 1.6. + + + * lib/rss/parser.rb: @@setter -> @@setters. + + * lib/rss/parser.rb + (RSS::BaseListener.register_uri) + (RSS::BaseListener.uri_registered?) + (RSS::BaseListener.install_get_text_element): + swapped the first argument and the second argument. + + * lib/rss/taxonomy.rb: swapped the first argument and the second + argument for RSS::BaseListener.install_get_text_element. + * lib/rss/image.rb: ditto. + * lib/rss/syndication.rb: ditto. + * lib/rss/dublincore.rb: ditto. + * lib/rss/parser.rb: ditto. + * lib/rss/1.0.rb: ditto. + * lib/rss/2.0.rb: ditto. + * lib/rss/0.9.rb: ditto. + * lib/rss/content.rb: ditto. + + * lib/rss/parser.rb + (RSS::BaseListener.install_setter) + (RSS::BaseListener.register_uri): changed fallback way. + + * lib/rss/parser.rb: added class name registry for complex model + elements. (ex. have childlen elements, have some attributes and + a child element and so on.) + + + * lib/rss/dublincore.rb: supported multiple Dublin Core items. + * lib/rss/maker/dublincore.rb: ditto. + + * lib/rss/maker/image.rb: supproted new Dublin Core API. + + + * lib/rss/maker/base.rb: added default current_element implementation. + + + * lib/rss/trackback.rb (RSS::TrackBackUtils.new_with_value_if_need): + moved to RSS::Utils. + + * lib/rss/utils.rb (RSS::Utils.new_with_value_if_need): + moved from RSS::TrackBackUtils. + + + * lib/rss/maker/image.rb: fixed invalid argument of + add_need_initialize_variable bug. + * lib/rss/maker/trackback.rb: ditto. + + + * lib/rss/rss.rb (Hash#merge): added for ruby 1.6. + + * lib/rss/rss.rb (RSS::BaseModel.date_writer): changed to accept nil + for date value. + + + * test/test_dublincore.rb: added tests for plural accessor and + multiple Dublin Core items. + + * test/test_setup_maker_1.0.rb: fixed swapped actual and expected + values. + + * test/rss/rss-assertions.rb (assert_multiple_dublin_core): added + an assertion for testing multiple Dublin Core items. + + * test/rss/test_maker_dc.rb (test_rss10_multiple): added a test + for making multiple Dublin Core items. + + * test/rss/test_maker_dc.rb (test_date): added a test for #date= + and #dc_date=. + + + * sample/rss/tdiary_plugin/rss-recent.rb: + new option: @options['rss-recent.use-image-link']: + use image as link instread of text if available. + + * sample/rss/tdiary_plugin/rss-recent.rb (RSS_RECENT_VERSION): + 0.0.5 -> 0.0.6. + Fri Apr 8 20:17:48 2005 Nobuyoshi Nakada * ext/extmk.rb (extmake): hdrdir needs to be defined also in diff --git a/lib/rss/0.9.rb b/lib/rss/0.9.rb index c780cdb66a..a473322612 100644 --- a/lib/rss/0.9.rb +++ b/lib/rss/0.9.rb @@ -25,8 +25,8 @@ module RSS install_model(tag, occurs) end - %w(channel).each do |x| - install_have_child_element(x) + %w(channel).each do |name| + install_have_child_element(name) end attr_accessor :rss_version, :version, :encoding, :standalone @@ -78,7 +78,9 @@ module RSS def _tags [ [nil, 'channel'], - ].delete_if {|x| send(x[1]).nil?} + ].delete_if do |uri, name| + send(name).nil? + end end def _attrs @@ -101,17 +103,17 @@ module RSS ["webMaster", "?"], ["rating", "?"], ["docs", "?"], - ].each do |x, occurs| - install_text_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_text_element(name) + install_model(name, occurs) end [ ["pubDate", "?"], ["lastBuildDate", "?"], - ].each do |x, occurs| - install_date_element(x, 'rfc822') - install_model(x, occurs) + ].each do |name, occurs| + install_date_element(name, 'rfc822') + install_model(name, occurs) end alias date pubDate alias date= pubDate= @@ -121,23 +123,23 @@ module RSS ["skipHours", "?"], ["image", nil], ["textInput", "?"], - ].each do |x, occurs| - install_have_child_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_have_child_element(name) + install_model(name, occurs) end [ ["cloud", "?"] - ].each do |x, occurs| - install_have_attribute_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_have_attribute_element(name) + install_model(name, occurs) end [ ["item", "*"] - ].each do |x, occurs| - install_have_children_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_have_children_element(name) + install_model(name, occurs) end def initialize() @@ -192,8 +194,8 @@ module RSS "image", "textInput", "cloud", - ].delete_if do |x| - send(x).nil? + ].delete_if do |name| + send(name).nil? end.collect do |elem| [nil, elem] end @@ -234,9 +236,9 @@ module RSS [ ["day", "*"] - ].each do |x, occurs| - install_have_children_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_have_children_element(name) + install_model(name, occurs) end def to_s(need_convert=true, indent=calc_indent) @@ -279,9 +281,9 @@ module RSS [ ["hour", "*"] - ].each do |x, occurs| - install_have_children_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_have_children_element(name) + install_model(name, occurs) end def to_s(need_convert=true, indent=calc_indent) @@ -328,13 +330,13 @@ module RSS include RSS09 - %w(url title link).each do |x| - install_text_element(x) - install_model(x, nil) + %w(url title link).each do |name| + install_text_element(name) + install_model(name, nil) end - %w(width height description).each do |x| - install_text_element(x) - install_model(x, "?") + %w(width height description).each do |name| + install_text_element(name) + install_model(name, "?") end def to_s(need_convert=true, indent=calc_indent) @@ -355,8 +357,8 @@ module RSS private def _tags - %w(url title link width height description).delete_if do |x| - send(x).nil? + %w(url title link width height description).delete_if do |name| + send(name).nil? end.collect do |elem| [nil, elem] end @@ -409,12 +411,12 @@ module RSS include RSS09 - %w(title link description).each do |x| - install_text_element(x) + %w(title link description).each do |name| + install_text_element(name) end - %w(source enclosure).each do |x| - install_have_child_element(x) + %w(source enclosure).each do |name| + install_have_child_element(name) end [ @@ -457,10 +459,10 @@ module RSS def _tags rv = %w(title link description author comments - source enclosure).delete_if do |x| - send(x).nil? - end.collect do |x| - [nil, x] + source enclosure).delete_if do |name| + send(name).nil? + end.collect do |name| + [nil, name] end @category.each do @@ -607,9 +609,9 @@ module RSS include RSS09 - %w(title description name link).each do |x| - install_text_element(x) - install_model(x, nil) + %w(title description name link).each do |name| + install_text_element(name) + install_model(name, nil) end def to_s(need_convert=true, indent=calc_indent) @@ -628,8 +630,8 @@ module RSS private def _tags - %w(title description name link).each do |x| - send(x).nil? + %w(title description name link).each do |name| + send(name).nil? end.collect do |elem| [nil, elem] end @@ -644,8 +646,8 @@ module RSS end - RSS09::ELEMENTS.each do |x| - BaseListener.install_get_text_element(x, nil, "#{x}=") + RSS09::ELEMENTS.each do |name| + BaseListener.install_get_text_element(nil, name, "#{name}=") end module ListenerMixin diff --git a/lib/rss/1.0.rb b/lib/rss/1.0.rb index 86c19afc70..0e0f8d9fd2 100644 --- a/lib/rss/1.0.rb +++ b/lib/rss/1.0.rb @@ -44,8 +44,8 @@ module RSS install_model(tag, occurs) end - %w(channel image textinput).each do |x| - install_have_child_element(x) + %w(channel image textinput).each do |name| + install_have_child_element(name) end install_have_children_element("item") @@ -87,8 +87,8 @@ module RSS rv = [ [::RSS::URI, "channel"], [::RSS::URI, "image"], - ].delete_if {|x| send(x[1]).nil?} - @item.each do |x| + ].delete_if {|uri, name| send(name).nil?} + @item.each do |item| rv << [::RSS::URI, "item"] end rv << [::RSS::URI, "textinput"] if @textinput @@ -142,7 +142,7 @@ module RSS def _tags rv = [] - @li.each do |x| + @li.each do |li| rv << [URI, "li"] end rv @@ -210,12 +210,12 @@ module RSS install_get_attribute(name, uri, required) end - %w(title link description).each do |x| - install_text_element(x) + %w(title link description).each do |name| + install_text_element(name) end - %w(image items textinput).each do |x| - install_have_child_element(x) + %w(image items textinput).each do |name| + install_have_child_element(name) end [ @@ -263,8 +263,8 @@ module RSS [::RSS::URI, 'image'], [::RSS::URI, 'items'], [::RSS::URI, 'textinput'], - ].delete_if do |x| - send(x[1]).nil? + ].delete_if do |uri, name| + send(name).nil? end end @@ -431,8 +431,8 @@ module RSS install_get_attribute(name, uri, required) end - %w(title url link).each do |x| - install_text_element(x) + %w(title url link).each do |name| + install_text_element(name) end [ @@ -467,8 +467,8 @@ module RSS [::RSS::URI, 'title'], [::RSS::URI, 'url'], [::RSS::URI, 'link'], - ].delete_if do |x| - send(x[1]).nil? + ].delete_if do |uri, name| + send(name).nil? end end @@ -501,8 +501,8 @@ module RSS install_get_attribute(name, uri, required) end - %w(title link description).each do |x| - install_text_element(x) + %w(title link description).each do |name| + install_text_element(name) end [ @@ -537,8 +537,8 @@ module RSS [::RSS::URI, 'title'], [::RSS::URI, 'link'], [::RSS::URI, 'description'], - ].delete_if do |x| - send(x[1]).nil? + ].delete_if do |uri, name| + send(name).nil? end end @@ -571,8 +571,8 @@ module RSS install_get_attribute(name, uri, required) end - %w(title description name link).each do |x| - install_text_element(x) + %w(title description name link).each do |name| + install_text_element(name) end [ @@ -610,8 +610,8 @@ module RSS [::RSS::URI, 'description'], [::RSS::URI, 'name'], [::RSS::URI, 'link'], - ].delete_if do |x| - send(x[1]).nil? + ].delete_if do |uri, name| + send(name).nil? end end @@ -628,8 +628,8 @@ module RSS end - RSS10::ELEMENTS.each do |x| - BaseListener.install_get_text_element(x, URI, "#{x}=") + RSS10::ELEMENTS.each do |name| + BaseListener.install_get_text_element(URI, name, "#{name}=") end module ListenerMixin diff --git a/lib/rss/2.0.rb b/lib/rss/2.0.rb index 4bd46512f4..af6ced9f7c 100644 --- a/lib/rss/2.0.rb +++ b/lib/rss/2.0.rb @@ -6,9 +6,9 @@ module RSS class Channel - %w(generator ttl).each do |x| - install_text_element(x) - install_model(x, '?') + %w(generator ttl).each do |name| + install_text_element(name) + install_model(name, '?') end remove_method :ttl= @@ -26,8 +26,8 @@ module RSS [ ["image", "?"], ["language", "?"], - ].each do |x, occurs| - install_model(x, occurs) + ].each do |name, occurs| + install_model(name, occurs) end def other_element(need_convert, indent) @@ -47,8 +47,8 @@ EOT alias _tags09 _tags def _tags - rv = %w(generator ttl).delete_if do |x| - send(x).nil? + rv = %w(generator ttl).delete_if do |name| + send(name).nil? end.collect do |elem| [nil, elem] end + _tags09 @@ -67,25 +67,25 @@ EOT [ ["comments", "?"], ["author", "?"], - ].each do |x, occurs| - install_text_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_text_element(name) + install_model(name, occurs) end [ ["pubDate", '?'], - ].each do |x, occurs| - install_date_element(x, 'rfc822') - install_model(x, occurs) + ].each do |name, occurs| + install_date_element(name, 'rfc822') + install_model(name, occurs) end alias date pubDate alias date= pubDate= [ ["guid", '?'], - ].each do |x, occurs| - install_have_child_element(x) - install_model(x, occurs) + ].each do |name, occurs| + install_have_child_element(name) + install_model(name, occurs) end def other_element(need_convert, indent) @@ -108,8 +108,8 @@ EOT alias _tags09 _tags def _tags - %w(comments author pubDate guid).delete_if do |x| - send(x).nil? + %w(comments author pubDate guid).delete_if do |name| + send(name).nil? end.collect do |elem| [nil, elem] end + _tags09 @@ -162,8 +162,8 @@ EOT end - RSS09::ELEMENTS.each do |x| - BaseListener.install_get_text_element(x, nil, "#{x}=") + RSS09::ELEMENTS.each do |name| + BaseListener.install_get_text_element(nil, name, "#{name}=") end end diff --git a/lib/rss/content.rb b/lib/rss/content.rb index 280bbb8d6b..a732cec973 100644 --- a/lib/rss/content.rb +++ b/lib/rss/content.rb @@ -17,16 +17,16 @@ module RSS super klass.module_eval(<<-EOC, *get_file_and_line_from_caller(1)) - %w(encoded).each do |x| - install_text_element("\#{CONTENT_PREFIX}_\#{x}") + %w(encoded).each do |name| + install_text_element("\#{CONTENT_PREFIX}_\#{name}") end EOC end def content_validate(tags) counter = {} - ELEMENTS.each do |x| - counter[x] = 0 + ELEMENTS.each do |name| + counter[name] = 0 end tags.each do |tag| @@ -45,8 +45,9 @@ module RSS prefix_size = CONTENT_PREFIX.size + 1 ContentModel::ELEMENTS.uniq! - ContentModel::ELEMENTS.each do |x| - BaseListener.install_get_text_element(x[prefix_size..-1], CONTENT_URI, "#{x}=") + ContentModel::ELEMENTS.each do |full_name| + name = full_name[prefix_size..-1] + BaseListener.install_get_text_element(CONTENT_URI, name, "#{full_name}=") end end diff --git a/lib/rss/dublincore.rb b/lib/rss/dublincore.rb index 016072edf1..5571640bf2 100644 --- a/lib/rss/dublincore.rb +++ b/lib/rss/dublincore.rb @@ -7,41 +7,135 @@ module RSS RDF.install_ns(DC_PREFIX, DC_URI) + module BaseDublinCoreModel + def append_features(klass) + super + + return if klass.instance_of?(Module) + DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name| + plural = plural_name || "#{name}s" + full_name = "#{DC_PREFIX}_#{name}" + full_plural_name = "#{DC_PREFIX}_#{plural}" + klass_name = "DublinCore#{Utils.to_class_name(name)}" + klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0)) + install_have_children_element(#{full_name.dump}, + #{full_plural_name.dump}) + + remove_method :#{full_name} + remove_method :#{full_name}= + remove_method :set_#{full_name} + + def #{full_name} + @#{full_name}.first and @#{full_name}.first.value + end + + def #{full_name}=(new_value) + @#{full_name}[0] = Utils.new_with_value_if_need(#{klass_name}, new_value) + end + alias set_#{full_name} #{full_name}= + EOC + end + klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0)) + alias date #{DC_PREFIX}_date + alias date= #{DC_PREFIX}_date= + EOC + end + end + module DublinCoreModel extend BaseModel + extend BaseDublinCoreModel - ELEMENTS = [] + TEXT_ELEMENTS = { + "title" => nil, + "description" => nil, + "creator" => nil, + "subject" => nil, + "publisher" => nil, + "contributor" => nil, + "type" => nil, + "format" => nil, + "identifier" => nil, + "source" => nil, + "language" => nil, + "relation" => nil, + "coverage" => nil, + "rights" => "rightses" # FIXME + } - def self.append_features(klass) - super + DATE_ELEMENTS = { + "date" => "w3cdtf", + } + + ELEMENT_NAME_INFOS = DublinCoreModel::TEXT_ELEMENTS.to_a + DublinCoreModel::DATE_ELEMENTS.each do |name, | + ELEMENT_NAME_INFOS << [name, nil] + end + + ELEMENTS = TEXT_ELEMENTS.keys + DATE_ELEMENTS.keys + + ELEMENTS.each do |name, plural_name| + module_eval(<<-EOC, *get_file_and_line_from_caller(0)) + class DublinCore#{Utils.to_class_name(name)} < Element + include RSS10 + + content_setup + + class << self + def required_prefix + DC_PREFIX + end + + def required_uri + DC_URI + end + end + + @tag_name = #{name.dump} + + alias_method(:value, :content) + alias_method(:value=, :content=) + + def initialize(content=nil) + super() + self.content = content + end - klass.module_eval(<<-EOC, *get_file_and_line_from_caller(1)) - %w(title description creator subject publisher - contributor type format identifier source - language relation coverage rights).each do |x| - install_text_element("\#{DC_PREFIX}_\#{x}") - end + def full_name + tag_name_with_prefix(DC_PREFIX) + end + + def maker_target(target) + target.new_#{name} + end - %w(date).each do |x| - install_date_element("\#{DC_PREFIX}_\#{x}", 'w3cdtf', x) + def setup_maker_attributes(#{name}) + #{name}.content = content + end end - alias date #{DC_PREFIX}_date - alias date= #{DC_PREFIX}_date= EOC end - def dc_validate(tags) - counter = {} - ELEMENTS.each do |x| - counter[x] = 0 - end + DATE_ELEMENTS.each do |name, type| + module_eval(<<-EOC, *get_file_and_line_from_caller(0)) + class DublinCore#{Utils.to_class_name(name)} < Element + remove_method(:content=) + remove_method(:value=) + date_writer("content", #{type.dump}, #{name.dump}) + + alias_method(:value=, :content=) + end + EOC + end + + def dc_validate(tags) tags.each do |tag| key = "#{DC_PREFIX}_#{tag}" - raise UnknownTagError.new(tag, DC_URI) unless counter.has_key?(key) - counter[key] += 1 - raise TooMuchTagError.new(tag, tag_name) if counter[key] > 1 + unless DublinCoreModel::ELEMENTS.include?(key) + raise UnknownTagError.new(tag, DC_URI) + end end end @@ -57,9 +151,10 @@ module RSS class Textinput; include DublinCoreModel; end end - prefix_size = DC_PREFIX.size + 1 - DublinCoreModel::ELEMENTS.each do |x| - BaseListener.install_get_text_element(x[prefix_size..-1], DC_URI, "#{x}=") + DublinCoreModel::ELEMENTS.each do |name| + class_name = Utils.to_class_name(name) + BaseListener.install_class_name(DC_URI, name, "DublinCore#{class_name}") end + DublinCoreModel::ELEMENTS.collect! {|name| "#{DC_PREFIX}_#{name}"} end diff --git a/lib/rss/image.rb b/lib/rss/image.rb index 9cc3c73018..4dea91925b 100644 --- a/lib/rss/image.rb +++ b/lib/rss/image.rb @@ -54,7 +54,7 @@ module RSS %w(width height).each do |tag| full_name = "#{IMAGE_PREFIX}_#{tag}" install_text_element(full_name) - BaseListener.install_get_text_element(tag, IMAGE_URI, "#{full_name}=") + BaseListener.install_get_text_element(IMAGE_URI, tag, "#{full_name}=") end def initialize(about=nil, resource=nil) @@ -105,8 +105,8 @@ module RSS [ [IMAGE_URI, 'width'], [IMAGE_URI, 'height'], - ].delete_if do |x| - send(x[1]).nil? + ].delete_if do |uri, name| + send(name).nil? end end diff --git a/lib/rss/maker/base.rb b/lib/rss/maker/base.rb index 0b157b1ee2..629f794b07 100644 --- a/lib/rss/maker/base.rb +++ b/lib/rss/maker/base.rb @@ -81,6 +81,10 @@ module RSS end end + def current_element(rss) + rss + end + def setup_values(target) set = false if have_required_values? @@ -181,10 +185,6 @@ EOC end end - def current_element(rss) - rss - end - private remove_method :make_xml_stylesheets def make_xml_stylesheets diff --git a/lib/rss/maker/dublincore.rb b/lib/rss/maker/dublincore.rb index 3eeb269df1..1876ec36ce 100644 --- a/lib/rss/maker/dublincore.rb +++ b/lib/rss/maker/dublincore.rb @@ -7,16 +7,94 @@ module RSS def self.append_features(klass) super - ::RSS::DublinCoreModel::ELEMENTS.uniq.each do |element| - klass.add_need_initialize_variable(element) - klass.add_other_element(element) - klass.__send__(:attr_accessor, element) + ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name| + plural_name ||= "#{name}s" + full_name = "#{RSS::DC_PREFIX}_#{name}" + full_plural_name = "#{RSS::DC_PREFIX}_#{plural_name}" + klass_name = Utils.to_class_name(name) + plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}" + full_plural_klass_name = "self.class::#{plural_klass_name}" + full_klass_name = "#{full_plural_klass_name}::#{klass_name}" + klass.add_need_initialize_variable(full_plural_name, + "make_#{full_plural_name}") + klass.add_other_element(full_plural_name) + klass.__send__(:attr_accessor, full_plural_name) klass.module_eval(<<-EOC, __FILE__, __LINE__) - def setup_#{element}(rss, current) - if #{element} and current.respond_to?(:#{element}=) - current.#{element} = #{element} + def make_#{full_plural_name} + #{full_plural_klass_name}.new(@maker) + end + + def setup_#{full_plural_name}(rss, current) + @#{full_plural_name}.to_rss(rss, current) + end + + def #{full_name} + @#{full_plural_name}[0] and @#{full_plural_name}[0].value + end + + def #{full_name}=(new_value) + @#{full_plural_name}[0] = #{full_klass_name}.new(self) + @#{full_plural_name}[0].value = new_value + end +EOC + end + end + + ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name| + plural_name ||= "#{name}s" + klass_name = Utils.to_class_name(name) + plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}" + module_eval(<<-EOC, __FILE__, __LINE__) + class #{plural_klass_name}Base + include Base + + def_array_element(#{plural_name.dump}) + + def new_#{name} + #{name} = self.class::#{klass_name}.new(self) + @#{plural_name} << #{name} + #{name} + end + + def to_rss(rss, current) + @#{plural_name}.each do |#{name}| + #{name}.to_rss(rss, current) + end + end + + class #{klass_name}Base + include Base + + attr_accessor :value + add_need_initialize_variable("value") + alias_method(:content, :value) + alias_method(:content=, :value=) + + def have_required_values? + @value + end + end + end + EOC + end + + def self.install_dublin_core(klass) + ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name| + plural_name ||= "#{name}s" + klass_name = Utils.to_class_name(name) + plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}" + full_klass_name = "DublinCore#{klass_name}" + klass.module_eval(<<-EOC, *Utils.get_file_and_line_from_caller(1)) + class #{plural_klass_name} < #{plural_klass_name}Base + class #{klass_name} < #{klass_name}Base + def to_rss(rss, current) + if value and current.respond_to?(:dc_#{name}) + new_item = current.class::#{full_klass_name}.new(value) + current.dc_#{plural_name} << new_item + end end end + end EOC end end @@ -24,9 +102,9 @@ EOC class ChannelBase include DublinCoreModel - - undef_method(:dc_date) - undef_method(:dc_date=) + + remove_method(:dc_date) + remove_method(:dc_date=) alias_method(:dc_date, :date) alias_method(:dc_date=, :date=) end @@ -36,12 +114,52 @@ EOC class ItemBase include DublinCoreModel - undef_method(:dc_date) - undef_method(:dc_date=) + remove_method(:dc_date) + remove_method(:dc_date=) alias_method(:dc_date, :date) alias_method(:dc_date=, :date=) end end class TextinputBase; include DublinCoreModel; end + + class RSS10 + class Channel + DublinCoreModel.install_dublin_core(self) + end + + class Image + DublinCoreModel.install_dublin_core(self) + end + + class Items + class Item + DublinCoreModel.install_dublin_core(self) + end + end + + class Textinput + DublinCoreModel.install_dublin_core(self) + end + end + + class RSS09 + class Channel + DublinCoreModel.install_dublin_core(self) + end + + class Image + DublinCoreModel.install_dublin_core(self) + end + + class Items + class Item + DublinCoreModel.install_dublin_core(self) + end + end + + class Textinput + DublinCoreModel.install_dublin_core(self) + end + end end end diff --git a/lib/rss/maker/image.rb b/lib/rss/maker/image.rb index 98d59f733c..9e3772f09c 100644 --- a/lib/rss/maker/image.rb +++ b/lib/rss/maker/image.rb @@ -30,8 +30,10 @@ EOC include Maker::DublinCoreModel attr_accessor :about, :resource, :image_width, :image_height - add_need_initialize_variable(:about, :resource) - add_need_initialize_variable(:image_width, :image_height) + add_need_initialize_variable("about") + add_need_initialize_variable("resource") + add_need_initialize_variable("image_width") + add_need_initialize_variable("image_height") alias width= image_width= alias width image_width alias height= image_height= @@ -69,7 +71,8 @@ EOC include Maker::DublinCoreModel attr_accessor :about, :image_size - add_need_initialize_variable(:about, :image_size) + add_need_initialize_variable("about") + add_need_initialize_variable("image_size") alias size image_size alias size= image_size= @@ -89,10 +92,12 @@ EOC class Items class Item class ImageItem < ImageItemBase + DublinCoreModel.install_dublin_core(self) def to_rss(rss, current) if @about item = ::RSS::ImageItemModel::Item.new(@about, @resource) setup_values(item) + setup_other_elements(item) current.image_item = item end end @@ -102,11 +107,13 @@ EOC class Channel class ImageFavicon < ImageFaviconBase + DublinCoreModel.install_dublin_core(self) def to_rss(rss, current) if @about and @image_size args = [@about, @image_size] favicon = ::RSS::ImageFaviconModel::Favicon.new(*args) setup_values(favicon) + setup_other_elements(favicon) current.image_favicon = favicon end end @@ -118,6 +125,7 @@ EOC class Items class Item class ImageItem < ImageItemBase + DublinCoreModel.install_dublin_core(self) def to_rss(*args) end end @@ -126,6 +134,7 @@ EOC class Channel class ImageFavicon < ImageFaviconBase + DublinCoreModel.install_dublin_core(self) def to_rss(*args) end end diff --git a/lib/rss/maker/trackback.rb b/lib/rss/maker/trackback.rb index ef78bf2f20..3df21924dd 100644 --- a/lib/rss/maker/trackback.rb +++ b/lib/rss/maker/trackback.rb @@ -56,7 +56,7 @@ module RSS include Base attr_accessor :value - add_need_initialize_variable(:value) + add_need_initialize_variable("value") alias_method(:resource, :value) alias_method(:resource=, :value=) diff --git a/lib/rss/parser.rb b/lib/rss/parser.rb index fb6c55fb72..7e93c62f61 100644 --- a/lib/rss/parser.rb +++ b/lib/rss/parser.rb @@ -123,26 +123,18 @@ module RSS class << self - @@setter = {} + @@setters = {} @@registered_uris = {} + @@class_names = {} def install_setter(uri, tag_name, setter) - @@setter[uri] = {} unless @@setter.has_key?(uri) - @@setter[uri][tag_name] = setter - end - - def register_uri(name, uri) - @@registered_uris[name] = {} unless @@registered_uris.has_key?(name) - @@registered_uris[name][uri] = nil - end - - def uri_registered?(name, uri) - @@registered_uris[name].has_key?(uri) + @@setters[uri] ||= {} + @@setters[uri][tag_name] = setter end def setter(uri, tag_name) begin - @@setter[uri][tag_name] + @@setters[uri][tag_name] rescue NameError nil end @@ -150,13 +142,35 @@ module RSS def available_tags(uri) begin - @@setter[uri].keys + @@setters[uri].keys rescue NameError [] end end - - def install_get_text_element(name, uri, setter) + + def register_uri(uri, name) + @@registered_uris[name] ||= {} + @@registered_uris[name][uri] = nil + end + + def uri_registered?(uri, name) + @@registered_uris[name].has_key?(uri) + end + + def install_class_name(uri, tag_name, class_name) + @@class_names[uri] ||= {} + @@class_names[uri][tag_name] = class_name + end + + def class_name(uri, tag_name) + begin + @@class_names[uri][tag_name] + rescue NameError + tag_name[0,1].upcase + tag_name[1..-1] + end + end + + def install_get_text_element(uri, name, setter) install_setter(uri, name, setter) def_get_text_element(uri, name, *get_file_and_line_from_caller(1)) end @@ -164,12 +178,12 @@ module RSS private def def_get_text_element(uri, name, file, line) - register_uri(name, uri) + register_uri(uri, name) unless private_instance_methods(false).include?("start_#{name}") module_eval(<<-EOT, file, line) def start_#{name}(name, prefix, attrs, ns) uri = ns[prefix] - if self.class.uri_registered?(#{name.inspect}, uri) + if self.class.uri_registered?(uri, #{name.inspect}) if @do_validate tags = self.class.available_tags(uri) unless tags.include?(name) @@ -275,13 +289,11 @@ module RSS end def start_else_element(local, prefix, attrs, ns) - class_name = local[0,1].upcase << local[1..-1] + class_name = self.class.class_name(ns[prefix], local) current_class = @last_element.class -# begin if current_class.constants.include?(class_name) next_class = current_class.const_get(class_name) start_have_something_element(local, prefix, attrs, ns, next_class) -# rescue NameError else if @ignore_unknown_element @proc_stack.push(nil) diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index 848fbd7b79..f9740a1c33 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -51,6 +51,14 @@ module Enumerable end end +class Hash + unless instance_methods.include?("merge") + def merge(other) + dup.update(other) + end + end +end + require "English" require "rss/utils" require "rss/converter" @@ -222,38 +230,7 @@ EOC # accessor convert_attr_reader name - module_eval(<<-EOC, *get_file_and_line_from_caller(2)) - def #{name}=(new_value) - if new_value.kind_of?(Time) - @#{name} = new_value - else - if @do_validate - begin - @#{name} = Time.send('#{type}', new_value) - rescue ArgumentError - raise NotAvailableValueError.new('#{disp_name}', new_value) - end - else - @#{name} = nil - if /\\A\\s*\\z/ !~ new_value.to_s - begin - @#{name} = Time.parse(new_value) - rescue ArgumentError - end - end - end - end - - # Is it need? - if @#{name} - class << @#{name} - undef_method(:to_s) - alias_method(:to_s, :#{type}) - end - end - - end -EOC + date_writer(name, type, disp_name) install_element(name) do |n, elem_name| <<-EOC @@ -301,6 +278,41 @@ EOC end end + def date_writer(name, type, disp_name=name) + module_eval(<<-EOC, *get_file_and_line_from_caller(2)) + def #{name}=(new_value) + if new_value.nil? or new_value.kind_of?(Time) + @#{name} = new_value + else + if @do_validate + begin + @#{name} = Time.send('#{type}', new_value) + rescue ArgumentError + raise NotAvailableValueError.new('#{disp_name}', new_value) + end + else + @#{name} = nil + if /\\A\\s*\\z/ !~ new_value.to_s + begin + @#{name} = Time.parse(new_value) + rescue ArgumentError + end + end + end + end + + # Is it need? + if @#{name} + class << @#{name} + undef_method(:to_s) + alias_method(:to_s, :#{type}) + end + end + + end +EOC + end + def def_children_accessor(accessor_name, plural_name) module_eval(<<-EOC, *get_file_and_line_from_caller(2)) def #{plural_name} @@ -660,8 +672,9 @@ EOC klass = next_element.class prefix = "" prefix << "#{klass.required_prefix}_" if klass.required_prefix - if self.class.plural_forms.has_key?(tag_name) - ary = __send__("#{prefix}#{self.class.plural_forms[tag_name]}") + key = "#{prefix}#{tag_name}" + if self.class.plural_forms.has_key?(key) + ary = __send__("#{self.class.plural_forms[key]}") ary << next_element else __send__("#{prefix}#{tag_name}=", next_element) diff --git a/lib/rss/syndication.rb b/lib/rss/syndication.rb index c274ff8c82..56976085a1 100644 --- a/lib/rss/syndication.rb +++ b/lib/rss/syndication.rb @@ -17,12 +17,12 @@ module RSS super klass.module_eval(<<-EOC, *get_file_and_line_from_caller(1)) - %w(updatePeriod updateFrequency).each do |x| - install_text_element("\#{SY_PREFIX}_\#{x}") + %w(updatePeriod updateFrequency).each do |name| + install_text_element("\#{SY_PREFIX}_\#{name}") end - %w(updateBase).each do |x| - install_date_element("\#{SY_PREFIX}_\#{x}", 'w3cdtf', x) + %w(updateBase).each do |name| + install_date_element("\#{SY_PREFIX}_\#{name}", 'w3cdtf', name) end alias_method(:_sy_updatePeriod=, :sy_updatePeriod=) @@ -42,8 +42,8 @@ module RSS def sy_validate(tags) counter = {} - ELEMENTS.each do |x| - counter[x] = 0 + ELEMENTS.each do |name| + counter[name] = 0 end tags.each do |tag| @@ -78,8 +78,9 @@ module RSS prefix_size = SY_PREFIX.size + 1 SyndicationModel::ELEMENTS.uniq! - SyndicationModel::ELEMENTS.each do |x| - BaseListener.install_get_text_element(x[prefix_size..-1], SY_URI, "#{x}=") + SyndicationModel::ELEMENTS.each do |full_name| + name = full_name[prefix_size..-1] + BaseListener.install_get_text_element(SY_URI, name, "#{full_name}=") end end diff --git a/lib/rss/taxonomy.rb b/lib/rss/taxonomy.rb index bec6ea3c17..f70d475a11 100644 --- a/lib/rss/taxonomy.rb +++ b/lib/rss/taxonomy.rb @@ -11,17 +11,17 @@ module RSS TAXO_ELEMENTS = [] - %w(link).each do |x| - if const_defined? :Listener - Listener.install_get_text_element(x, TAXO_NS, "#{TAXO_PREFIX}_#{x}=") - end - TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{x}" + %w(link).each do |name| + full_name = "#{TAXO_PREFIX}_#{name}" + BaseListener.install_get_text_element(TAXO_NS, name, "#{full_name}=") + TAXO_ELEMENTS << "#{TAXO_PREFIX}_#{name}" end module TaxonomyModel attr_writer(*%w(title description creator subject publisher contributor date format identifier source - language relation coverage rights).collect{|x| "#{TAXO_PREFIX}_#{x}"}) + language relation coverage rights + ).collect{|name| "#{TAXO_PREFIX}_#{name}"}) end class Channel; extend TaxonomyModel; end diff --git a/lib/rss/trackback.rb b/lib/rss/trackback.rb index 439bdf3843..973a1035d1 100644 --- a/lib/rss/trackback.rb +++ b/lib/rss/trackback.rb @@ -11,18 +11,10 @@ module RSS module TrackBackUtils private - def new_with_value_if_need(klass, value) - if value.is_a?(klass) - value - else - klass.new(value) - end - end - def trackback_validate(tags) counter = {} - %w(ping about).each do |x| - counter["#{TRACKBACK_PREFIX}_#{x}"] = 0 + %w(ping about).each do |name| + counter["#{TRACKBACK_PREFIX}_#{name}"] = 0 end tags.each do |tag| @@ -48,9 +40,9 @@ module RSS unless klass.class == Module klass.__send__(:include, TrackBackUtils) - %w(ping).each do |x| - var_name = "#{TRACKBACK_PREFIX}_#{x}" - klass_name = x.capitalize + %w(ping).each do |name| + var_name = "#{TRACKBACK_PREFIX}_#{name}" + klass_name = name.capitalize klass.install_have_child_element(var_name) klass.module_eval(<<-EOC, __FILE__, __LINE__) remove_method :#{var_name} @@ -60,7 +52,7 @@ module RSS remove_method :#{var_name}= def #{var_name}=(value) - @#{var_name} = new_with_value_if_need(#{klass_name}, value) + @#{var_name} = Utils.new_with_value_if_need(#{klass_name}, value) end EOC end @@ -88,16 +80,16 @@ module RSS remove_method :set_#{var_name} def #{var_name}=(*args) if args.size == 1 - item = new_with_value_if_need(#{klass_name}, args[0]) + item = Utils.new_with_value_if_need(#{klass_name}, args[0]) @#{var_name}.push(item) else new_val = args.last if new_val.is_a?(Array) new_val = new_value.collect do |val| - new_with_value_if_need(#{klass_name}, val) + Utils.new_with_value_if_need(#{klass_name}, val) end else - new_val = new_with_value_if_need(#{klass_name}, new_val) + new_val = Utils.new_with_value_if_need(#{klass_name}, new_val) end @#{var_name}.send("[]=", *(args[0..-2] + [new_val])) end diff --git a/lib/rss/utils.rb b/lib/rss/utils.rb index 19f27183eb..0286becf00 100644 --- a/lib/rss/utils.rb +++ b/lib/rss/utils.rb @@ -19,6 +19,13 @@ module RSS end alias h html_escape + def new_with_value_if_need(klass, value) + if value.is_a?(klass) + value + else + klass.new(value) + end + end end end diff --git a/sample/rss/tdiary_plugin/rss-recent.rb b/sample/rss/tdiary_plugin/rss-recent.rb index f0943838fc..261ca872b7 100644 --- a/sample/rss/tdiary_plugin/rss-recent.rb +++ b/sample/rss/tdiary_plugin/rss-recent.rb @@ -1,12 +1,16 @@ # -*- indent-tabs-mode: t -*- # rss-recent.rb: RSS recent plugin # +# options: +# @options['rss-recent.use-image-link'] : use image as link +# instead of text if available. +# # rss_recnet: show recnet list from RSS # parameters (default): # url: URL of RSS # max: max of list itmes(5) # cache_time: cache time(second) of RSS(60*60) -# +# # # Copyright (c) 2003-2005 Kouhei Sutou # Distributed under the GPL @@ -16,7 +20,7 @@ require "rss/rss" RSS_RECENT_FIELD_SEPARATOR = "\0" RSS_RECENT_ENTRY_SEPARATOR = "\1" -RSS_RECENT_VERSION = "0.0.5" +RSS_RECENT_VERSION = "0.0.6" RSS_RECENT_HTTP_HEADER = { "User-Agent" => "tDiary RSS recent plugin version #{RSS_RECENT_VERSION}. " << "Using RSS parser version is #{::RSS::VERSION}.", @@ -36,8 +40,8 @@ def rss_recent(url, max=5, cache_time=3600) site_info, *infos = rss_recent_read_from_cache(cache_file) if site_info - title, url, time = site_info - content = rss_recent_entry_to_html(title, url, time) + title, url, time, image = site_info + content = rss_recent_entry_to_html(title, url, time, image) rv << "
\n" rv << "#{content}\n" rv << "
\n" @@ -47,12 +51,12 @@ def rss_recent(url, max=5, cache_time=3600) rv << "
    \n" if have_entry i = 0 - infos.each do |title, url, time| + infos.each do |title, url, time, image| break if i >= max next if title.nil? rv << '
  • ' rv << %Q[] - rv << rss_recent_entry_to_html(title, url, time) + rv << rss_recent_entry_to_html(title, url, time, image) rv << %Q[] rv << "
  • \n" i += 1 @@ -81,6 +85,10 @@ def rss_recent_cache_rss(url, cache_file, cache_time) require 'rss/1.0' require 'rss/2.0' require 'rss/dublincore' + begin + require 'rss/image' + rescue LoadError + end begin uri = URI.parse(url) @@ -107,11 +115,17 @@ def rss_recent_cache_rss(url, cache_file, cache_time) rss.channel.title, rss.channel.link, rss.channel.dc_date, + rss.image && rss.image.url, ] ] rss.items.each do |item| rss_recent_pubDate_to_dc_date(item) - rss_infos << [item.title, item.link, item.dc_date] + if item.respond_to?(:image_item) and item.image_item + image = item.image_item.about + else + image = nil + end + rss_infos << [item.title, item.link, item.dc_date, image] end rss_recent_write_to_cache(cache_file, rss_infos) @@ -174,11 +188,12 @@ def rss_recent_read_from_cache(cache_file) infos << info.split(RSS_RECENT_FIELD_SEPARATOR) end end - infos.collect do |title, url, time| + infos.collect do |title, url, time, image| [ rss_recent_convert(title), rss_recent_convert(url), rss_recent_convert(time) {|t| Time.parse(t)}, + rss_recent_convert(image), ] end end @@ -195,14 +210,21 @@ def rss_recent_convert(str) end end -def rss_recent_entry_to_html(title, url, time) +def rss_recent_entry_to_html(title, url, time, image=nil) rv = "" unless url.nil? rv << %Q[] end - rv << CGI::escapeHTML(title) + if image and @options['rss-recent.use-image-link'] + rv << %Q[site image\n] + else + rv << CGI::escapeHTML(title) + end rv << '' unless url.nil? rv << "(#{rss_recent_modified(time)})" rv diff --git a/test/rss/rss-assertions.rb b/test/rss/rss-assertions.rb index 569e9621d2..2636062519 100644 --- a/test/rss/rss-assertions.rb +++ b/test/rss/rss-assertions.rb @@ -394,6 +394,16 @@ module RSS end end + def assert_multiple_dublin_core(elems, target) + _wrap_assertion do + elems.each do |name, values, plural| + plural ||= "#{name}s" + actual = target.__send__("dc_#{plural}").collect{|x| x.value} + assert_equal(values, actual) + end + end + end + def assert_syndication(elems, target) _wrap_assertion do elems.each do |name, value| diff --git a/test/rss/test_dublincore.rb b/test/rss/test_dublincore.rb index e1ee929afb..5ad9311fb5 100644 --- a/test/rss/test_dublincore.rb +++ b/test/rss/test_dublincore.rb @@ -54,26 +54,35 @@ EOR end def test_parser - assert_nothing_raised do Parser.parse(@rss_source) end @elems.each do |tag, value| - assert_too_much_tag(tag.to_s, "channel") do - Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) + rss = nil + assert_nothing_raised do + rss = Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) #{make_channel(("<" + @prefix + ":" + tag.to_s + ">" + value.to_s + "") * 2)} #{make_item} EOR end + plural_reader = "dc_#{tag}" + (tag == :rights ? "es" : "s") + values = rss.channel.__send__(plural_reader).collect do |x| + val = x.value + if val.kind_of?(String) + CGI.escapeHTML(val) + else + val + end + end + assert_equal([value, value], values) end end - - def test_accessor - + + def test_singular_accessor new_value = "hoge" @elems.each do |name, value| @@ -101,15 +110,58 @@ EOR end end end + end + def test_plural_accessor + new_value = "hoge" + + @elems.each do |name, value| + @parents.each do |parent| + parsed_value = @rss.send(parent).send("dc_#{name}") + if parsed_value.kind_of?(String) + parsed_value = CGI.escapeHTML(parsed_value) + end + assert_equal(value, parsed_value) + + plural_reader = "dc_#{name}" + (name == :rights ? "es" : "s") + klass_name = "DublinCore#{Utils.to_class_name(name.to_s)}" + klass = DublinCoreModel.const_get(klass_name) + if name == :date + t = Time.iso8601("2003-01-01T02:30:23+09:00") + class << t + alias_method(:to_s, :iso8601) + end + elems = @rss.send(parent).send(plural_reader) + elems << klass.new(t.iso8601) + values = @rss.send(parent).send(plural_reader).collect{|x| x.value} + assert_equal([@rss.send(parent).send("dc_#{name}"), t], + values) + else + elems = @rss.send(parent).send(plural_reader) + elems << klass.new(new_value) + values = @rss.send(parent).send(plural_reader).collect{|x| x.value} + assert_equal([@rss.send(parent).send("dc_#{name}"), new_value], + values) + end + end + end end def test_to_s - @elems.each do |name, value| excepted = "<#{@prefix}:#{name}>#{value}" @parents.each do |parent| - assert_equal(excepted, @rss.send(parent).send("dc_#{name}_element")) + assert_equal(excepted, @rss.send(parent).send("dc_#{name}_elements")) + end + + excepted = Array.new(2, excepted).join("\n") + @parents.each do |parent| + reader = "dc_#{name}" + (name == :rights ? "es" : "s") + elems = @rss.send(parent).send(reader) + klass_name = "DublinCore#{Utils.to_class_name(name.to_s)}" + klass = DublinCoreModel.const_get(klass_name) + elems << klass.new(@rss.send(parent).send("dc_#{name}")) + assert_equal(excepted, @rss.send(parent).send("dc_#{name}_elements")) end end @@ -117,12 +169,12 @@ EOR if @parents.include?(parent.name) parent.each_element do |elem| if elem.namespace == @uri - assert_equal(CGI.escapeHTML(elem.text), @elems[elem.name.intern].to_s) + assert_equal(CGI.escapeHTML(elem.text), + @elems[elem.name.intern].to_s) end end end end - end end diff --git a/test/rss/test_maker_dc.rb b/test/rss/test_maker_dc.rb index 0a4a4d42cf..9f568ed441 100644 --- a/test/rss/test_maker_dc.rb +++ b/test/rss/test_maker_dc.rb @@ -56,16 +56,86 @@ module RSS assert_dublin_core(@elements, rss.textinput) end + def test_rss10_multiple + elems = [] + @elements.each do |name, value| + plural = name.to_s + (name == :rights ? "es" : "s") + values = [value] + if name == :date + values << value + 60 + else + values << value * 2 + end + elems << [name, values, plural] + end + + rss = RSS::Maker.make("1.0") do |maker| + setup_dummy_channel(maker) + set_multiple_elements(maker.channel, elems) + + setup_dummy_image(maker) + set_multiple_elements(maker.image, elems) + + setup_dummy_item(maker) + item = maker.items.last + elems.each do |name, values, plural| + dc_elems = item.__send__("dc_#{plural}") + values.each do |value| + elem = dc_elems.__send__("new_#{name}") + elem.value = value + end + end + + setup_dummy_textinput(maker) + set_multiple_elements(maker.textinput, elems) + end + assert_multiple_dublin_core(elems, rss.channel) + assert_multiple_dublin_core(elems, rss.image) + assert_multiple_dublin_core(elems, rss.items.last) + assert_multiple_dublin_core(elems, rss.textinput) + end + + def test_date + t1 = Time.iso8601("2000-01-01T12:00:05+00:00") + t2 = Time.iso8601("2005-01-01T12:00:05+00:00") + + rss = RSS::Maker.make("1.0") do |maker| + setup_dummy_channel(maker) + maker.channel.date = t1 + date = maker.channel.dc_dates.new_date + date.value = t2 + + setup_dummy_item(maker) + item = maker.items.last + item.date = t2 + date = item.dc_dates.new_date + date.value = t1 + end + assert_equal([t1, t2], rss.channel.dc_dates.collect{|x| x.value}) + assert_equal([t2, t1], rss.items.last.dc_dates.collect{|x| x.value}) + end + private def accessor_name(name) "dc_#{name}" end - def set_elements(target) - @elements.each do |name, value| + def set_elements(target, elems=@elements) + elems.each do |name, value| target.__send__("#{accessor_name(name)}=", value) end end + def set_multiple_elements(target, elems) + elems.each do |name, values, plural| + plural ||= "#{name}s" + dc_elems = target.__send__("dc_#{plural}") + values.each do |value| + new_dc_elem = dc_elems.__send__("new_#{name}") + new_dc_elem.value = value + end + end + end + end end diff --git a/test/rss/test_setup_maker_1.0.rb b/test/rss/test_setup_maker_1.0.rb index 52557486f4..395e9b1ec9 100644 --- a/test/rss/test_setup_maker_1.0.rb +++ b/test/rss/test_setup_maker_1.0.rb @@ -84,11 +84,11 @@ module RSS assert_nil(channel.textinput) @dc_elems.each do |var, value| - assert_equal(channel.__send__("dc_#{var}"), value) + assert_equal(value, channel.__send__("dc_#{var}")) end @sy_elems.each do |var, value| - assert_equal(channel.__send__("sy_#{var}"), value) + assert_equal(value, channel.__send__("sy_#{var}")) end end -- cgit v1.2.3