* lib/rss/dublincore.rb: supported multiple DublinCore items.

* 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/maker/base.rb: added default current_element implementation.

* lib/rss/maker/dublincore.rb: supported multiple DublinCore items.

* lib/rss/maker/image.rb: supproted new DublinCore API.

* 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 DublinCore items.

* test/test_setup_maker_1.0.rb: fixed swapped actual and expected
  values.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8252 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2005-04-05 06:42:03 +00:00
Родитель 5ddcd35cf1
Коммит d12dff187a
12 изменённых файлов: 442 добавлений и 102 удалений

Просмотреть файл

@ -1,3 +1,43 @@
Tue Apr 5 15:15:26 2005 Kouhei Sutou <kou@kono.cis.iwate-u.ac.jp>
* lib/rss/dublincore.rb: supported multiple DublinCore items.
* 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/maker/base.rb: added default current_element implementation.
* lib/rss/maker/dublincore.rb: supported multiple DublinCore items.
* lib/rss/maker/image.rb: supproted new DublinCore API.
* 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 DublinCore items.
* test/test_setup_maker_1.0.rb: fixed swapped actual and expected
values.
Mon Apr 4 23:17:52 2005 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/lib/tk.rb (TkComm#array2tk_list): accept enc-mode argument to

Просмотреть файл

@ -7,41 +7,135 @@ module RSS
RDF.install_ns(DC_PREFIX, DC_URI)
module DublinCoreModel
extend BaseModel
ELEMENTS = []
def self.append_features(klass)
module BaseDublinCoreModel
def append_features(klass)
super
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}")
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
%w(date).each do |x|
install_date_element("\#{DC_PREFIX}_\#{x}", 'w3cdtf', x)
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
def dc_validate(tags)
counter = {}
ELEMENTS.each do |x|
counter[x] = 0
end
module DublinCoreModel
extend BaseModel
extend BaseDublinCoreModel
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
}
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 |x, plural_name|
module_eval(<<-EOC, *get_file_and_line_from_caller(0))
class DublinCore#{Utils.to_class_name(x)} < Element
include RSS10
content_setup
class << self
def required_prefix
DC_PREFIX
end
def required_uri
DC_URI
end
end
@tag_name = #{x.dump}
alias_method(:value, :content)
alias_method(:value=, :content=)
def initialize(content=nil)
super()
self.content = content
end
def full_name
tag_name_with_prefix(DC_PREFIX)
end
def maker_target(target)
target.new_#{x}
end
def setup_maker_attributes(#{x})
#{x}.content = content
end
end
EOC
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
undef_method(:content=)
undef_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(DC_URI, x[prefix_size..-1], "#{x}=")
class_name = Utils.to_class_name(x)
BaseListener.install_class_name(DC_URI, x, "DublinCore#{class_name}")
end
DublinCoreModel::ELEMENTS.collect! {|x| "#{DC_PREFIX}_#{x}"}
end

Просмотреть файл

@ -81,6 +81,10 @@ module RSS
end
end
def current_element(rss)
rss
end
def setup_values(target)
set = false
if have_required_values?

Просмотреть файл

@ -7,14 +7,90 @@ 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)}"
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})
current.dc_#{name} = value
end
end
end
end
EOC
@ -43,5 +119,45 @@ EOC
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

Просмотреть файл

@ -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

Просмотреть файл

@ -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=)

Просмотреть файл

@ -125,6 +125,7 @@ module RSS
@@setters = {}
@@registered_uris = {}
@@class_names = {}
def install_setter(uri, tag_name, setter)
@@setters[uri] ||= {}
@ -156,6 +157,19 @@ module RSS
@@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))
@ -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)

Просмотреть файл

@ -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)

Просмотреть файл

@ -11,14 +11,6 @@ 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|
@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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 +
"</" + @prefix + ":" + tag.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}</#{@prefix}:#{name}>"
@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

Просмотреть файл

@ -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