Sha256: 5acdd0f2a2e322d2a5d6956b82f6b2155aac2c424810f31c757a06ee31a9371b

Contents?: true

Size: 885 Bytes

Versions: 3

Compression:

Stored size: 885 Bytes

Contents

# frozen_string_literal: true

# Initial code taken from Facets gem by Rubyworks
# https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb

module Lokalise
  module Utils
    module StringUtils
      refine String do
        # Underscore a string such that camelcase, dashes and spaces are
        # replaced by underscores.
        def snakecase
          base_class_name.
            gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
            gsub(/([a-z\d])([A-Z])/, '\1_\2').
            tr('-', '_').
            gsub(/\s/, '_').
            gsub(/__+/, '_').
            downcase
        end

        # Turn `Module::Nested::ClassName` to just `ClassName`
        def base_class_name
          split('::').last
        end

        def remove_trailing_slash
          delete_suffix '/'
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-lokalise-api-5.0.0 lib/ruby-lokalise-api/utils/string_utils.rb
ruby-lokalise-api-4.5.1 lib/ruby-lokalise-api/utils/string_utils.rb
ruby-lokalise-api-4.5.0 lib/ruby-lokalise-api/utils/string_utils.rb