PageRenderTime 75ms CodeModel.GetById 59ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/agross/netopenspace
Ruby | 118 lines | 100 code | 18 blank | 0 comment | 3 complexity | 4871bebae5ed6e95f8e455e65b3906db MD5 | raw file
  1require 'rss/taxonomy'
  2require 'rss/maker/1.0'
  3require 'rss/maker/dublincore'
  4
  5module RSS
  6  module Maker
  7    module TaxonomyTopicsModel
  8      def self.append_features(klass)
  9        super
 10
 11        klass.def_classed_element("#{RSS::TAXO_PREFIX}_topics",
 12                                  "TaxonomyTopics")
 13      end
 14
 15      def self.install_taxo_topics(klass)
 16        klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
 17          class TaxonomyTopics < TaxonomyTopicsBase
 18            def to_feed(feed, current)
 19              if current.respond_to?(:taxo_topics)
 20                topics = current.class::TaxonomyTopics.new
 21                bag = topics.Bag
 22                @resources.each do |resource|
 23                  bag.lis << RDF::Bag::Li.new(resource)
 24                end
 25                current.taxo_topics = topics
 26              end
 27            end
 28          end
 29EOC
 30      end
 31
 32      class TaxonomyTopicsBase < Base
 33        attr_reader :resources
 34        def_array_element("resource")
 35        remove_method :new_resource
 36      end
 37    end
 38
 39    module TaxonomyTopicModel
 40      def self.append_features(klass)
 41        super
 42
 43        class_name = "TaxonomyTopics"
 44        klass.def_classed_elements("#{TAXO_PREFIX}_topic", "value", class_name)
 45      end
 46
 47      def self.install_taxo_topic(klass)
 48        klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
 49          class TaxonomyTopics < TaxonomyTopicsBase
 50            class TaxonomyTopic < TaxonomyTopicBase
 51              DublinCoreModel.install_dublin_core(self)
 52              TaxonomyTopicsModel.install_taxo_topics(self)
 53
 54              def to_feed(feed, current)
 55                if current.respond_to?(:taxo_topics)
 56                  topic = current.class::TaxonomyTopic.new(value)
 57                  topic.taxo_link = value
 58                  taxo_topics.to_feed(feed, topic) if taxo_topics
 59                  current.taxo_topics << topic
 60                  setup_other_elements(feed, topic)
 61                end
 62              end
 63            end
 64          end
 65EOC
 66      end
 67
 68      class TaxonomyTopicsBase < Base
 69        def_array_element("topic", nil, "TaxonomyTopic")
 70        alias_method(:new_taxo_topic, :new_topic) # For backward compatibility
 71
 72        class TaxonomyTopicBase < Base
 73          include DublinCoreModel
 74          include TaxonomyTopicsModel
 75          
 76          attr_accessor :value
 77          add_need_initialize_variable("value")
 78          alias_method(:taxo_link, :value)
 79          alias_method(:taxo_link=, :value=)
 80          
 81          def have_required_values?
 82            @value
 83          end
 84        end
 85      end
 86    end
 87
 88    class RSSBase
 89      include TaxonomyTopicModel
 90    end
 91    
 92    class ChannelBase
 93      include TaxonomyTopicsModel
 94    end
 95    
 96    class ItemsBase
 97      class ItemBase
 98        include TaxonomyTopicsModel
 99      end
100    end
101
102    makers.each do |maker|
103      maker.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
104        TaxonomyTopicModel.install_taxo_topic(self)
105
106        class Channel
107          TaxonomyTopicsModel.install_taxo_topics(self)
108        end
109
110        class Items
111          class Item
112            TaxonomyTopicsModel.install_taxo_topics(self)
113          end
114        end
115      EOC
116    end
117  end
118end