Class RSS::Maker::Base
In: lib/rss/maker/base.rb
Parent: Object

Methods

Included Modules

Enumerable

Constants

OTHER_ELEMENTS = []
NEED_INITIALIZE_VARIABLES = []

Attributes

maker  [R] 

Public Class methods

[Source]

    # File lib/rss/maker/base.rb, line 34
34:         def add_need_initialize_variable(variable_name, init_value="nil")
35:           self::NEED_INITIALIZE_VARIABLES << [variable_name, init_value]
36:         end

[Source]

    # File lib/rss/maker/base.rb, line 30
30:         def add_other_element(variable_name)
31:           self::OTHER_ELEMENTS << variable_name
32:         end

[Source]

    # File lib/rss/maker/base.rb, line 38
38:         def def_array_element(name, plural=nil, klass_name=nil)
39:           include Enumerable
40:           extend Forwardable
41: 
42:           plural ||= "#{name}s"
43:           klass_name ||= Utils.to_class_name(name)
44:           def_delegators("@#{plural}", :<<, :[], :[]=, :first, :last)
45:           def_delegators("@#{plural}", :push, :pop, :shift, :unshift)
46:           def_delegators("@#{plural}", :each, :size, :empty?, :clear)
47: 
48:           add_need_initialize_variable(plural, "[]")
49: 
50:           module_eval("def new_\#{name}\n\#{name} = self.class::\#{klass_name}.new(@maker)\n@\#{plural} << \#{name}\nif block_given?\nyield \#{name}\nelse\n\#{name}\nend\nend\nalias new_child new_\#{name}\n\ndef to_feed(*args)\n@\#{plural}.each do |\#{name}|\n\#{name}.to_feed(*args)\nend\nend\n\ndef replace(elements)\n@\#{plural}.replace(elements.to_a)\nend\n", __FILE__, __LINE__ + 1)
51:         end

[Source]

    # File lib/rss/maker/base.rb, line 92
92:         def def_classed_element(name, class_name=nil, attribute_name=nil)
93:           def_classed_element_without_accessor(name, class_name)
94:           if attribute_name
95:             module_eval("def \#{name}\nif block_given?\nyield(@\#{name})\nelse\n@\#{name}.\#{attribute_name}\nend\nend\n\ndef \#{name}=(new_value)\n@\#{name}.\#{attribute_name} = new_value\nend\n", __FILE__, __LINE__ + 1)
96:           else
97:             attr_reader name
98:           end
99:         end

[Source]

    # File lib/rss/maker/base.rb, line 75
75:         def def_classed_element_without_accessor(name, class_name=nil)
76:           class_name ||= Utils.to_class_name(name)
77:           add_other_element(name)
78:           add_need_initialize_variable(name, "make_#{name}")
79:           module_eval("private\ndef setup_\#{name}(feed, current)\n@\#{name}.to_feed(feed, current)\nend\n\ndef make_\#{name}\nself.class::\#{class_name}.new(@maker)\nend\n", __FILE__, __LINE__ + 1)
80:         end

[Source]

     # File lib/rss/maker/base.rb, line 114
114:         def def_classed_elements(name, attribute, plural_class_name=nil,
115:                                  plural_name=nil, new_name=nil)
116:           plural_name ||= "#{name}s"
117:           new_name ||= name
118:           def_classed_element(plural_name, plural_class_name)
119:           local_variable_name = "_#{name}"
120:           new_value_variable_name = "new_value"
121:           additional_setup_code = nil
122:           if block_given?
123:             additional_setup_code = yield(local_variable_name,
124:                                           new_value_variable_name)
125:           end
126:           module_eval("def \#{name}\n\#{local_variable_name} = \#{plural_name}.first\n\#{local_variable_name} ? \#{local_variable_name}.\#{attribute} : nil\nend\n\ndef \#{name}=(\#{new_value_variable_name})\n\#{local_variable_name} =\n\#{plural_name}.first || \#{plural_name}.new_\#{new_name}\n\#{additional_setup_code}\n\#{local_variable_name}.\#{attribute} = \#{new_value_variable_name}\nend\n", __FILE__, __LINE__ + 1)
127:         end

[Source]

     # File lib/rss/maker/base.rb, line 160
160:         def def_csv_element(name, type=nil)
161:           def_other_element_without_accessor(name)
162:           attr_reader(name)
163:           converter = ""
164:           if type == :integer
165:             converter = "{|v| Integer(v)}"
166:           end
167:           module_eval("def \#{name}=(value)\n@\#{name} = Utils::CSV.parse(value)\#{converter}\nend\n", __FILE__, __LINE__ + 1)
168:         end

