Sha256: e8b8088954d57f2002177897928df9cb2fa28e39f03a2f4a004c724a3350f542

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# -*-mode: ruby; coding: utf-8 -*-

module ModulePlus
  module CoreExt
    module String
      #
      # Camel case string to under score string
      #
      # double column("::") to slash("/")
      # dash("-") to underscore("_")
      # reference ActiveSupport (MIT License)
      # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L92
      #
      def self.underscore(origin)
        return origin unless /[A-Z-]|::/.match?(origin)
        word = origin.to_s.gsub('::', '/')
        word.gsub!(acronyms_underscore_regex) { "#{$1 && '_'.freeze }#{$2.downcase}" }
        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 self.acronyms_underscore_regex
        /(?:(?<=([A-Za-z\d]))|\b)(#{acronym_regex})(?=\b|[^a-z])/
      end
      def self.acronym_regex
        /(?=a)b/
      end
    end
  end
end

class String
  def underscore
    ModulePlus::CoreExt::String.underscore(self)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
module_plus-0.1.0 lib/module_plus/core_ext/string.rb