Sha256: 2b0601d32de96eddfeaee6b47285414ea21166724a550d66d750ba7933c9c1f6

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Ing
  module Util
    extend self
    
    def decode_class(str, base=::Object)
      namespaced_const_get( decode_class_names(str), base )
    end
    
    def decode_class_names(str)
      str.split(':').map {|c| c.gsub(/(?:\A|_+)(\w)/) {$1.upcase} }
    end    
    
    def encode_class(klass)
      encode_class_names(klass.to_s.split('::'))
    end
    
    def encode_class_names(list)
      list.map {|c| c.to_s.gsub(/([A-Z])/) {
          ($`.empty? ? "" : "_") + $1.downcase
        } 
      }.join(':')
    end
        
    def namespaced_const_get(list, base=::Object)
      list.inject(base) {|m, klass| m.const_get(klass, false)}
    end
    
    def option?(arg)
      !!(/^-{1,2}/ =~ arg)
    end
    
    # not used
    def split_method_args(args)
      if option?(args.first)
        [nil, args]
      else
        [args.first, args[1..-1]]
      end
    end    
    
    # Returns a string that has had any glob characters escaped.
    # The glob characters are `* ? { } [ ]`.
    #
    # ==== Examples
    #
    #   Util.escape_globs('[apps]')   # => '\[apps\]'
    #
    # ==== Parameters
    # String
    #
    # ==== Returns
    # String
    #
    def escape_globs(path)
      path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ing-0.2.1 lib/ing/util.rb