Sha256: 9128a5dac8816791e131781e04387be2cff784ae55d83b6d3397555bb358bdd1

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module Tide
  module API
    # Provides methods for string manipulation
    #
    # @api private
    #
    module Util
      module_function

      # Converts a string to snake case
      #
      # @param [String|Symbol] term The term to be converted.
      #
      # @return [String] A snake-cased version of the input
      #
      def underscore(term)
        term
          .to_s
          .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
          .gsub(/([a-z\d])([A-Z])/, '\1_\2')
          .tr('-', '_')
          .downcase
          .to_sym
      end

      # Converts a string to camel case
      #
      # @param [String|Symbol] term The term to be converted.
      #
      # @return [String] A camel-cased version of the input
      #
      def camelize(term)
        string = term.to_s
        string = string.sub(/^[a-z\d]*/, &:capitalize)
        string.gsub!(%r{(?:_|(\/))([a-z\d]*)}) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
        string.gsub!('/'.freeze, '::'.freeze)
        string
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tide-api-0.4.0 lib/tide/api/util.rb
tide-api-0.3.0 lib/tide/api/util.rb
tide-api-0.2.0 lib/tide/api/util.rb