2004-01-27 22:46:13 -05:00
|
|
|
require "rss/1.0"
|
|
|
|
|
|
|
|
module RSS
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
DC_PREFIX = 'dc'
|
|
|
|
DC_URI = "http://purl.org/dc/elements/1.1/"
|
|
|
|
|
|
|
|
RDF.install_ns(DC_PREFIX, DC_URI)
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2005-04-05 02:42:03 -04:00
|
|
|
module BaseDublinCoreModel
|
|
|
|
def append_features(klass)
|
|
|
|
super
|
|
|
|
|
|
|
|
return if klass.instance_of?(Module)
|
|
|
|
DublinCoreModel::ELEMENT_NAME_INFOS.each do |name, plural_name|
|
|
|
|
plural = plural_name || "#{name}s"
|
|
|
|
full_name = "#{DC_PREFIX}_#{name}"
|
|
|
|
full_plural_name = "#{DC_PREFIX}_#{plural}"
|
|
|
|
klass_name = "DublinCore#{Utils.to_class_name(name)}"
|
|
|
|
klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0))
|
|
|
|
install_have_children_element(#{full_name.dump},
|
|
|
|
#{full_plural_name.dump})
|
|
|
|
|
|
|
|
remove_method :#{full_name}
|
|
|
|
remove_method :#{full_name}=
|
|
|
|
remove_method :set_#{full_name}
|
|
|
|
|
|
|
|
def #{full_name}
|
|
|
|
@#{full_name}.first and @#{full_name}.first.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def #{full_name}=(new_value)
|
|
|
|
@#{full_name}[0] = Utils.new_with_value_if_need(#{klass_name}, new_value)
|
|
|
|
end
|
|
|
|
alias set_#{full_name} #{full_name}=
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
klass.module_eval(<<-EOC, *get_file_and_line_from_caller(0))
|
|
|
|
alias date #{DC_PREFIX}_date
|
|
|
|
alias date= #{DC_PREFIX}_date=
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
module DublinCoreModel
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
extend BaseModel
|
2005-04-05 02:42:03 -04:00
|
|
|
extend BaseDublinCoreModel
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2005-04-05 02:42:03 -04:00
|
|
|
TEXT_ELEMENTS = {
|
|
|
|
"title" => nil,
|
|
|
|
"description" => nil,
|
|
|
|
"creator" => nil,
|
|
|
|
"subject" => nil,
|
|
|
|
"publisher" => nil,
|
|
|
|
"contributor" => nil,
|
|
|
|
"type" => nil,
|
|
|
|
"format" => nil,
|
|
|
|
"identifier" => nil,
|
|
|
|
"source" => nil,
|
|
|
|
"language" => nil,
|
|
|
|
"relation" => nil,
|
|
|
|
"coverage" => nil,
|
|
|
|
"rights" => "rightses" # FIXME
|
|
|
|
}
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2005-04-05 02:42:03 -04:00
|
|
|
DATE_ELEMENTS = {
|
|
|
|
"date" => "w3cdtf",
|
|
|
|
}
|
|
|
|
|
|
|
|
ELEMENT_NAME_INFOS = DublinCoreModel::TEXT_ELEMENTS.to_a
|
|
|
|
DublinCoreModel::DATE_ELEMENTS.each do |name, |
|
|
|
|
ELEMENT_NAME_INFOS << [name, nil]
|
|
|
|
end
|
|
|
|
|
|
|
|
ELEMENTS = TEXT_ELEMENTS.keys + DATE_ELEMENTS.keys
|
|
|
|
|
2005-04-05 03:03:43 -04:00
|
|
|
ELEMENTS.each do |name, plural_name|
|
2005-04-05 02:42:03 -04:00
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(0))
|
2005-04-05 03:03:43 -04:00
|
|
|
class DublinCore#{Utils.to_class_name(name)} < Element
|
2005-04-05 02:42:03 -04:00
|
|
|
include RSS10
|
|
|
|
|
|
|
|
content_setup
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def required_prefix
|
|
|
|
DC_PREFIX
|
|
|
|
end
|
|
|
|
|
|
|
|
def required_uri
|
|
|
|
DC_URI
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-05 03:03:43 -04:00
|
|
|
@tag_name = #{name.dump}
|
2005-04-05 02:42:03 -04:00
|
|
|
|
|
|
|
alias_method(:value, :content)
|
|
|
|
alias_method(:value=, :content=)
|
|
|
|
|
|
|
|
def initialize(content=nil)
|
|
|
|
super()
|
|
|
|
self.content = content
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2005-04-05 02:42:03 -04:00
|
|
|
def full_name
|
|
|
|
tag_name_with_prefix(DC_PREFIX)
|
|
|
|
end
|
|
|
|
|
|
|
|
def maker_target(target)
|
2005-04-05 03:03:43 -04:00
|
|
|
target.new_#{name}
|
2005-04-05 02:42:03 -04:00
|
|
|
end
|
2004-02-03 09:01:42 -05:00
|
|
|
|
2005-04-05 03:03:43 -04:00
|
|
|
def setup_maker_attributes(#{name})
|
|
|
|
#{name}.content = content
|
2005-04-05 02:42:03 -04:00
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2005-04-05 02:42:03 -04:00
|
|
|
DATE_ELEMENTS.each do |name, type|
|
|
|
|
module_eval(<<-EOC, *get_file_and_line_from_caller(0))
|
|
|
|
class DublinCore#{Utils.to_class_name(name)} < Element
|
2005-04-05 03:03:43 -04:00
|
|
|
remove_method(:content=)
|
|
|
|
remove_method(:value=)
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2005-04-05 02:42:03 -04:00
|
|
|
date_writer("content", #{type.dump}, #{name.dump})
|
|
|
|
|
|
|
|
alias_method(:value=, :content=)
|
|
|
|
end
|
|
|
|
EOC
|
|
|
|
end
|
|
|
|
|
|
|
|
def dc_validate(tags)
|
2004-10-16 00:51:15 -04:00
|
|
|
tags.each do |tag|
|
|
|
|
key = "#{DC_PREFIX}_#{tag}"
|
2005-04-05 02:42:03 -04:00
|
|
|
unless DublinCoreModel::ELEMENTS.include?(key)
|
|
|
|
raise UnknownTagError.new(tag, DC_URI)
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
# For backward compatibility
|
|
|
|
DublincoreModel = DublinCoreModel
|
2004-03-02 11:21:11 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
class RDF
|
|
|
|
class Channel; include DublinCoreModel; end
|
|
|
|
class Image; include DublinCoreModel; end
|
|
|
|
class Item; include DublinCoreModel; end
|
|
|
|
class Textinput; include DublinCoreModel; end
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2005-04-05 03:03:43 -04:00
|
|
|
DublinCoreModel::ELEMENTS.each do |name|
|
|
|
|
class_name = Utils.to_class_name(name)
|
|
|
|
BaseListener.install_class_name(DC_URI, name, "DublinCore#{class_name}")
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
2005-04-05 03:03:43 -04:00
|
|
|
DublinCoreModel::ELEMENTS.collect! {|name| "#{DC_PREFIX}_#{name}"}
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|