Sha256: ccb6c1b2f105bff28ed96cd0093d0507d8e37efc6f7c2342762006a9e1a8d805

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module Beatport
  module Inflector
    def self.constantize(camel_cased_word)
      names = camel_cased_word.split('::')
      names.shift if names.empty? || names.first.empty?

      constant = Object
      names.each do |name|
        constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
      end
      constant
    end
    
    def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
      if first_letter_in_uppercase
        lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
      else
        lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
      end
    end    
    
    def self.underscore(camel_cased_word)
      word = camel_cased_word.to_s.dup
      word.gsub!(/::/, '/')
      word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
      word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
      word.tr!("-", "_")
      word.downcase!
      word
    end
    
    def self.process_keys(obj, &block)
      case obj
      when Hash
        Hash[obj.map {|k, v| [yield(k), process_keys(v, &block)]}]
      when Array
        obj.map {|o| process_keys(o, &block)}
      else
        obj
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
beatport-0.1.1 lib/beatport/inflector.rb