[Source]

     # File lib/rss/maker/base.rb, line 142
142:         def def_other_element(name)
143:           attr_accessor name
144:           def_other_element_without_accessor(name)
145:         end

[Source]

     # File lib/rss/maker/base.rb, line 147
147:         def def_other_element_without_accessor(name)
148:           add_need_initialize_variable(name)
149:           add_other_element(name)
150:           module_eval("def setup_\#{name}(feed, current)\nif !@\#{name}.nil? and current.respond_to?(:\#{name}=)\ncurrent.\#{name} = @\#{name}\nend\nend\n", __FILE__, __LINE__ + 1)
151:         end

[Source]

    # File lib/rss/maker/base.rb, line 25
25:         def inherited(subclass)
26:           subclass.const_set("OTHER_ELEMENTS", [])
27:           subclass.const_set("NEED_INITIALIZE_VARIABLES", [])
28:         end

[Source]

    # File lib/rss/maker/base.rb, line 21
21:         def inherited_base
22:           ::RSS::Maker::Base
23:         end

[Source]

    # File lib/rss/maker/base.rb, line 17
17:         def need_initialize_variables
18:           inherited_array_reader("NEED_INITIALIZE_VARIABLES")
19:         end

[Source]

     # File lib/rss/maker/base.rb, line 177
177:       def initialize(maker)
178:         @maker = maker
179:         @default_values_are_set = false
180:         initialize_variables
181:       end

[Source]

    # File lib/rss/maker/base.rb, line 14
14:         def other_elements
15:           inherited_array_reader("OTHER_ELEMENTS")
16:         end

Public Instance methods

[Source]

     # File lib/rss/maker/base.rb, line 183
183:       def have_required_values?
184:         not_set_required_variables.empty?
185:       end

[Source]

     # File lib/rss/maker/base.rb, line 187
187:       def variable_is_set?
188:         variables.any? {|var| not __send__(var).nil?}
189:       end

Private Instance methods

[Source]

     # File lib/rss/maker/base.rb, line 220
220:       def _set_default_values(&block)
221:         yield
222:       end

[Source]

     # File lib/rss/maker/base.rb, line 205
205:       def current_element(feed)
206:         feed
207:       end

[Source]

     # File lib/rss/maker/base.rb, line 192
192:       def initialize_variables
193:         self.class.need_initialize_variables.each do |variable_name, init_value|
194:           instance_eval("@#{variable_name} = #{init_value}", __FILE__, __LINE__)
195:         end
196:       end

[Source]

     # File lib/rss/maker/base.rb, line 253
253:       def not_set_required_variables
254:         required_variable_names.find_all do |var|
255:           __send__(var).nil?
256:         end
257:       end

[Source]

     # File lib/rss/maker/base.rb, line 259
259:       def required_variables_are_set?
260:         required_variable_names.each do |var|
261:           return false if __send__(var).nil?
262:         end
263:         true
264:       end

[Source]

     # File lib/rss/maker/base.rb, line 209
209:       def set_default_values(&block)
210:         return yield if @default_values_are_set
211: 
212:         begin
213:           @default_values_are_set = true
214:           _set_default_values(&block)
215:         ensure
216:           @default_values_are_set = false
217:         end
218:       end

[Source]

     # File lib/rss/maker/base.rb, line 241
241:       def set_parent(target, parent)
242:         target.parent = parent if target.class.need_parent?
243:       end

[Source]

     # File lib/rss/maker/base.rb, line 198
198:       def setup_other_elements(feed, current=nil)
199:         current ||= current_element(feed)
200:         self.class.other_elements.each do |element|
201:           __send__("setup_#{element}", feed, current)
202:         end
203:       end

[Source]

     # File lib/rss/maker/base.rb, line 224
224:       def setup_values(target)
225:         set = false
226:         if have_required_values?
227:           variables.each do |var|
228:             setter = "#{var}="
229:             if target.respond_to?(setter)
230:               value = __send__(var)
231:               if value
232:                 target.__send__(setter, value)
233:                 set = true
234:               end
235:             end
236:           end
237:         end
238:         set
239:       end

[Source]

     # File lib/rss/maker/base.rb, line 245
245:       def variables
246:         self.class.need_initialize_variables.find_all do |name, init|
247:           "nil" == init
248:         end.collect do |name, init|
249:           name
250:         end
251:       end

[Validate]