PageRenderTime 950ms CodeModel.GetById 938ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/rss/utils.rb

http://github.com/agross/netopenspace
Ruby | 111 lines | 106 code | 3 blank | 2 comment | 1 complexity | 2e65d8682b420b19d463cb6561fb0cc0 MD5 | raw file
  1module RSS
  2  module Utils
  3    module_function
  4
  5    # Convert a name_with_underscores to CamelCase.
  6    def to_class_name(name)
  7      name.split(/[_\-]/).collect do |part|
  8        "#{part[0, 1].upcase}#{part[1..-1]}"
  9      end.join("")
 10    end
 11    
 12    def get_file_and_line_from_caller(i=0)
 13      file, line, = caller[i].split(':')
 14      line = line.to_i
 15      line += 1 if i.zero?
 16      [file, line]
 17    end
 18
 19    # escape '&', '"', '<' and '>' for use in HTML.
 20    def html_escape(s)
 21      s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
 22    end
 23    alias h html_escape
 24    
 25    # If +value+ is an instance of class +klass+, return it, else
 26    # create a new instance of +klass+ with value +value+.
 27    def new_with_value_if_need(klass, value)
 28      if value.is_a?(klass)
 29        value
 30      else
 31        klass.new(value)
 32      end
 33    end
 34
 35    def element_initialize_arguments?(args)
 36      [true, false].include?(args[0]) and args[1].is_a?(Hash)
 37    end
 38
 39    module YesCleanOther
 40      module_function
 41      def parse(value)
 42        if [true, false, nil].include?(value)
 43          value
 44        else
 45          case value.to_s
 46          when /\Ayes\z/i
 47            true
 48          when /\Aclean\z/i
 49            false
 50          else
 51            nil
 52          end
 53        end
 54      end
 55    end
 56
 57    module YesOther
 58      module_function
 59      def parse(value)
 60        if [true, false].include?(value)
 61          value
 62        else
 63          /\Ayes\z/i.match(value.to_s) ? true : false
 64        end
 65      end
 66    end
 67
 68    module CSV
 69      module_function
 70      def parse(value, &block)
 71        if value.is_a?(String)
 72          value = value.strip.split(/\s*,\s*/)
 73          value = value.collect(&block) if block_given?
 74          value
 75        else
 76          value
 77        end
 78      end
 79    end
 80
 81    module InheritedReader
 82      def inherited_reader(constant_name)
 83        base_class = inherited_base
 84        result = base_class.const_get(constant_name)
 85        found_base_class = false
 86        ancestors.reverse_each do |klass|
 87          if found_base_class
 88            if klass.const_defined?(constant_name)
 89              result = yield(result, klass.const_get(constant_name))
 90            end
 91          else
 92            found_base_class = klass == base_class
 93          end
 94        end
 95        result
 96      end
 97
 98      def inherited_array_reader(constant_name)
 99        inherited_reader(constant_name) do |result, current|
100          current + result
101        end
102      end
103
104      def inherited_hash_reader(constant_name)
105        inherited_reader(constant_name) do |result, current|
106          result.merge(current)
107        end
108      end
109    end
110  end
111end