module RSS::BaseModel

Public Instance Methods

install_date_element(tag_name, uri, occurs, name=nil, type=nil, disp_name=nil) click to toggle source
# File lib/rss/rss.rb, line 271
    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_have_attribute_element(tag_name, uri, occurs, name=nil, type=nil)
install_have_child_element(tag_name, uri, occurs, name=nil, type=nil) click to toggle source
# File lib/rss/rss.rb, line 197
    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) click to toggle source
# File lib/rss/rss.rb, line 217
    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) click to toggle source
# File lib/rss/rss.rb, line 237
    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

Private Instance Methods

boolean_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 496
    def boolean_writer(name, disp_name=name)
      module_eval("      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
", *get_file_and_line_from_caller(2))
    end
content_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 528
    def content_writer(name, disp_name=name)
      klass_name = "self.class::#{Utils.to_class_name(name)}"
      module_eval("      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
", *get_file_and_line_from_caller(2))
    end
convert_attr_reader(*attrs) click to toggle source
# File lib/rss/rss.rb, line 358
    def convert_attr_reader(*attrs)
      attrs.each do |attr|
        attr = attr.id2name if attr.kind_of?(Integer)
        module_eval("        def #{attr}
          convert(@#{attr})
        end
", *get_file_and_line_from_caller(2))
      end
    end
csv_attr_reader(*attrs) click to toggle source
# File lib/rss/rss.rb, line 393
    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("          attr_reader(:#{attr})
          def #{attr}_content
            if @#{attr}.nil?
              @#{attr}
            else
              @#{attr}.join(#{separator.dump})
            end
          end
", __FILE__, __LINE__ + 1)
      end
    end
csv_integer_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 570
    def csv_integer_writer(name, disp_name=name)
      module_eval("        def #{name}=(new_value)
          @#{name} = Utils::CSV.parse(new_value) {|v| Integer(v)}
        end
", __FILE__, __LINE__ + 1)
    end
csv_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 562
    def csv_writer(name, disp_name=name)
      module_eval("        def #{name}=(new_value)
          @#{name} = Utils::CSV.parse(new_value)
        end
", __FILE__, __LINE__ + 1)
    end
date_writer(name, type, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 415
    def date_writer(name, type, disp_name=name)
      module_eval("      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
", *get_file_and_line_from_caller(2))
    end
def_children_accessor(accessor_name, plural_name) click to toggle source
# File lib/rss/rss.rb, line 578
    def def_children_accessor(accessor_name, plural_name)
      module_eval("      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}=)
", *get_file_and_line_from_caller(2))
    end
inherit_convert_attr_reader(*attrs) click to toggle source
# File lib/rss/rss.rb, line 316
    def inherit_convert_attr_reader(*attrs)
      attrs.each do |attr|
        attr = attr.id2name if attr.kind_of?(Integer)
        module_eval("        def #{attr}_without_inherit
          convert(@#{attr})
        end

        def #{attr}
          if @#{attr}
            #{attr}_without_inherit
          elsif @parent
            @parent.#{attr}
          else
            nil
          end
        end
", *get_file_and_line_from_caller(2))
      end
    end
install_element(name, postfix="") click to toggle source
# File lib/rss/rss.rb, line 304
    def install_element(name, postfix="")
      elem_name = name.sub('_', ':')
      method_name = "#{name}_element#{postfix}"
      add_to_element_method(method_name)
      module_eval("      def #{method_name}(need_convert=true, indent='')
        #{yield(name, elem_name)}
      end
      private :#{method_name}
", *get_file_and_line_from_caller(2))
    end
integer_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 454
    def integer_writer(name, disp_name=name)
      module_eval("      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
", *get_file_and_line_from_caller(2))
    end
positive_integer_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 474
    def positive_integer_writer(name, disp_name=name)
      module_eval("      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
", *get_file_and_line_from_caller(2))
    end
text_type_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 516
    def text_type_writer(name, disp_name=name)
      module_eval("      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
", *get_file_and_line_from_caller(2))
    end
uri_convert_attr_reader(*attrs) click to toggle source
# File lib/rss/rss.rb, line 337
    def uri_convert_attr_reader(*attrs)
      attrs.each do |attr|
        attr = attr.id2name if attr.kind_of?(Integer)
        module_eval("        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
", *get_file_and_line_from_caller(2))
      end
    end
yes_clean_other_attr_reader(*attrs) click to toggle source
# File lib/rss/rss.rb, line 369
    def yes_clean_other_attr_reader(*attrs)
      attrs.each do |attr|
        attr = attr.id2name if attr.kind_of?(Integer)
        module_eval("          attr_reader(:#{attr})
          def #{attr}?
            YesCleanOther.parse(@#{attr})
          end
", __FILE__, __LINE__ + 1)
      end
    end
yes_clean_other_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 542
    def yes_clean_other_writer(name, disp_name=name)
      module_eval("        def #{name}=(value)
          value = (value ? "yes" : "no") if [true, false].include?(value)
          @#{name} = value
        end
", __FILE__, __LINE__ + 1)
    end
yes_other_attr_reader(*attrs) click to toggle source
# File lib/rss/rss.rb, line 381
    def yes_other_attr_reader(*attrs)
      attrs.each do |attr|
        attr = attr.id2name if attr.kind_of?(Integer)
        module_eval("          attr_reader(:#{attr})
          def #{attr}?
            Utils::YesOther.parse(@#{attr})
          end
", __FILE__, __LINE__ + 1)
      end
    end
yes_other_writer(name, disp_name=name) click to toggle source
# File lib/rss/rss.rb, line 551
    def yes_other_writer(name, disp_name=name)
      module_eval("        def #{name}=(new_value)
          if [true, false].include?(new_value)
            new_value = new_value ? "yes" : "no"
          end
          @#{name} = new_value
        end
", __FILE__, __LINE__ + 1)
    end