123456789_123456789_123456789_123456789_123456789_

Module: RSS::BaseModel

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Atom::Entry, Atom::Feed, Atom::Feed::Author, Atom::Feed::Category, Atom::Feed::Contributor, Atom::Feed::Entry, Atom::Feed::Entry::Content, Atom::Feed::Entry::Published, Atom::Feed::Entry::Source, Atom::Feed::Entry::Summary, Atom::Feed::Generator, Atom::Feed::Icon, Atom::Feed::Id, Atom::Feed::Link, Atom::Feed::Logo, Atom::Feed::Rights, Atom::Feed::Subtitle, Atom::Feed::Title, Atom::Feed::Updated, Atom::PersonConstruct::Email, Atom::PersonConstruct::Name, Atom::PersonConstruct::Uri, ContentModel, DublinCoreModel, Element, ITunesChannelModel, ITunesChannelModel::ITunesCategory, ITunesChannelModel::ITunesImage, ITunesChannelModel::ITunesOwner, ITunesItemModel, ITunesItemModel::ITunesDuration, ImageFaviconModel, ImageFaviconModel::ImageFavicon, ImageItemModel, ImageItemModel::ImageItem, RDF, RDF::Bag, RDF::Channel, RDF::Channel::Image, RDF::Channel::Items, RDF::Channel::Textinput, RDF::Image, RDF::Item, RDF::Li, RDF::Seq, RDF::Textinput, Rss, Rss::Channel, Rss::Channel::Cloud, Rss::Channel::Image, Rss::Channel::Item, Rss::Channel::Item::Category, Rss::Channel::Item::Enclosure, Rss::Channel::Item::Guid, Rss::Channel::Item::Source, Rss::Channel::SkipDays, Rss::Channel::SkipDays::Day, Rss::Channel::SkipHours, Rss::Channel::SkipHours::Hour, Rss::Channel::TextInput, SlashModel, SyndicationModel, TaxonomyTopicModel, TaxonomyTopicModel::TaxonomyTopic, TaxonomyTopicsModel, TaxonomyTopicsModel::TaxonomyTopics, RSS::TrackBackModel10, RSS::TrackBackModel10::TrackBackAbout, RSS::TrackBackModel10::TrackBackPing, RSS::TrackBackModel20, RSS::TrackBackModel20::TrackBackAbout, RSS::TrackBackModel20::TrackBackPing
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Utils
Defined in: lib/rss/rss.rb

Instance Method Summary

Utils - Included

#element_initialize_arguments?

This method is used inside of several different objects to determine if special behavior is needed in the constructor.

#get_file_and_line_from_caller

Returns an array of two elements: the filename where the calling method is located, and the line number where it is defined.

#h,
#html_escape

Takes a string s with some HTML in it, and escapes '&', '“', '<' and '>', by replacing them with the appropriate entities.

#new_with_value_if_need

If value is an instance of class klass, return it, else create a new instance of klass with value value.

#to_class_name

Given a name in a name_with_underscores or a name-with-dashes format, returns the CamelCase version of name.

Instance Method Details

#boolean_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 507

def boolean_writer(name, disp_name=name)
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{name}=(new_value)
    if new_value.nil?
      @#{name} = new_value
    else
      if @do_validate and
          ![true, false, "true", "false"].include?(new_value)
        raise NotAvailableValueError.new('#{disp_name}', new_value)
      end
      if [true, false].include?(new_value)
        @#{name} = new_value
      else
        @#{name} = new_value == "true"
      end
    end
  end
EOC
end

#content_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 539

