From 6b988d9fab9c4c3b713fbcfee3c96633350a1731 Mon Sep 17 00:00:00 2001 From: kou Date: Sat, 16 Oct 2004 04:39:58 +0000 Subject: * lib/rss: supported prety print. * test/rss/test_1.0.rb: added test for calculating default indent size. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@7047 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/rss/rss.rb | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'lib/rss/rss.rb') diff --git a/lib/rss/rss.rb b/lib/rss/rss.rb index 13eaf7c505..24ba0eb170 100644 --- a/lib/rss/rss.rb +++ b/lib/rss/rss.rb @@ -154,7 +154,7 @@ module RSS install_element(name) do |n, elem_name| <<-EOC if @#{n} - "\#{indent}\#{@#{n}.to_s(convert)}" + "\#{@#{n}.to_s(convert, indent)}" else '' end @@ -171,7 +171,7 @@ EOC <<-EOC rv = '' @#{n}.each do |x| - rv << "\#{indent}\#{x.to_s(convert)}" + rv << "\#{x.to_s(convert, indent)}" end rv EOC @@ -320,14 +320,16 @@ EOC extend BaseModel include Utils + INDENT = " " + class << self def inherited(klass) klass.module_eval(<<-EOC) public - TAG_NAME = name.split('::').last.downcase - + @tag_name = name.split(/::/).last.downcase + @indent_size = name.split(/::/).size - 2 @@must_call_validators = {} @@ -415,6 +417,14 @@ EOC self::NSPOOL[prefix] = uri end + def tag_name + @tag_name + end + + def indent_size + @indent_size + end + end attr_accessor :do_validate @@ -426,7 +436,7 @@ EOC end def tag_name - self.class::TAG_NAME + self.class.tag_name end def converter=(converter) @@ -498,7 +508,7 @@ EOC def validate_attribute _attrs.each do |a_name, required| if required and send(a_name).nil? - raise MissingAttributeError.new(self.class::TAG_NAME, a_name) + raise MissingAttributeError.new(self.class.tag_name, a_name) end end end @@ -615,6 +625,15 @@ EOC rv end + private + def calc_indent + INDENT * (self.class.indent_size) + end + + def remove_empty_newline(string) + string.gsub(/^\s*$(?:\r?\n?)/, '') + end + end module RootElementMixin @@ -646,11 +665,11 @@ EOC rv end - def ns_declaration + def ns_declaration(indent) rv = '' self.class::NSPOOL.each do |prefix, uri| prefix = ":#{prefix}" unless prefix.empty? - rv << %Q|\n\txmlns#{prefix}="#{html_escape(uri)}"| + rv << %Q|\n#{indent}xmlns#{prefix}="#{html_escape(uri)}"| end rv end -- cgit v1.2.3