Sha256: ad180eb9d82a801a7939518b0d1525d749b9df8b56a568388c41e402e96a76b5

Contents?: true

Size: 824 Bytes

Versions: 9

Compression:

Stored size: 824 Bytes

Contents

# frozen_string_literal: true

module RatingChgkV2
  module Utils
    module StringUtils
      refine String do
        # Underscore a string such that camelcase, dashes and spaces are
        # replaced by underscores.
        # Initial code taken from Facets gem by Rubyworks
        # https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
        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
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rating-chgk-v2-1.6.0 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.5.0 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.4.0 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.3.0 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.2.0 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.1.0 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.0.1 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.0.0 lib/rating_chgk_v2/utils/string_utils.rb
rating-chgk-v2-1.0.0.rc1 lib/rating_chgk_v2/utils/string_utils.rb