summaryrefslogtreecommitdiff
path: root/lib/rss
diff options
context:
space:
mode:
authorkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-17 01:02:08 +0000
committerkou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-17 01:02:08 +0000
commit448dce34a627cf46d844c2e7c628a7af8174fcb4 (patch)
treec3ad7c6fb89080c46efe3c8b7df70a499f5a4940 /lib/rss
parent2da88324a206217714817b0dc90c80861ba757b4 (diff)
* lib/rss, test/rss: backported from trunk. (2005-11-16 - now)
* lib/rss/rss.rb: improved type conversion. * lib/rss/1.0.rb: ditto. * lib/rss/0.9.rb: ditto. * lib/rss/2.0.rb: ditto. * lib/rss/image.rb: ditto. * lib/rss/syndication.rb: ditto. * test/rss/test_2.0.rb: added type conversion tests. * test/rss/test_accessor.rb: ditto. * test/rss/test_to_s.rb: ditto. * test/rss/test_syndication.rb: ditto. * test/rss/test_setup_maker_2.0.rb: ditto. * test/rss/test_setup_maker_1.0.rb: ditto. * test/rss/test_setup_maker_0.9.rb: ditto. * test/rss/test_maker_sy.rb: ditto. * test/rss/test_maker_image.rb: ditto. * test/rss/test_maker_2.0.rb: ditto. * test/rss/test_maker_0.9.rb: ditto. * test/rss/test_image.rb: ditto. * test/rss/test_maker_1.0.rb: use assert instead of assert_equal. * test/rss/rss-assertions.rb: improved type conversion assertions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10298 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rss')
-rw-r--r--lib/rss/0.9.rb78
-rw-r--r--lib/rss/1.0.rb14
-rw-r--r--lib/rss/2.0.rb29
-rw-r--r--lib/rss/image.rb53
-rw-r--r--lib/rss/rss.rb102
-rw-r--r--lib/rss/syndication.rb23
6 files changed, 185 insertions, 114 deletions
diff --git a/lib/rss/0.9.rb b/lib/rss/0.9.rb
index a4b2ba9b8f..f0060cbad5 100644
--- a/lib/rss/0.9.rb
+++ b/lib/rss/0.9.rb
@@ -269,7 +269,7 @@ module RSS
def initialize(content=nil)
super()
- @content = content
+ self.content = content
end
end
@@ -310,18 +310,12 @@ module RSS
class Hour < Element
include RSS09
- content_setup
+ content_setup(:integer)
def initialize(content=nil)
super()
- @content = content
- end
-
- remove_method :content=
- def content=(value)
- @content = value.to_i
+ self.content = content
end
-
end
end
@@ -334,20 +328,24 @@ module RSS
install_text_element(name)
install_model(name, nil)
end
- %w(width height description).each do |name|
- install_text_element(name)
+ [
+ ["width", :integer],
+ ["height", :integer],
+ ["description"],
+ ].each do |name, type|
+ install_text_element(name, type)
install_model(name, "?")
end
def initialize(url=nil, title=nil, link=nil, width=nil, height=nil,
description=nil)
super()
- @url = url
- @title = title
- @link = link
- @width = width
- @height = height
- @description = description
+ self.url = url
+ self.title = title
+ self.link = link
+ self.width = width
+ self.height = height
+ self.description = description
end
def to_s(need_convert=true, indent=calc_indent)
@@ -386,21 +384,21 @@ module RSS
[
["domain", nil, true],
- ["port", nil, true],
+ ["port", nil, true, :integer],
["path", nil, true],
["registerProcedure", nil, true],
- ["protocol", nil ,true],
- ].each do |name, uri, required|
- install_get_attribute(name, uri, required)
+ ["protocol", nil, true],
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
end
def initialize(domain=nil, port=nil, path=nil, rp=nil, protocol=nil)
super()
- @domain = domain
- @port = port
- @path = path
- @registerProcedure = rp
- @protocol = protocol
+ self.domain = domain
+ self.port = port
+ self.path = path
+ self.registerProcedure = rp
+ self.protocol = protocol
end
def to_s(need_convert=true, indent=calc_indent)
@@ -511,8 +509,8 @@ module RSS
def initialize(url=nil, content=nil)
super()
- @url = url
- @content = content
+ self.url = url
+ self.content = content
end
private
@@ -543,17 +541,17 @@ module RSS
[
["url", nil, true],
- ["length", nil, true],
+ ["length", nil, true, :integer],
["type", nil, true],
- ].each do |name, uri, required|
- install_get_attribute(name, uri, required)
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
end
def initialize(url=nil, length=nil, type=nil)
super()
- @url = url
- @length = length
- @type = type
+ self.url = url
+ self.length = length
+ self.type = type
end
def to_s(need_convert=true, indent=calc_indent)
@@ -596,8 +594,8 @@ module RSS
def initialize(domain=nil, content=nil)
super()
- @domain = domain
- @content = content
+ self.domain = domain
+ self.content = content
end
private
@@ -631,10 +629,10 @@ module RSS
def initialize(title=nil, description=nil, name=nil, link=nil)
super()
- @title = title
- @description = description
- @name = name
- @link = link
+ self.title = title
+ self.description = description
+ self.name = name
+ self.link = link
end
def to_s(need_convert=true, indent=calc_indent)
diff --git a/lib/rss/1.0.rb b/lib/rss/1.0.rb
index 6603d525d3..36d6f5df87 100644
--- a/lib/rss/1.0.rb
+++ b/lib/rss/1.0.rb
@@ -113,7 +113,7 @@ module RSS
def initialize(resource=nil)
super()
- @resource = resource
+ self.resource = resource
end
def full_name
@@ -293,7 +293,7 @@ module RSS
def initialize(about=nil)
super()
- @about = about
+ self.about = about
end
def to_s(need_convert=true, indent=calc_indent)
@@ -364,7 +364,7 @@ module RSS
def initialize(resource=nil)
super()
- @resource = resource
+ self.resource = resource
end
def to_s(need_convert=true, indent=calc_indent)
@@ -401,7 +401,7 @@ module RSS
def initialize(resource=nil)
super()
- @resource = resource
+ self.resource = resource
end
def to_s(need_convert=true, indent=calc_indent)
@@ -509,7 +509,7 @@ module RSS
def initialize(about=nil)
super()
- @about = about
+ self.about = about
end
def to_s(need_convert=true, indent=calc_indent)
@@ -579,7 +579,7 @@ module RSS
def initialize(about=nil)
super()
- @about = about
+ self.about = about
end
def to_s(need_convert=true, indent=calc_indent)
@@ -654,7 +654,7 @@ module RSS
def initialize(about=nil)
super()
- @about = about
+ self.about = about
end
def to_s(need_convert=true, indent=calc_indent)
diff --git a/lib/rss/2.0.rb b/lib/rss/2.0.rb
index 66474ff76d..1c3c22ee70 100644
--- a/lib/rss/2.0.rb
+++ b/lib/rss/2.0.rb
@@ -6,16 +6,14 @@ module RSS
class Channel
- %w(generator ttl).each do |name|
- install_text_element(name)
+ [
+ ["generator"],
+ ["ttl", :integer],
+ ].each do |name, type|
+ install_text_element(name, type)
install_model(name, '?')
end
- remove_method :ttl=
- def ttl=(value)
- @ttl = value.to_i
- end
-
[
%w(category categories),
].each do |name, plural_name|
@@ -126,17 +124,24 @@ EOT
include RSS09
[
- ["isPermaLink", nil, false]
- ].each do |name, uri, required|
- install_get_attribute(name, uri, required)
+ ["isPermaLink", nil, false, :boolean]
+ ].each do |name, uri, required, type|
+ install_get_attribute(name, uri, required, type)
end
content_setup
def initialize(isPermaLink=nil, content=nil)
super()
- @isPermaLink = isPermaLink
- @content = content
+ self.isPermaLink = isPermaLink
+ self.content = content
+ end
+
+ alias_method :_PermaLink?, :PermaLink?
+ private :_PermaLink?
+ def PermaLink?
+ perma = _PermaLink?
+ perma or perma.nil?
end
private
diff --git a/lib/rss/image.rb b/lib/rss/image.rb
index 818edd4f2c..b81b72586f 100644
--- a/lib/rss/image.rb
+++ b/lib/rss/image.rb
@@ -63,14 +63,20 @@ module RSS
%w(width height).each do |tag|
full_name = "#{IMAGE_PREFIX}_#{tag}"
- install_text_element(full_name)
+ disp_name = "#{IMAGE_PREFIX}:#{tag}"
+ install_text_element(full_name, :integer, disp_name)
BaseListener.install_get_text_element(IMAGE_URI, tag, "#{full_name}=")
end
+ alias width= image_width=
+ alias width image_width
+ alias height= image_height=
+ alias height image_height
+
def initialize(about=nil, resource=nil)
super()
- @about = about
- @resource = resource
+ self.about = about
+ self.resource = resource
end
def full_name
@@ -87,29 +93,6 @@ module RSS
rv
end
- alias _image_width= image_width=
- def image_width=(new_value)
- if @do_validate
- self._image_width = Integer(new_value)
- else
- self._image_width = new_value.to_i
- end
- end
-
- alias _image_height= image_height=
- def image_height=(new_value)
- if @do_validate
- self._image_height = Integer(new_value)
- else
- self._image_height = new_value.to_i
- end
- end
-
- alias width= image_width=
- alias width image_width
- alias height= image_height=
- alias height image_height
-
private
def _tags
[
@@ -177,13 +160,27 @@ module RSS
install_get_attribute(name, uri, required)
end
+ AVAILABLE_SIZES = %w(small medium large)
+ alias_method :_size=, :size=
+ private :_size=
+ def size=(new_value)
+ if @do_validate and !new_value.nil?
+ new_value = new_value.strip
+ unless AVAILABLE_SIZES.include?(new_value)
+ attr_name = "#{IMAGE_PREFIX}:size"
+ raise NotAvailableValueError.new(full_name, new_value, attr_name)
+ end
+ end
+ __send__(:_size=, new_value)
+ end
+
alias image_size= size=
alias image_size size
def initialize(about=nil, size=nil)
super()
- @about = about
- @size = size
+ self.about = about
+ self.size = size
end
def full_name
diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb
index 17e56d5a7d..589478665c 100644
--- a/lib/rss/rss.rb
+++ b/lib/rss/rss.rb
@@ -117,10 +117,13 @@ module RSS
end
class NotAvailableValueError < InvalidRSSError
- attr_reader :tag, :value
- def initialize(tag, value)
- @tag, @value = tag, value
- super("value <#{value}> of tag <#{tag}> is not available.")
+ attr_reader :tag, :value, :attribute
+ def initialize(tag, value, attribute=nil)
+ @tag, @value, @attribute = tag, value, attribute
+ message = "value <#{value}> of "
+ message << "attribute <#{attribute}> of " if attribute
+ message << "tag <#{tag}> is not available."
+ super(message)
end
end
@@ -192,11 +195,11 @@ EOC
end
end
- def install_text_element(name)
+ def install_text_element(name, type=nil, disp_name=name)
self::ELEMENTS << name
add_need_initialize_variable(name)
- attr_writer name
+ def_corresponded_attr_writer name, type, disp_name
convert_attr_reader name
install_element(name) do |n, elem_name|
<<-EOC
@@ -306,6 +309,68 @@ EOC
EOC
end
+ def integer_writer(name, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.nil?
+ @#{name} = new_value
+ else
+ if @do_validate
+ begin
+ @#{name} = Integer(new_value)
+ rescue ArgumentError
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ else
+ @#{name} = new_value.to_i
+ end
+ end
+ end
+EOC
+ end
+
+ def positive_integer_writer(name, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.nil?
+ @#{name} = new_value
+ else
+ if @do_validate
+ begin
+ tmp = Integer(new_value)
+ raise ArgumentError if tmp <= 0
+ @#{name} = tmp
+ rescue ArgumentError
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ else
+ @#{name} = new_value.to_i
+ end
+ end
+ end
+EOC
+ end
+
+ def boolean_writer(name, disp_name=name)
+ module_eval(<<-EOC, *get_file_and_line_from_caller(2))
+ def #{name}=(new_value)
+ if new_value.nil?
+ @#{name} = new_value
+ else
+ if @do_validate and
+ ![true, false, "true", "false"].include?(new_value)
+ raise NotAvailableValueError.new('#{disp_name}', new_value)
+ end
+ if [true, false].include?(new_value)
+ @#{name} = new_value
+ else
+ @#{name} = new_value == "true"
+ 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}
@@ -437,14 +502,31 @@ EOC
end
end
- def self.install_get_attribute(name, uri, required=true)
- attr_writer name
+ def self.install_get_attribute(name, uri, required=true,
+ type=nil, disp_name=name)
+ def_corresponded_attr_writer name, type, disp_name
convert_attr_reader name
+ if type == :boolean and /^is/ =~ name
+ alias_method "\#{$POSTMATCH}?", name
+ end
GET_ATTRIBUTES << [name, uri, required]
end
- def self.content_setup
- attr_writer :content
+ def self.def_corresponded_attr_writer(name, type=nil, disp_name=name)
+ case type
+ when :integer
+ integer_writer name, disp_name
+ when :positive_integer
+ positive_integer_writer name, disp_name
+ when :boolean
+ boolean_writer name, disp_name
+ else
+ attr_writer name
+ end
+ end
+
+ def self.content_setup(type=nil)
+ def_corresponded_attr_writer "content", type
convert_attr_reader :content
def_content_only_to_s
@have_content = true
diff --git a/lib/rss/syndication.rb b/lib/rss/syndication.rb
index 56976085a1..8791ec24fc 100644
--- a/lib/rss/syndication.rb
+++ b/lib/rss/syndication.rb
@@ -17,8 +17,12 @@ module RSS
super
klass.module_eval(<<-EOC, *get_file_and_line_from_caller(1))
- %w(updatePeriod updateFrequency).each do |name|
- install_text_element("\#{SY_PREFIX}_\#{name}")
+ [
+ ["updatePeriod"],
+ ["updateFrequency", :positive_integer]
+ ].each do |name, type|
+ install_text_element("\#{SY_PREFIX}_\#{name}", type,
+ "\#{SY_PREFIX}:\#{name}")
end
%w(updateBase).each do |name|
@@ -31,12 +35,6 @@ module RSS
validate_sy_updatePeriod(new_value) if @do_validate
self._sy_updatePeriod = new_value
end
-
- alias_method(:_sy_updateFrequency=, :sy_updateFrequency=)
- def sy_updateFrequency=(new_value)
- validate_sy_updateFrequency(new_value) if @do_validate
- self._sy_updateFrequency = new_value.to_i
- end
EOC
end
@@ -61,15 +59,6 @@ module RSS
raise NotAvailableValueError.new("updatePeriod", value)
end
end
-
- SY_UPDATEFREQUENCY_AVAILABLE_RE = /\A\s*\+?\d+\s*\z/
- def validate_sy_updateFrequency(value)
- value = value.to_s.strip
- if SY_UPDATEFREQUENCY_AVAILABLE_RE !~ value
- raise NotAvailableValueError.new("updateFrequency", value)
- end
- end
-
end
class RDF