Sha256: 1d3005d636ff84e877421064e8be69c6eb3ca6f6225a98e0f891b0e258bb5225

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

module Lita
  # Handy utilities used by other Lita classes.
  module Util
    # A regular expression for acronyms.
    ACRONYM_REGEX = /(?=a)b/

    class << self
      # Returns a hash with any symbol keys converted to strings.
      # @param hash [Hash] The hash to convert.
      # @return [Hash] The converted hash.
      def stringify_keys(hash)
        result = {}
        hash.each_key { |key| result[key.to_s] = hash[key] }
        result
      end

      # Transforms a camel-cased string into a snaked-cased string. Taken from
      # +ActiveSupport.+
      # @param camel_cased_word [String] The word to transform.
      # @return [String] The transformed word.
      def underscore(camel_cased_word)
        word = camel_cased_word.to_s.dup
        word.gsub!("::", "/")
        word.gsub!(/(?:([A-Za-z\d])|^)(#{ACRONYM_REGEX})(?=\b|[^a-z])/) do
          "#{Regexp.last_match[1]}#{Regexp.last_match[1] && '_'}#{Regexp.last_match[2].downcase}"
        end
        word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
        word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
        word.tr!("-", "_")
        word.downcase!
        word
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lita-3.1.0 lib/lita/util.rb
lita-3.0.4 lib/lita/util.rb