class RSS::Maker::Base

Constants

NEED_INITIALIZE_VARIABLES
OTHER_ELEMENTS

Attributes

maker[R]

Public Class Methods

add_need_initialize_variable(variable_name, init_value=nil, &init_block) click to toggle source
# File lib/rss/maker/base.rb, line 34
def add_need_initialize_variable(variable_name, init_value=nil,
                                 &init_block)
  init_value ||= init_block
  self::NEED_INITIALIZE_VARIABLES << [variable_name, init_value]
end
add_other_element(variable_name) click to toggle source
# File lib/rss/maker/base.rb, line 30
def add_other_element(variable_name)
  self::OTHER_ELEMENTS << variable_name
end
def_array_element(name, plural=nil, klass_name=nil) click to toggle source
# File lib/rss/maker/base.rb, line 40
        def def_array_element(name, plural=nil, klass_name=nil)
          include Enumerable
          extend Forwardable

          plural ||= "#{name}s"
          klass_name ||= Utils.to_class_name(name)
          def_delegators("@#{plural}", :<<, :[], :[]=, :first, :last)
          def_delegators("@#{plural}", :push, :pop, :shift, :unshift)
          def_delegators("@#{plural}", :each, :size, :empty?, :clear)

          add_need_initialize_variable(plural) {[]}

          module_eval(<<-EOC, __FILE__, __LINE__ + 1)
            def new_#{name}
              #{name} = self.class::#{klass_name}.new(@maker)
              @#{plural} << #{name}
              if block_given?
                yield #{name}
              else
                #{name}
              end
            end
            alias new_child new_#{name}

            def to_feed(*args)
              @#{plural}.each do |#{name}|
                #{name}.to_feed(*args)
              end
            end

            def replace(elements)
              @#{plural}.replace(elements.to_a)
            end
          EOC
        end
