Sha256: 2cff6670066c9f7307a6daac51790ae933b784b3c3166fdcfb6460711b280c1a

Contents?: true

Size: 911 Bytes

Versions: 33

Compression:

Stored size: 911 Bytes

Contents

# frozen_string_literal  = true

class Ree::StringUtils
  class << self
    def truncate(str, limit = 80)
      str.length > limit ? "#{str[0..limit]}..." : str
    end
    
    def underscore(camel_cased_word)
      return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
      word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
      word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
      word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
      word.tr!("-".freeze, "_".freeze)
      word.downcase!
      word
    end

    def camelize(string, uppercase_first_letter = true)
      if uppercase_first_letter
        string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
      else
        string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
      end
      
      string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
ree-1.0.32 lib/ree/utils/string_utils.rb
ree-1.0.31 lib/ree/utils/string_utils.rb
ree-1.0.30 lib/ree/utils/string_utils.rb
ree-1.0.29 lib/ree/utils/string_utils.rb
ree-1.0.28 lib/ree/utils/string_utils.rb
ree-1.0.27 lib/ree/utils/string_utils.rb
ree-1.0.26 lib/ree/utils/string_utils.rb
ree-1.0.25 lib/ree/utils/string_utils.rb
ree-1.0.24 lib/ree/utils/string_utils.rb
ree-1.0.23 lib/ree/utils/string_utils.rb
ree-1.0.22 lib/ree/utils/string_utils.rb
ree-1.0.21 lib/ree/utils/string_utils.rb
ree-1.0.20 lib/ree/utils/string_utils.rb
ree-1.0.19 lib/ree/utils/string_utils.rb
ree-1.0.18 lib/ree/utils/string_utils.rb
ree-1.0.17 lib/ree/utils/string_utils.rb
ree-1.0.16 lib/ree/utils/string_utils.rb
ree-1.0.15 lib/ree/utils/string_utils.rb
ree-1.0.14 lib/ree/utils/string_utils.rb
ree-1.0.13 lib/ree/utils/string_utils.rb