PageRenderTime 146ms CodeModel.GetById 137ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/rss/maker/dublincore.rb

http://github.com/agross/netopenspace
Ruby | 124 lines | 106 code | 14 blank | 4 comment | 2 complexity | d1e274f9977e846b91408e0f03973cf2 MD5 | raw file
  1require 'rss/dublincore'
  2require 'rss/maker/1.0'
  3
  4module RSS
  5  module Maker
  6    module DublinCoreModel
  7      def self.append_features(klass)
  8        super
  9
 10        ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
 11          plural_name ||= "#{name}s"
 12          full_name = "#{RSS::DC_PREFIX}_#{name}"
 13          full_plural_name = "#{RSS::DC_PREFIX}_#{plural_name}"
 14          klass_name = Utils.to_class_name(name)
 15          plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}"
 16          full_plural_klass_name = "self.class::#{plural_klass_name}"
 17          full_klass_name = "#{full_plural_klass_name}::#{klass_name}"
 18          klass.def_classed_elements(full_name, "value", plural_klass_name,
 19                                     full_plural_name, name)
 20          klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
 21            def new_#{full_name}(value=nil)
 22              _#{full_name} = #{full_plural_name}.new_#{name}
 23              _#{full_name}.value = value
 24              if block_given?
 25                yield _#{full_name}
 26              else
 27                _#{full_name}
 28              end
 29            end
 30          EOC
 31        end
 32
 33        klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
 34          # For backward compatibility
 35          alias #{DC_PREFIX}_rightses #{DC_PREFIX}_rights_list
 36        EOC
 37      end
 38
 39      ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
 40        plural_name ||= "#{name}s"
 41        full_name ||= "#{DC_PREFIX}_#{name}"
 42        full_plural_name ||= "#{DC_PREFIX}_#{plural_name}"
 43        klass_name = Utils.to_class_name(name)
 44        full_klass_name = "DublinCore#{klass_name}"
 45        plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}"
 46        module_eval(<<-EOC, __FILE__, __LINE__ + 1)
 47        class #{plural_klass_name}Base < Base
 48          def_array_element(#{name.dump}, #{full_plural_name.dump},
 49                            #{full_klass_name.dump})
 50
 51          class #{full_klass_name}Base < Base
 52            attr_accessor :value
 53            add_need_initialize_variable("value")
 54            alias_method(:content, :value)
 55            alias_method(:content=, :value=)
 56
 57            def have_required_values?
 58              @value
 59            end
 60
 61            def to_feed(feed, current)
 62              if value and current.respond_to?(:#{full_name})
 63                new_item = current.class::#{full_klass_name}.new(value)
 64                current.#{full_plural_name} << new_item
 65              end
 66            end
 67          end
 68          #{klass_name}Base = #{full_klass_name}Base
 69        end
 70        EOC
 71      end
 72
 73      def self.install_dublin_core(klass)
 74        ::RSS::DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
 75          plural_name ||= "#{name}s"
 76          klass_name = Utils.to_class_name(name)
 77          full_klass_name = "DublinCore#{klass_name}"
 78          plural_klass_name = "DublinCore#{Utils.to_class_name(plural_name)}"
 79          klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
 80          class #{plural_klass_name} < #{plural_klass_name}Base
 81            class #{full_klass_name} < #{full_klass_name}Base
 82            end
 83            #{klass_name} = #{full_klass_name}
 84          end
 85EOC
 86        end
 87      end
 88    end
 89
 90    class ChannelBase
 91      include DublinCoreModel
 92    end
 93    
 94    class ImageBase; include DublinCoreModel; end
 95    class ItemsBase
 96      class ItemBase
 97        include DublinCoreModel
 98      end
 99    end
100    class TextinputBase; include DublinCoreModel; end
101
102    makers.each do |maker|
103      maker.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
104        class Channel
105          DublinCoreModel.install_dublin_core(self)
106        end
107
108        class Image
109          DublinCoreModel.install_dublin_core(self)
110        end
111
112        class Items
113          class Item
114            DublinCoreModel.install_dublin_core(self)
115          end
116        end
117
118        class Textinput
119          DublinCoreModel.install_dublin_core(self)
120        end
121      EOC
122    end
123  end
124end