def_classed_element(name, class_name=nil, attribute_name=nil) click to toggle source
# File lib/rss/maker/base.rb, line 94
        def def_classed_element(name, class_name=nil, attribute_name=nil)
          def_classed_element_without_accessor(name, class_name)
          if attribute_name
            module_eval(<<-EOC, __FILE__, __LINE__ + 1)
              def #{name}
                if block_given?
                  yield(@#{name})
                else
                  @#{name}.#{attribute_name}
                end
              end

              def #{name}=(new_value)
                @#{name}.#{attribute_name} = new_value
              end
            EOC
          else
            attr_reader name
          end
        end
def_classed_element_without_accessor(name, class_name=nil) click to toggle source
# File lib/rss/maker/base.rb, line 76
        def def_classed_element_without_accessor(name, class_name=nil)
          class_name ||= Utils.to_class_name(name)
          add_other_element(name)
          add_need_initialize_variable(name) do |object|
            object.send("make_#{name}")
          end
          module_eval(<<-EOC, __FILE__, __LINE__ + 1)
            private
            def setup_#{name}(feed, current)
              @#{name}.to_feed(feed, current)
            end

            def make_#{name}
              self.class::#{class_name}.new(@maker)
            end
          EOC
        end
def_classed_elements(name, attribute, plural_class_name=nil, plural_name=nil, new_name=nil) { |local_variable_name, new_value_variable_name| ... } click to toggle source
# File lib/rss/maker/base.rb, line 115
        def def_classed_elements(name, attribute, plural_class_name=nil,
                                 plural_name=nil, new_name=nil)
          plural_name ||= "#{name}s"
          new_name ||= name
          def_classed_element(plural_name, plural_class_name)
          local_variable_name = "_#{name}"
          new_value_variable_name = "new_value"
          additional_setup_code = nil
          if block_given?
            additional_setup_code = yield(local_variable_name,
                                          new_value_variable_name)
          end
          module_eval(<<-EOC, __FILE__, __LINE__ + 1)
            def #{name}
              #{local_variable_name} = #{plural_name}.first
              #{local_variable_name} ? #{local_variable_name}.#{attribute} : nil
            end

            def #{name}=(#{new_value_variable_name})
              #{local_variable_name} =
                #{plural_name}.first || #{plural_name}.new_#{new_name}
              #{additional_setup_code}
              #{local_variable_name}.#{attribute} = #{new_value_variable_name}
            end
          EOC
        end
def_csv_element(name, type=nil) click to toggle source
# File lib/rss/maker/base.rb, line 159
        def def_csv_element(name, type=nil)
          def_other_element_without_accessor(name)
          attr_reader(name)
          converter = ""
          if type == :integer
            converter = "{|v| Integer(v)}"
          end
          module_eval(<<-EOC, __FILE__, __LINE__ + 1)
            def #{name}=(value)
              @#{name} = Utils::CSV.parse(value)#{converter}
            end
          EOC
        end
def_other_element(name) click to toggle source
# File lib/rss/maker/base.rb, line 142
def def_other_element(name)
  attr_accessor name
  def_other_element_without_accessor(name)
end
def_other_element_without_accessor(name) click to toggle source
# File lib/rss/maker/base.rb, line 147
        def def_other_element_without_accessor(name)
          add_need_initialize_variable(name)
          add_other_element(name)
          module_eval(<<-EOC, __FILE__, __LINE__ + 1)
            def setup_#{name}(feed, current)
              if !@#{name}.nil? and current.respond_to?(:#{name}=)
                current.#{name} = @#{name}
              end
            end
          EOC
        end
inherited(subclass) click to toggle source
# File lib/rss/maker/base.rb, line 25
def inherited(subclass)
  subclass.const_set(:OTHER_ELEMENTS, [])
  subclass.const_set(:NEED_INITIALIZE_VARIABLES, [])
end
inherited_base() click to toggle source
# File lib/rss/maker/base.rb, line 21
def inherited_base
  ::RSS::Maker::Base
end
need_initialize_variables() click to toggle source
# File lib/rss/maker/base.rb, line 17
def need_initialize_variables
  inherited_array_reader("NEED_INITIALIZE_VARIABLES")
end
new(maker) click to toggle source
# File lib/rss/maker/base.rb, line 175
def initialize(maker)
  @maker = maker
  @default_values_are_set = false
  initialize_variables
end
other_elements() click to toggle source
# File lib/rss/maker/base.rb, line 14
def other_elements
  inherited_array_reader("OTHER_ELEMENTS")
end

Public Instance Methods

have_required_values?() click to toggle source
# File lib/rss/maker/base.rb, line 181
def have_required_values?
  not_set_required_variables.empty?
end
variable_is_set?() click to toggle source
# File lib/rss/maker/base.rb, line 185
def variable_is_set?
  variables.any? {|var| not __send__(var).nil?}
end

Private Instance Methods

_set_default_values() { || ... } click to toggle source
# File lib/rss/maker/base.rb, line 230
def _set_default_values(&block)
  yield
end
current_element(feed) click to toggle source
# File lib/rss/maker/base.rb, line 215
def current_element(feed)
  feed
end
initialize_variables() click to toggle source
# File lib/rss/maker/base.rb, line 190
def initialize_variables
  self.class.need_initialize_variables.each do |variable_name, init_value|
    if init_value.nil?
      value = nil
    else
      if init_value.respond_to?(:call)
        value = init_value.call(self)
      elsif init_value.is_a?(String)
        # just for backward compatibility
        value = instance_eval(init_value, __FILE__, __LINE__)
      else
        value = init_value
      end
    end
    instance_variable_set("@#{variable_name}", value)
  end
end
not_set_required_variables() click to toggle source
# File lib/rss/maker/base.rb, line 264
def not_set_required_variables
  required_variable_names.find_all do |var|
    __send__(var).nil?
  end
end
required_variables_are_set?() click to toggle source
# File lib/rss/maker/base.rb, line 270
def required_variables_are_set?
  required_variable_names.each do |var|
    return false if __send__(var).nil?
  end
  true
end
set_default_values() { || ... } click to toggle source
# File lib/rss/maker/base.rb, line 219
def set_default_values(&block)
  return yield if @default_values_are_set

  begin
    @default_values_are_set = true
    _set_default_values(&block)
  ensure
    @default_values_are_set = false
  end
end
set_parent(target, parent) click to toggle source
# File lib/rss/maker/base.rb, line 251
def set_parent(target, parent)
  target.parent = parent if target.class.need_parent?
end
setup_other_elements(feed, current=nil) click to toggle source
# File lib/rss/maker/base.rb, line 208
def setup_other_elements(feed, current=nil)
  current ||= current_element(feed)
  self.class.other_elements.each do |element|
    __send__("setup_#{element}", feed, current)
  end
end
setup_values(target) click to toggle source
# File lib/rss/maker/base.rb, line 234
def setup_values(target)
  set = false
  if have_required_values?
    variables.each do |var|
      setter = "#{var}="
      if target.respond_to?(setter)
        value = __send__(var)
        unless value.nil?
          target.__send__(setter, value)
          set = true
        end
      end
    end
  end
  set
end
variables() click to toggle source
# File lib/rss/maker/base.rb, line 255
def variables
  self.class.need_initialize_variables.find_all do |name, init|
    # init == "nil" is just for backward compatibility
    init.nil? or init == "nil"
  end.collect do |name, init|
    name
  end
end