PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/rss/maker/itunes.rb

http://github.com/ruby/ruby
Ruby | 243 lines | 208 code | 31 blank | 4 comment | 8 complexity | 9d20867541fbf569cd09aad46bc2e9f4 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'rss/itunes'
  3. require 'rss/maker/2.0'
  4. module RSS
  5. module Maker
  6. module ITunesBaseModel
  7. def def_class_accessor(klass, name, type, *args)
  8. name = name.gsub(/-/, "_").gsub(/^itunes_/, '')
  9. full_name = "#{RSS::ITUNES_PREFIX}_#{name}"
  10. case type
  11. when nil
  12. klass.def_other_element(full_name)
  13. when :yes_other
  14. def_yes_other_accessor(klass, full_name)
  15. when :yes_clean_other
  16. def_yes_clean_other_accessor(klass, full_name)
  17. when :csv
  18. def_csv_accessor(klass, full_name)
  19. when :element, :attribute
  20. recommended_attribute_name, = *args
  21. klass_name = "ITunes#{Utils.to_class_name(name)}"
  22. klass.def_classed_element(full_name, klass_name,
  23. recommended_attribute_name)
  24. when :elements
  25. plural_name, recommended_attribute_name = args
  26. plural_name ||= "#{name}s"
  27. full_plural_name = "#{RSS::ITUNES_PREFIX}_#{plural_name}"
  28. klass_name = "ITunes#{Utils.to_class_name(name)}"
  29. plural_klass_name = "ITunes#{Utils.to_class_name(plural_name)}"
  30. def_elements_class_accessor(klass, name, full_name, full_plural_name,
  31. klass_name, plural_klass_name,
  32. recommended_attribute_name)
  33. end
  34. end
  35. def def_yes_other_accessor(klass, full_name)
  36. klass.def_other_element(full_name)
  37. klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
  38. def #{full_name}?
  39. Utils::YesOther.parse(@#{full_name})
  40. end
  41. EOC
  42. end
  43. def def_yes_clean_other_accessor(klass, full_name)
  44. klass.def_other_element(full_name)
  45. klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
  46. def #{full_name}?
  47. Utils::YesCleanOther.parse(#{full_name})
  48. end
  49. EOC
  50. end
  51. def def_csv_accessor(klass, full_name)
  52. klass.def_csv_element(full_name)
  53. end
  54. def def_elements_class_accessor(klass, name, full_name, full_plural_name,
  55. klass_name, plural_klass_name,
  56. recommended_attribute_name=nil)
  57. if recommended_attribute_name
  58. klass.def_classed_elements(full_name, recommended_attribute_name,
  59. plural_klass_name, full_plural_name)
  60. else
  61. klass.def_classed_element(full_plural_name, plural_klass_name)
  62. end
  63. klass.module_eval(<<-EOC, __FILE__, __LINE__ + 1)
  64. def new_#{full_name}(text=nil)
  65. #{full_name} = @#{full_plural_name}.new_#{name}
  66. #{full_name}.text = text
  67. if block_given?
  68. yield #{full_name}
  69. else
  70. #{full_name}
  71. end
  72. end
  73. EOC
  74. end
  75. end
  76. module ITunesChannelModel
  77. extend ITunesBaseModel
  78. class << self
  79. def append_features(klass)
  80. super
  81. ::RSS::ITunesChannelModel::ELEMENT_INFOS.each do |name, type, *args|
  82. def_class_accessor(klass, name, type, *args)
  83. end
  84. end
  85. end
  86. class ITunesCategoriesBase < Base
  87. def_array_element("category", "itunes_categories",
  88. "ITunesCategory")
  89. class ITunesCategoryBase < Base
  90. attr_accessor :text
  91. add_need_initialize_variable("text")
  92. def_array_element("category", "itunes_categories",
  93. "ITunesCategory")
  94. def have_required_values?
  95. text
  96. end
  97. alias_method :to_feed_for_categories, :to_feed
  98. def to_feed(feed, current)
  99. if text and current.respond_to?(:itunes_category)
  100. new_item = current.class::ITunesCategory.new(text)
  101. to_feed_for_categories(feed, new_item)
  102. current.itunes_categories << new_item
  103. end
  104. end
  105. end
  106. end
  107. class ITunesImageBase < Base
  108. add_need_initialize_variable("href")
  109. attr_accessor("href")
  110. def to_feed(feed, current)
  111. if @href and current.respond_to?(:itunes_image)
  112. current.itunes_image ||= current.class::ITunesImage.new
  113. current.itunes_image.href = @href
  114. end
  115. end
  116. end
  117. class ITunesOwnerBase < Base
  118. %w(itunes_name itunes_email).each do |name|
  119. add_need_initialize_variable(name)
  120. attr_accessor(name)
  121. end
  122. def to_feed(feed, current)
  123. if current.respond_to?(:itunes_owner=)
  124. _not_set_required_variables = not_set_required_variables
  125. if (required_variable_names - _not_set_required_variables).empty?
  126. return
  127. end
  128. unless have_required_values?
  129. raise NotSetError.new("maker.channel.itunes_owner",
  130. _not_set_required_variables)
  131. end
  132. current.itunes_owner ||= current.class::ITunesOwner.new
  133. current.itunes_owner.itunes_name = @itunes_name
  134. current.itunes_owner.itunes_email = @itunes_email
  135. end
  136. end
  137. private
  138. def required_variable_names
  139. %w(itunes_name itunes_email)
  140. end
  141. end
  142. end
  143. module ITunesItemModel
  144. extend ITunesBaseModel
  145. class << self
  146. def append_features(klass)
  147. super
  148. ::RSS::ITunesItemModel::ELEMENT_INFOS.each do |name, type, *args|
  149. def_class_accessor(klass, name, type, *args)
  150. end
  151. end
  152. end
  153. class ITunesDurationBase < Base
  154. attr_reader :content
  155. add_need_initialize_variable("content")
  156. %w(hour minute second).each do |name|
  157. attr_reader(name)
  158. add_need_initialize_variable(name, 0)
  159. end
  160. def content=(content)
  161. if content.nil?
  162. @hour, @minute, @second, @content = nil
  163. else
  164. @hour, @minute, @second =
  165. ::RSS::ITunesItemModel::ITunesDuration.parse(content)
  166. @content = content
  167. end
  168. end
  169. def hour=(hour)
  170. @hour = Integer(hour)
  171. update_content
  172. end
  173. def minute=(minute)
  174. @minute = Integer(minute)
  175. update_content
  176. end
  177. def second=(second)
  178. @second = Integer(second)
  179. update_content
  180. end
  181. def to_feed(feed, current)
  182. if @content and current.respond_to?(:itunes_duration=)
  183. current.itunes_duration ||= current.class::ITunesDuration.new
  184. current.itunes_duration.content = @content
  185. end
  186. end
  187. private
  188. def update_content
  189. components = [@hour, @minute, @second]
  190. @content =
  191. ::RSS::ITunesItemModel::ITunesDuration.construct(*components)
  192. end
  193. end
  194. end
  195. class ChannelBase
  196. include Maker::ITunesChannelModel
  197. class ITunesCategories < ITunesCategoriesBase
  198. class ITunesCategory < ITunesCategoryBase
  199. ITunesCategory = self
  200. end
  201. end
  202. class ITunesImage < ITunesImageBase; end
  203. class ITunesOwner < ITunesOwnerBase; end
  204. end
  205. class ItemsBase
  206. class ItemBase
  207. include Maker::ITunesItemModel
  208. class ITunesDuration < ITunesDurationBase; end
  209. end
  210. end
  211. end
  212. end