Sha256: 6b5fc3992d85b342061777dcf746d20ef389765410bab04c202e1fccf4b69e81

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# Pull in some AS String utilities (not loaded if AS is available)
unless Object.const_defined?('ActiveSupport')
  class String
    def camelize(first_letter = :upper)
      case first_letter
        when :upper then Garb::Inflector.camelize(self, true)
        when :lower then Garb::Inflector.camelize(self, false)
      end
    end
    alias_method :camelcase, :camelize

    def underscore
      Garb::Inflector.underscore(self)
    end

    def demodulize
      Garb::Inflector.demodulize(self)
    end
  end

  module Garb
    module Inflector
      extend self

      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 demodulize(class_name_in_module)
        class_name_in_module.to_s.gsub(/^.*::/, '')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
garb-0.9.8 lib/garb/core_ext/string.rb
garb-0.9.7 lib/garb/core_ext/string.rb