PageRenderTime 49ms CodeModel.GetById 38ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/rss/syndication.rb

http://github.com/agross/netopenspace
Ruby | 67 lines | 53 code | 14 blank | 0 comment | 1 complexity | cb262a23b462d1d666f78a49574781c9 MD5 | raw file
 1require "rss/1.0"
 2
 3module RSS
 4
 5  SY_PREFIX = 'sy'
 6  SY_URI = "http://purl.org/rss/1.0/modules/syndication/"
 7
 8  RDF.install_ns(SY_PREFIX, SY_URI)
 9
10  module SyndicationModel
11    
12    extend BaseModel
13    
14    ELEMENTS = []
15    
16    def self.append_features(klass)
17      super
18
19      klass.install_must_call_validator(SY_PREFIX, SY_URI)
20      klass.module_eval do
21        [
22          ["updatePeriod"],
23          ["updateFrequency", :positive_integer]
24        ].each do |name, type|
25          install_text_element(name, SY_URI, "?",
26                               "#{SY_PREFIX}_#{name}", type,
27                               "#{SY_PREFIX}:#{name}")
28        end
29
30        %w(updateBase).each do |name|
31          install_date_element(name, SY_URI, "?",
32                               "#{SY_PREFIX}_#{name}", 'w3cdtf',
33                               "#{SY_PREFIX}:#{name}")
34        end
35      end
36
37      klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
38        alias_method(:_sy_updatePeriod=, :sy_updatePeriod=)
39        def sy_updatePeriod=(new_value)
40          new_value = new_value.strip
41          validate_sy_updatePeriod(new_value) if @do_validate
42          self._sy_updatePeriod = new_value
43        end
44      EOC
45    end
46
47    private
48    SY_UPDATEPERIOD_AVAILABLE_VALUES = %w(hourly daily weekly monthly yearly)
49    def validate_sy_updatePeriod(value)
50      unless SY_UPDATEPERIOD_AVAILABLE_VALUES.include?(value)
51        raise NotAvailableValueError.new("updatePeriod", value)
52      end
53    end
54  end
55
56  class RDF
57    class Channel; include SyndicationModel; end
58  end
59
60  prefix_size = SY_PREFIX.size + 1
61  SyndicationModel::ELEMENTS.uniq!
62  SyndicationModel::ELEMENTS.each do |full_name|
63    name = full_name[prefix_size..-1]
64    BaseListener.install_get_text_element(SY_URI, name, full_name)
65  end
66
67end