1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
require 'forwardable'
require 'rss/rss'
module RSS
module Maker
module Base
def self.append_features(klass)
super
klass.module_eval(<<-EOC, __FILE__, __LINE__)
OTHER_ELEMENTS = []
NEED_INITIALIZE_VARIABLES = []
def self.inherited(subclass)
subclass.const_set("OTHER_ELEMENTS", [])
subclass.const_set("NEED_INITIALIZE_VARIABLES", [])
subclass.module_eval(<<-EOEOC, __FILE__, __LINE__)
def self.other_elements
const_get("OTHER_ELEMENTS") + super
end
def self.need_initialize_variables
const_get("NEED_INITIALIZE_VARIABLES") + super
end
EOEOC
end
def self.add_other_element(variable_name)
const_get("OTHER_ELEMENTS") << variable_name
end
def self.other_elements
OTHER_ELEMENTS
end
def self.add_need_initialize_variable(variable_name, init_value="nil")
const_get("NEED_INITIALIZE_VARIABLES") << [variable_name, init_value]
end
def self.need_initialize_variables
NEED_INITIALIZE_VARIABLES
end
def self.def_array_element(name)
include Enumerable
extend Forwardable
def_delegators("@\#{name}", :<<, :[], :[]=, :first, :last)
def_delegators("@\#{name}", :push, :pop, :shift, :unshift)
def_delegators("@\#{name}", :each)
add_need_initialize_variable(name, "[]")
end
EOC
end
def initialize(maker)
@maker = maker
initialize_variables
end
def have_required_values?
true
end
private
def initialize_variables
self.class.need_initialize_variables.each do |variable_name, init_value|
instance_eval("@#{variable_name} = #{init_value}", __FILE__, __LINE__)
end
end
def setup_other_elements(rss)
self.class.other_elements.each do |element|
__send__("setup_#{element}", rss, current_element(rss))
end
end
def setup_values(target)
set = false
if have_required_values?
variables.each do |var|
setter = "#{var}="
if target.respond_to?(setter)
value = self.__send__(var)
if value
target.__send__(setter, value)
set = true
end
end
end
end
set
end
def variables
self.class.need_initialize_variables.find_all do |name, init|
"nil" == init
end.collect do |name, init|
name
end
end
end
class RSSBase
include Base
class << self
def make(&block)
new.make(&block)
end
end
attr_reader :rss_version, :xml_stylesheets
attr_reader :channel, :image, :items, :textinput
attr_accessor :version, :encoding, :standalone
def initialize(rss_version)
super(self)
@rss_version = rss_version
@version = "1.0"
@encoding = "UTF-8"
@standalone = nil
@xml_stylesheets = make_xml_stylesheets
@channel = make_channel
@image = make_image
@items = make_items
@textinput = make_textinput
end
def make(&block)
block.call(self) if block
to_rss
end
def current_element(rss)
rss
end
private
def make_xml_stylesheets
XMLStyleSheets.new(self)
end
def make_channel
self.class::Channel.new(self)
end
def make_image
self.class::Image.new(self)
end
def make_items
self.class::Items.new(self)
end
def make_textinput
self.class::Textinput.new(self)
end
def setup_xml_stylesheets(rss)
@xml_stylesheets.to_rss(rss)
end
end
class XMLStyleSheets
include Base
def_array_element("xml_stylesheets")
def to_rss(rss)
@xml_stylesheets.each do |xss|
xss.to_rss(rss)
end
end
def new_xml_stylesheet
xss = XMLStyleSheet.new(@maker)
@xml_stylesheets << xss
xss
end
class XMLStyleSheet
include Base
::RSS::XMLStyleSheet::ATTRIBUTES.each do |attribute|
attr_accessor attribute
add_need_initialize_variable(attribute)
end
def to_rss(rss)
xss = ::RSS::XMLStyleSheet.new
guess_type_if_need(xss)
set = setup_values(xss)
if set
rss.xml_stylesheets << xss
end
end
def have_required_values?
@href and @type
end
private
def guess_type_if_need(xss)
if @type.nil?
xss.href = @href
@type = xss.type
end
end
end
end
class ChannelBase
include Base
%w(cloud categories skipDays skipHours).each do |element|
attr_reader element
add_other_element(element)
add_need_initialize_variable(element, "make_#{element}")
module_eval(<<-EOC, __FILE__, __LINE__)
private
def setup_#{element}(rss, current)
@#{element}.to_rss(rss, current)
end
def make_#{element}
self.class::#{element[0,1].upcase}#{element[1..-1]}.new(@maker)
end
EOC
end
%w(about title link description language copyright
managingEditor webMaster rating docs date
lastBuildDate generator ttl).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
alias_method(:pubDate, :date)
alias_method(:pubDate=, :date=)
def current_element(rss)
rss.channel
end
class SkipDaysBase
include Base
def_array_element("days")
def new_day
day = self.class::Day.new(@maker)
@days << day
day
end
def current_element(rss)
rss.channel.skipDays
end
class DayBase
include Base
%w(content).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
def current_element(rss)
rss.channel.skipDays.last
end
end
end
class SkipHoursBase
include Base
def_array_element("hours")
def new_hour
hour = self.class::Hour.new(@maker)
@hours << hour
hour
end
def current_element(rss)
rss.channel.skipHours
end
class HourBase
include Base
%w(content).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
def current_element(rss)
rss.channel.skipHours.last
end
end
end
class CloudBase
include Base
%w(domain port path registerProcedure protocol).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
def current_element(rss)
rss.channel.cloud
end
end
class CategoriesBase
include Base
def_array_element("categories")
def new_category
category = self.class::Category.new(@maker)
@categories << category
category
end
class CategoryBase
include Base
%w(domain content).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
end
end
end
class ImageBase
include Base
%w(title url width height description).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
def link
@maker.channel.link
end
def current_element(rss)
rss.image
end
end
class ItemsBase
include Base
def_array_element("items")
attr_accessor :do_sort, :max_size
def initialize(maker)
super
@do_sort = false
@max_size = -1
end
def normalize
sort_if_need[0..@max_size]
end
def current_element(rss)
rss.items
end
def new_item
item = self.class::Item.new(@maker)
@items << item
item
end
private
def sort_if_need
if @do_sort.respond_to?(:call)
@items.sort do |x, y|
@do_sort.call(x, y)
end
elsif @do_sort
@items.sort do |x, y|
y <=> x
end
else
@items
end
end
class ItemBase
include Base
%w(guid enclosure source categories).each do |element|
attr_reader element
add_other_element(element)
add_need_initialize_variable(element, "make_#{element}")
module_eval(<<-EOC, __FILE__, __LINE__)
private
def setup_#{element}(rss, current)
@#{element}.to_rss(rss, current)
end
def make_#{element}
self.class::#{element[0,1].upcase}#{element[1..-1]}.new(@maker)
end
EOC
end
%w(title link description date author comments).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
alias_method(:pubDate, :date)
alias_method(:pubDate=, :date=)
def <=>(other)
if @date and other.date
@date <=> other.date
elsif @date
1
elsif other.date
-1
else
0
end
end
def current_element(rss)
rss.items.last
end
class GuidBase
include Base
%w(isPermaLink content).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
end
class EnclosureBase
include Base
%w(url length type).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
end
class SourceBase
include Base
%w(url content).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
end
CategoriesBase = ChannelBase::CategoriesBase
end
end
class TextinputBase
include Base
%w(title description name link).each do |element|
attr_accessor element
add_need_initialize_variable(element)
end
def current_element(rss)
rss.textinput
end
end
end
end
|