2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-02-03 10:31:57 -05:00
|
|
|
require 'rss/1.0'
|
|
|
|
require 'rss/dublincore'
|
|
|
|
|
|
|
|
module RSS
|
|
|
|
|
2013-11-09 00:56:09 -05:00
|
|
|
# The prefix for the Image XML namespace.
|
2005-02-03 10:31:57 -05:00
|
|
|
IMAGE_PREFIX = 'image'
|
2013-11-09 00:56:09 -05:00
|
|
|
# The URI for the Image specification.
|
2007-05-06 22:02:50 -04:00
|
|
|
IMAGE_URI = 'http://purl.org/rss/1.0/modules/image/'
|
2005-02-03 10:31:57 -05:00
|
|
|
|
|
|
|
RDF.install_ns(IMAGE_PREFIX, IMAGE_URI)
|
|
|
|
|
2013-11-09 00:56:09 -05:00
|
|
|
# This constant holds strings which contain the names of
|
|
|
|
# image elements, with the appropriate prefix.
|
2005-11-24 20:39:16 -05:00
|
|
|
IMAGE_ELEMENTS = []
|
|
|
|
|
|
|
|
%w(item favicon).each do |name|
|
|
|
|
class_name = Utils.to_class_name(name)
|
|
|
|
BaseListener.install_class_name(IMAGE_URI, name, "Image#{class_name}")
|
|
|
|
IMAGE_ELEMENTS << "#{IMAGE_PREFIX}_#{name}"
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
module ImageModelUtils
|
2007-03-17 06:13:25 -04:00
|
|
|
def validate_one_tag_name(ignore_unknown_element, name, tags)
|
|
|
|
if !ignore_unknown_element
|
|
|
|
invalid = tags.find {|tag| tag != name}
|
|
|
|
raise UnknownTagError.new(invalid, IMAGE_URI) if invalid
|
|
|
|
end
|
2005-02-03 10:31:57 -05:00
|
|
|
raise TooMuchTagError.new(name, tag_name) if tags.size > 1
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
module ImageItemModel
|
|
|
|
include ImageModelUtils
|
|
|
|
extend BaseModel
|
|
|
|
|
|
|
|
def self.append_features(klass)
|
|
|
|
super
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
klass.install_have_child_element("item", IMAGE_URI, "?",
|
|
|
|
"#{IMAGE_PREFIX}_item")
|
|
|
|
klass.install_must_call_validator(IMAGE_PREFIX, IMAGE_URI)
|
2005-02-03 10:31:57 -05:00
|
|
|
end
|
|
|
|
|
2005-11-24 20:39:16 -05:00
|
|
|
class ImageItem < Element
|
2005-02-03 10:31:57 -05:00
|
|
|
include RSS10
|
|
|
|
include DublinCoreModel
|
|
|
|
|
2005-11-24 20:39:16 -05:00
|
|
|
@tag_name = "item"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
class << self
|
|
|
|
def required_prefix
|
|
|
|
IMAGE_PREFIX
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
def required_uri
|
|
|
|
IMAGE_URI
|
|
|
|
end
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
|
|
|
install_must_call_validator(IMAGE_PREFIX, IMAGE_URI)
|
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
[
|
|
|
|
["about", ::RSS::RDF::URI, true],
|
|
|
|
["resource", ::RSS::RDF::URI, false],
|
|
|
|
].each do |name, uri, required|
|
2007-03-17 06:13:25 -04:00
|
|
|
install_get_attribute(name, uri, required, nil, nil,
|
|
|
|
"#{::RSS::RDF::PREFIX}:#{name}")
|
2005-02-03 10:31:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
%w(width height).each do |tag|
|
|
|
|
full_name = "#{IMAGE_PREFIX}_#{tag}"
|
2005-11-24 22:43:48 -05:00
|
|
|
disp_name = "#{IMAGE_PREFIX}:#{tag}"
|
2007-03-17 06:13:25 -04:00
|
|
|
install_text_element(tag, IMAGE_URI, "?",
|
|
|
|
full_name, :integer, disp_name)
|
|
|
|
BaseListener.install_get_text_element(IMAGE_URI, tag, full_name)
|
2005-02-03 10:31:57 -05:00
|
|
|
end
|
|
|
|
|
2005-11-24 22:43:48 -05:00
|
|
|
alias width= image_width=
|
|
|
|
alias width image_width
|
|
|
|
alias height= image_height=
|
|
|
|
alias height image_height
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def initialize(*args)
|
|
|
|
if Utils.element_initialize_arguments?(args)
|
|
|
|
super
|
|
|
|
else
|
|
|
|
super()
|
|
|
|
self.about = args[0]
|
|
|
|
self.resource = args[1]
|
|
|
|
end
|
2005-02-03 10:31:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_name
|
|
|
|
tag_name_with_prefix(IMAGE_PREFIX)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def maker_target(target)
|
|
|
|
target.image_item
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_maker_attributes(item)
|
|
|
|
item.about = self.about
|
|
|
|
item.resource = self.resource
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
module ImageFaviconModel
|
|
|
|
include ImageModelUtils
|
|
|
|
extend BaseModel
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
def self.append_features(klass)
|
|
|
|
super
|
|
|
|
|
|
|
|
unless klass.class == Module
|
2007-03-17 06:13:25 -04:00
|
|
|
klass.install_have_child_element("favicon", IMAGE_URI, "?",
|
|
|
|
"#{IMAGE_PREFIX}_favicon")
|
|
|
|
klass.install_must_call_validator(IMAGE_PREFIX, IMAGE_URI)
|
2005-02-03 10:31:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-24 20:39:16 -05:00
|
|
|
class ImageFavicon < Element
|
2005-02-03 10:31:57 -05:00
|
|
|
include RSS10
|
|
|
|
include DublinCoreModel
|
|
|
|
|
2005-11-24 20:39:16 -05:00
|
|
|
@tag_name = "favicon"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
class << self
|
|
|
|
def required_prefix
|
|
|
|
IMAGE_PREFIX
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
def required_uri
|
|
|
|
IMAGE_URI
|
|
|
|
end
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
[
|
2007-03-17 06:13:25 -04:00
|
|
|
["about", ::RSS::RDF::URI, true, ::RSS::RDF::PREFIX],
|
|
|
|
["size", IMAGE_URI, true, IMAGE_PREFIX],
|
|
|
|
].each do |name, uri, required, prefix|
|
|
|
|
install_get_attribute(name, uri, required, nil, nil,
|
|
|
|
"#{prefix}:#{name}")
|
2005-02-03 10:31:57 -05:00
|
|
|
end
|
|
|
|
|
2005-11-24 22:43:48 -05:00
|
|
|
AVAILABLE_SIZES = %w(small medium large)
|
2007-08-04 23:03:05 -04:00
|
|
|
alias_method :set_size, :size=
|
|
|
|
private :set_size
|
2005-11-24 22:43:48 -05:00
|
|
|
def size=(new_value)
|
|
|
|
if @do_validate and !new_value.nil?
|
|
|
|
new_value = new_value.strip
|
|
|
|
unless AVAILABLE_SIZES.include?(new_value)
|
|
|
|
attr_name = "#{IMAGE_PREFIX}:size"
|
|
|
|
raise NotAvailableValueError.new(full_name, new_value, attr_name)
|
|
|
|
end
|
|
|
|
end
|
2007-08-04 23:03:05 -04:00
|
|
|
set_size(new_value)
|
2005-11-24 22:43:48 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-02-03 10:31:57 -05:00
|
|
|
alias image_size= size=
|
|
|
|
alias image_size size
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def initialize(*args)
|
|
|
|
if Utils.element_initialize_arguments?(args)
|
|
|
|
super
|
|
|
|
else
|
|
|
|
super()
|
|
|
|
self.about = args[0]
|
|
|
|
self.size = args[1]
|
|
|
|
end
|
2005-02-03 10:31:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_name
|
|
|
|
tag_name_with_prefix(IMAGE_PREFIX)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def maker_target(target)
|
|
|
|
target.image_favicon
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_maker_attributes(favicon)
|
|
|
|
favicon.about = self.about
|
|
|
|
favicon.size = self.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class RDF
|
|
|
|
class Channel; include ImageFaviconModel; end
|
|
|
|
class Item; include ImageItemModel; end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|