123456789_123456789_123456789_123456789_123456789_

Class: RSS::Maker::Base

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
ChannelBase, ImageBase, ItemsBase, RSS09, RSS091, RSS092, RSS10, RSS20, RSSBase, TextinputBase, XMLStyleSheets, Atom::Entry, Atom::Feed, ChannelBase::AuthorsBase, ChannelBase::CategoriesBase, ChannelBase::CloudBase, ChannelBase::ContributorsBase, ChannelBase::CopyrightBase, ChannelBase::DescriptionBase, ChannelBase::GeneratorBase, ChannelBase::LinksBase, ChannelBase::SkipDaysBase, ChannelBase::SkipHoursBase, ChannelBase::TitleBase, ITunesChannelModel::ITunesCategoriesBase, ITunesChannelModel::ITunesImageBase, ITunesChannelModel::ITunesOwnerBase, ITunesItemModel::ITunesDurationBase, ITunesItemModel::ITunesImageBase, ImageFaviconModel::ImageFaviconBase, ImageItemModel::ImageItemBase, ItemsBase::ItemBase, RSS091::Channel, RSS091::Image, RSS091::Items, RSS091::Textinput, RSS092::Channel, RSS092::Image, RSS092::Items, RSS092::Textinput, RSS09::Channel, RSS09::Image, RSS09::Items, RSS09::Textinput, RSS10::Channel, RSS10::Image, RSS10::Items, RSS10::Textinput, RSS20::Channel, RSS20::Image, RSS20::Items, RSS20::Textinput, TaxonomyTopicModel::TaxonomyTopicsBase, TaxonomyTopicsModel::TaxonomyTopicsBase, TrackBackModel::TrackBackAboutsBase, XMLStyleSheets::XMLStyleSheet, Atom::Entry::Channel, Atom::Entry::Image, Atom::Entry::Items, Atom::Entry::Textinput, Atom::Feed::Channel, Atom::Feed::Image, Atom::Feed::Items, Atom::Feed::Textinput, ChannelBase::AuthorsBase::AuthorBase, ChannelBase::CategoriesBase::CategoryBase, ChannelBase::ContributorsBase::ContributorBase, ChannelBase::LinksBase::LinkBase, ChannelBase::SkipDaysBase::DayBase, ChannelBase::SkipHoursBase::HourBase, ITunesChannelModel::ITunesCategoriesBase::ITunesCategoryBase, ItemsBase::ItemBase::ContentBase, ItemsBase::ItemBase::DescriptionBase, ItemsBase::ItemBase::EnclosureBase, ItemsBase::ItemBase::GuidBase, ItemsBase::ItemBase::RightsBase, ItemsBase::ItemBase::SourceBase, ItemsBase::ItemBase::TitleBase, TaxonomyTopicModel::TaxonomyTopicsBase::TaxonomyTopicBase, TrackBackModel::TrackBackAboutsBase::TrackBackAboutBase, ItemsBase::ItemBase::SourceBase::IconBase, ItemsBase::ItemBase::SourceBase::LogoBase, ItemsBase::ItemBase::SourceBase::RightsBase, ItemsBase::ItemBase::SourceBase::SubtitleBase, ItemsBase::ItemBase::SourceBase::TitleBase
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Inherits: Object
Defined in: lib/rss/maker/base.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(maker) ⇒ Base

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 176

def initialize(maker)
  @maker = maker
  @default_values_are_set = false
  initialize_variables
end

Class Method Details

.add_need_initialize_variable(variable_name, init_value = nil, &init_block)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 35

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)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 31

def add_other_element(variable_name)
  self::OTHER_ELEMENTS << variable_name
end

.def_array_element(name, plural = nil, klass_name = nil)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 41

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)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 95

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)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 77

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)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 116

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)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 160

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)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 143

def def_other_element(name)
  attr_accessor name
  def_other_element_without_accessor(name)
end

.def_other_element_without_accessor(name)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 148

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)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 26

def inherited(subclass)
  subclass.const_set(:OTHER_ELEMENTS, [])
  subclass.const_set(:NEED_INITIALIZE_VARIABLES, [])
end

.inherited_base

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 22

def inherited_base
  ::RSS::Maker::Base
end

.need_initialize_variables

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 18

def need_initialize_variables
  inherited_array_reader("NEED_INITIALIZE_VARIABLES")
end

.other_elements

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 15

def other_elements
  inherited_array_reader("OTHER_ELEMENTS")
end

Instance Attribute Details

#have_required_values?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 182

def have_required_values?
  not_set_required_variables.empty?
end

#maker (readonly)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 175

attr_reader :maker

#required_variables_are_set?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 271

def required_variables_are_set?
  required_variable_names.each do |var|
    return false if __send__(var).nil?
  end
  true
end

#variable_is_set?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 186

def variable_is_set?
  variables.any? {|var| not __send__(var).nil?}
end

Instance Method Details

#_set_default_values(&block) (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 231

def _set_default_values(&block)
  yield
end

#current_element(feed) (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 216

def current_element(feed)
  feed
end

#initialize_variables (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 191

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 (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 265

def not_set_required_variables
  required_variable_names.find_all do |var|
    __send__(var).nil?
  end
end

#set_default_values(&block) (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 220

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) (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 252

def set_parent(target, parent)
  target.parent = parent if target.class.need_parent?
end

#setup_other_elements(feed, current = nil) (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 209

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) (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 235

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 (private)

[ GitHub ]

  
# File 'lib/rss/maker/base.rb', line 256

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