Sha256: 5e7ff5220ab55411a27b0d3b8d1e9a500392fd51450a5fe22d19d7921b929449

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

Contents

module Beatport
  module Support
    module Inflector
      extend self
      
      def 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 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 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 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
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
beatport-0.4.0 lib/beatport/support/inflector.rb
beatport-0.3.0 lib/beatport/support/inflector.rb
beatport-0.2.3 lib/beatport/support/inflector.rb
beatport-0.2.2 lib/beatport/support/inflector.rb
beatport-0.2.1 lib/beatport/support/inflector.rb
beatport-0.2.0 lib/beatport/support/inflector.rb
beatport-0.1.10 lib/beatport/support/inflector.rb
beatport-0.1.9 lib/beatport/support/inflector.rb
beatport-0.1.8 lib/beatport/support/inflector.rb
beatport-0.1.7 lib/beatport/support/inflector.rb
beatport-0.1.6 lib/beatport/support/inflector.rb
beatport-0.1.5 lib/beatport/support/inflector.rb
beatport-0.1.3 lib/beatport/support/inflector.rb
beatport-0.1.2 lib/beatport/support/inflector.rb