def content_writer(name, disp_name=name)
  klass_name = "self.class::#{Utils.to_class_name(name)}"
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{name}=(new_value)
    if new_value.is_a?(#{klass_name})
      @#{name} = new_value
    else
      @#{name} = #{klass_name}.new
      @#{name}.content = new_value
    end
  end
EOC
end

#convert_attr_reader(*attrs) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 369

def convert_attr_reader(*attrs)
  attrs.each do |attr|
    attr = attr.id2name if attr.kind_of?(Integer)
    module_eval(<<-EOC, *get_file_and_line_from_caller(2))
    def #{attr}
      convert(@#{attr})
    end
EOC
  end
end

#csv_attr_reader(*attrs) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 404

def csv_attr_reader(*attrs)
  separator = nil
  if attrs.last.is_a?(Hash)
    options = attrs.pop
    separator = options[:separator]
  end
  separator ||= ", "
  attrs.each do |attr|
    attr = attr.id2name if attr.kind_of?(Integer)
    module_eval(<<-EOC, __FILE__, __LINE__ + 1)
      attr_reader(:#{attr})
      def #{attr}_content
        if @#{attr}.nil?
          @#{attr}
        else
          @#{attr}.join(#{separator.dump})
        end
      end
    EOC
  end
end

#csv_integer_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 581

def csv_integer_writer(name, disp_name=name)
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def #{name}=(new_value)
      @#{name} = Utils::CSV.parse(new_value) {|v| Integer(v)}
    end
  EOC
end

#csv_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 573

def csv_writer(name, disp_name=name)
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def #{name}=(new_value)
      @#{name} = Utils::CSV.parse(new_value)
    end
  EOC
end

#date_writer(name, type, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 426

def date_writer(name, type, disp_name=name)
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{name}=(new_value)
    if new_value.nil?
      @#{name} = new_value
    elsif new_value.kind_of?(Time)
      @#{name} = new_value.dup
    else
      if @do_validate
        begin
          @#{name} = Time.__send__('#{type}', new_value)
        rescue ArgumentError
          raise NotAvailableValueError.new('#{disp_name}', new_value)
        end
      else
        @#{name} = nil
        if /\\A\\s*\\z/ !~ new_value.to_s
          begin
            unless Date._parse(new_value, false).empty?
              @#{name} = Time.parse(new_value)
            end
          rescue ArgumentError
          end
        end
      end
    end

    # Is it need?
    if @#{name}
      class << @#{name}
        undef_method(:to_s)
        alias_method(:to_s, :#{type})
      end
    end

  end
EOC
end

#def_children_accessor(accessor_name, plural_name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 589

def def_children_accessor(accessor_name, plural_name)
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{plural_name}
    @#{accessor_name}
  end

  def #{accessor_name}(*args)
    if args.empty?
      @#{accessor_name}.first
    else
      @#{accessor_name}[*args]
    end
  end

  def #{accessor_name}=(*args)
    receiver = self.class.name
    warn("Warning:\#{caller.first.sub(/:in `.*'\z/, '')}: " \
         "Don't use `\#{receiver}\##{accessor_name} = XXX'/" \
         "`\#{receiver}\#set_#{accessor_name}(XXX)'. " \
         "Those APIs are not sense of Ruby. " \
         "Use `\#{receiver}\##{plural_name} << XXX' instead of them.")
    if args.size == 1
      @#{accessor_name}.push(args[0])
    else
      @#{accessor_name}.__send__("[]=", *args)
    end
  end
  alias_method(:set_#{accessor_name}, :#{accessor_name}=)
EOC
end

#inherit_convert_attr_reader(*attrs) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 327

def inherit_convert_attr_reader(*attrs)
  attrs.each do |attr|
    attr = attr.id2name if attr.kind_of?(Integer)
    module_eval(<<-EOC, *get_file_and_line_from_caller(2))
    def #{attr}_without_inherit
      convert(@#{attr})
    end

    def #{attr}
      if @#{attr}
        #{attr}_without_inherit
      elsif @parent
        @parent.#{attr}
      else
        nil
      end
    end
EOC
  end
end

#install_date_element(tag_name, uri, occurs, name = nil, type = nil, disp_name = nil)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 282

def install_date_element(tag_name, uri, occurs, name=nil, type=nil, disp_name=nil)
  name ||= tag_name
  type ||= :w3cdtf
  disp_name ||= name
  self::ELEMENTS << name
  add_need_initialize_variable(name)
  install_model(tag_name, uri, occurs, name)

  # accessor
  convert_attr_reader name
  date_writer(name, type, disp_name)

  install_element(name) do |n, elem_name|
    <<-EOC
    if @#{n}
      rv = "\#{indent}<#{elem_name}>"
      value = html_escape(@#{n}.#{type})
      if need_convert
        rv << convert(value)
      else
        rv << value
      end
        rv << "</#{elem_name}>"
      rv
    else
      ''
    end
EOC
  end

end

#install_element(name, postfix = "") (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 315

def install_element(name, postfix="")
  elem_name = name.sub('_', ':')
  method_name = "#{name}_element#{postfix}"
  add_to_element_method(method_name)
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{method_name}(need_convert=true, indent='')
    #{yield(name, elem_name)}
  end
  private :#{method_name}
EOC
end

#install_have_attribute_element(tag_name, uri, occurs, name = nil, type = nil)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 226

alias_method(:install_have_attribute_element, :install_have_child_element)

#install_have_child_element(tag_name, uri, occurs, name = nil, type = nil) Also known as: #install_have_attribute_element

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 208

def install_have_child_element(tag_name, uri, occurs, name=nil, type=nil)
  name ||= tag_name
  add_need_initialize_variable(name)
  install_model(tag_name, uri, occurs, name)

  writer_type, reader_type = type
  def_corresponded_attr_writer name, writer_type
  def_corresponded_attr_reader name, reader_type
  install_element(name) do |n, elem_name|
    <<-EOC
    if @#{n}
      "\#{@#{n}.to_s(need_convert, indent)}"
    else
      ''
    end
EOC
  end
end

#install_have_children_element(tag_name, uri, occurs, name = nil, plural_name = nil)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 228

def install_have_children_element(tag_name, uri, occurs, name=nil, plural_name=nil)
  name ||= tag_name
  plural_name ||= "#{name}s"
  add_have_children_element(name, plural_name)
  add_plural_form(name, plural_name)
  install_model(tag_name, uri, occurs, plural_name, true)

  def_children_accessor(name, plural_name)
  install_element(name, "s") do |n, elem_name|
    <<-EOC
    rv = []
    @#{n}.each do |x|
      value = "\#{x.to_s(need_convert, indent)}"
      rv << value if /\\A\\s*\\z/ !~ value
    end
    rv.join("\n")
EOC
  end
end

#install_text_element(tag_name, uri, occurs, name = nil, type = nil, disp_name = nil)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 248

def install_text_element(tag_name, uri, occurs, name=nil, type=nil,
                         disp_name=nil)
  name ||= tag_name
  disp_name ||= name
  self::ELEMENTS << name unless self::ELEMENTS.include?(name)
  add_need_initialize_variable(name)
  install_model(tag_name, uri, occurs, name)

  def_corresponded_attr_writer(name, type, disp_name)
  def_corresponded_attr_reader(name, type || :convert)
  install_element(name) do |n, elem_name|
    <<-EOC
    if respond_to?(:#{n}_content)
      content = #{n}_content
    else
      content = @#{n}
    end
    if content
      rv = "\#{indent}<#{elem_name}>"
      value = html_escape(content)
      if need_convert
        rv << convert(value)
      else
        rv << value
      end
        rv << "</#{elem_name}>"
      rv
    else
      ''
    end
EOC
  end
end

#integer_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 465

def integer_writer(name, disp_name=name)
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{name}=(new_value)
    if new_value.nil?
      @#{name} = new_value
    else
      if @do_validate
        begin
          @#{name} = Integer(new_value)
        rescue ArgumentError
          raise NotAvailableValueError.new('#{disp_name}', new_value)
        end
      else
        @#{name} = new_value.to_i
      end
    end
  end
EOC
end

#positive_integer_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 485

def positive_integer_writer(name, disp_name=name)
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{name}=(new_value)
    if new_value.nil?
      @#{name} = new_value
    else
      if @do_validate
        begin
          tmp = Integer(new_value)
          raise ArgumentError if tmp <= 0
          @#{name} = tmp
        rescue ArgumentError
          raise NotAvailableValueError.new('#{disp_name}', new_value)
        end
      else
        @#{name} = new_value.to_i
      end
    end
  end
EOC
end

#text_type_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 527

def text_type_writer(name, disp_name=name)
  module_eval(<<-EOC, *get_file_and_line_from_caller(2))
  def #{name}=(new_value)
    if @do_validate and
        !["text", "html", "xhtml", nil].include?(new_value)
      raise NotAvailableValueError.new('#{disp_name}', new_value)
    end
    @#{name} = new_value
  end
EOC
end

#uri_convert_attr_reader(*attrs) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 348

def uri_convert_attr_reader(*attrs)
  attrs.each do |attr|
    attr = attr.id2name if attr.kind_of?(Integer)
    module_eval(<<-EOC, *get_file_and_line_from_caller(2))
    def #{attr}_without_base
      convert(@#{attr})
    end

    def #{attr}
      value = #{attr}_without_base
      return nil if value.nil?
      if /\\A[a-z][a-z0-9+.\\-]*:/i =~ value
        value
      else
        "\#{base}\#{value}"
      end
    end
EOC
  end
end

#yes_clean_other_attr_reader(*attrs) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 380

def yes_clean_other_attr_reader(*attrs)
  attrs.each do |attr|
    attr = attr.id2name if attr.kind_of?(Integer)
    module_eval(<<-EOC, __FILE__, __LINE__ + 1)
      attr_reader(:#{attr})
      def #{attr}?
        YesCleanOther.parse(@#{attr})
      end
    EOC
  end
end

#yes_clean_other_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 553

def yes_clean_other_writer(name, disp_name=name)
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def #{name}=(value)
      value = (value ? "yes" : "no") if [true, false].include?(value)
      @#{name} = value
    end
  EOC
end

#yes_other_attr_reader(*attrs) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 392

def yes_other_attr_reader(*attrs)
  attrs.each do |attr|
    attr = attr.id2name if attr.kind_of?(Integer)
    module_eval(<<-EOC, __FILE__, __LINE__ + 1)
      attr_reader(:#{attr})
      def #{attr}?
        Utils::YesOther.parse(@#{attr})
      end
    EOC
  end
end

#yes_other_writer(name, disp_name = name) (private)

[ GitHub ]

  
# File 'lib/rss/rss.rb', line 562

def yes_other_writer(name, disp_name=name)
  module_eval(<<-EOC, __FILE__, __LINE__ + 1)
    def #{name}=(new_value)
      if [true, false].include?(new_value)
        new_value = new_value ? "yes" : "no"
      end
      @#{name} = new_value
    end
  EOC
end