Sha256: ac7d04bd7e8dda455e43744f4f83a134fa59f590c0620f0d487e4d2a91702ad3

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8
# frozen_string_literal: false

require 'forwardable'

# String utility for creating titles and class-names
class StringExtra
  extend Forwardable
  def_delegators :@str, :upcase, :capitalize, :length, :downcase, :gsub, :tr
  def initialize(str)
    @str = str
  end

  def titleize
    gsub(/::/, '/')
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .tr('-', '_')
      .downcase
      .gsub(/_id$/, '')
      .tr('_', ' ').capitalize
      .gsub(/\b([a-z])/) { Regexp.last_match[1].capitalize }
  end

  def humanize
    gsub(/_id$/, '').tr(/_/, ' ').capitalize
  end

  def underscore
    gsub(/::/, '/')
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .sub('-', '_')
      .downcase
  end

  def camelize(first_letter_in_uppercase = true)
    if first_letter_in_uppercase
      @str.gsub(%r{/(.?)}) { '::' + Regexp.last_match[1].upcase }
          .gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
    else
      @str[0] + camelize[1..-1]
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
propane-0.8.0-java lib/propane/helpers/string_extra.rb
propane-0.7.0-java lib/propane/helpers/string_extra.rb
jruby_art-1.1.1 lib/jruby_art/helpers/string_extra.rb
jruby_art-1.1.0 lib/jruby_art/helpers/string_extra.rb
jruby_art-1.0.8 lib/jruby_art/helpers/string_extra.rb
propane-0.6.0-java lib/propane/helpers/string_extra.rb
jruby_art-1.0.7 lib/jruby_art/helpers/string_extra.rb
jruby_art-1.0.6 lib/jruby_art/helpers/string_extra.rb
propane-0.5.0-java lib/propane/helpers/string_extra.rb