4
ITUNES_PREFIX = 'itunes'
5
ITUNES_URI = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
7
Rss.install_ns(ITUNES_PREFIX, ITUNES_URI)
9
module ITunesModelUtils
12
def def_class_accessor(klass, name, type, *args)
13
normalized_name = name.gsub(/-/, "_")
14
full_name = "#{ITUNES_PREFIX}_#{normalized_name}"
15
klass_name = "ITunes#{Utils.to_class_name(normalized_name)}"
18
when :element, :attribute
19
klass::ELEMENTS << full_name
20
def_element_class_accessor(klass, name, full_name, klass_name, *args)
22
klass::ELEMENTS << full_name
23
def_elements_class_accessor(klass, name, full_name, klass_name, *args)
25
klass.install_must_call_validator(ITUNES_PREFIX, ITUNES_URI)
26
klass.install_text_element(normalized_name, ITUNES_URI, "?",
27
full_name, type, name)
31
def def_element_class_accessor(klass, name, full_name, klass_name,
32
recommended_attribute_name=nil)
33
klass.install_have_child_element(name, ITUNES_PREFIX, "?", full_name)
36
def def_elements_class_accessor(klass, name, full_name, klass_name,
37
plural_name, recommended_attribute_name=nil)
38
full_plural_name = "#{ITUNES_PREFIX}_#{plural_name}"
39
klass.install_have_children_element(name, ITUNES_PREFIX, "*",
40
full_name, full_plural_name)
44
module ITunesBaseModel
45
extend ITunesModelUtils
49
ELEMENT_INFOS = [["author"],
50
["block", :yes_other],
51
["explicit", :yes_clean_other],
57
module ITunesChannelModel
59
extend ITunesModelUtils
60
include ITunesBaseModel
65
def append_features(klass)
68
return if klass.instance_of?(Module)
69
ELEMENT_INFOS.each do |name, type, *additional_infos|
70
def_class_accessor(klass, name, type, *additional_infos)
76
["category", :elements, "categories", "text"],
77
["image", :attribute, "href"],
80
] + ITunesBaseModel::ELEMENT_INFOS
82
class ITunesCategory < Element
85
@tag_name = "category"
99
].each do |name, uri, required|
100
install_get_attribute(name, uri, required)
103
ITunesCategory = self
104
install_have_children_element("category", ITUNES_URI, "*",
105
"#{ITUNES_PREFIX}_category",
106
"#{ITUNES_PREFIX}_categories")
108
def initialize(*args)
109
if Utils.element_initialize_arguments?(args)
118
tag_name_with_prefix(ITUNES_PREFIX)
122
def maker_target(categories)
123
if text or !itunes_categories.empty?
124
categories.new_category
130
def setup_maker_attributes(category)
131
category.text = text if text
134
def setup_maker_elements(category)
136
itunes_categories.each do |sub_category|
137
sub_category.setup_maker(category)
142
class ITunesImage < Element
159
].each do |name, uri, required|
160
install_get_attribute(name, uri, required)
163
def initialize(*args)
164
if Utils.element_initialize_arguments?(args)
173
tag_name_with_prefix(ITUNES_PREFIX)
177
def maker_target(target)
179
target.itunes_image {|image| image}
185
def setup_maker_attributes(image)
190
class ITunesOwner < Element
205
install_must_call_validator(ITUNES_PREFIX, ITUNES_URI)
210
ITunesBaseModel::ELEMENT_INFOS << name
211
install_text_element(name, ITUNES_URI, nil, "#{ITUNES_PREFIX}_#{name}")
214
def initialize(*args)
215
if Utils.element_initialize_arguments?(args)
219
self.itunes_name = args[0]
220
self.itunes_email = args[1]
225
tag_name_with_prefix(ITUNES_PREFIX)
229
def maker_target(target)
233
def setup_maker_element(owner)
235
owner.itunes_name = itunes_name
236
owner.itunes_email = itunes_email
241
module ITunesItemModel
243
extend ITunesModelUtils
244
include ITunesBaseModel
247
def append_features(klass)
250
return if klass.instance_of?(Module)
251
ELEMENT_INFOS.each do |name, type|
252
def_class_accessor(klass, name, type)
257
ELEMENT_INFOS = ITunesBaseModel::ELEMENT_INFOS +
258
[["duration", :element, "content"]]
260
class ITunesDuration < Element
263
@tag_name = "duration"
274
def parse(duration, do_validate=true)
275
if do_validate and /\A(?:
276
\d?\d:[0-5]\d:[0-5]\d|
280
"must be one of HH:MM:SS, H:MM:SS, MM::SS, M:SS: " +
284
components = duration.split(':')
285
components[3..-1] = nil if components.size > 3
287
components.unshift("00") until components.size == 3
289
components.collect do |component|
294
def construct(hour, minute, second)
295
components = [minute, second]
296
if components.include?(nil)
299
components.unshift(hour) if hour and hour > 0
300
components.collect do |component|
308
alias_method(:value, :content)
309
remove_method(:content=)
311
attr_reader :hour, :minute, :second
312
def initialize(*args)
313
if Utils.element_initialize_arguments?(args)
317
args = args[0] if args.size == 1 and args[0].is_a?(Array)
319
self.content = args[0]
322
"must be (do_validate, params), (content), " +
323
"(minute, second), ([minute, second]), " +
324
"(hour, minute, second) or ([hour, minute, second]): " +
327
@second, @minute, @hour = args.reverse
336
elsif value.is_a?(self.class)
337
self.content = value.content
340
@hour, @minute, @second = self.class.parse(value, @do_validate)
342
raise NotAvailableValueError.new(tag_name, value)
347
alias_method(:value=, :content=)
350
@hour = @do_validate ? Integer(hour) : hour.to_i
356
@minute = @do_validate ? Integer(minute) : minute.to_i
362
@second = @do_validate ? Integer(second) : second.to_i
368
tag_name_with_prefix(ITUNES_PREFIX)
373
@content = self.class.construct(hour, minute, second)
376
def maker_target(target)
378
target.itunes_duration {|duration| duration}
384
def setup_maker_element(duration)
386
duration.content = @content
393
include ITunesChannelModel
394
class Item; include ITunesItemModel; end
399
ITunesChannelModel::ELEMENT_INFOS + ITunesItemModel::ELEMENT_INFOS
400
element_infos.each do |name, type|
401
class_name = Utils.to_class_name(name)
403
when :element, :elements, :attribute
404
BaseListener.install_class_name(ITUNES_URI, name, "ITunes#{class_name}")
406
accessor_base = "#{ITUNES_PREFIX}_#{name.gsub(/-/, '_')}"
407
BaseListener.install_get_text_element(ITUNES_URI, name, accessor_base)