Sha256: 335675db9b05232382b98a06b1a593a98b531afdcc2e83f7ca069e2f32cbb912

Contents?: true

Size: 1.44 KB

Versions: 12

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Utils
    module String
      ##
      # @example
      #   demodulize('ActiveSupport::Inflector::Inflections') # => "Inflections"
      #   demodulize('Inflections')                           # => "Inflections"
      #   demodulize('::Inflections')                         # => "Inflections"
      #   demodulize('')                                      # => ""
      #
      # @see https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-demodulize
      #
      class Demodulize < Support::Command
        ##
        # @!attribute [r] string
        #   @return [String]
        #
        attr_reader :string

        ##
        # @param string [#to_s]
        # @return [void]
        #
        def initialize(string)
          @string = string.to_s
        end

        ##
        # @return [String]
        #
        # @internal
        #   NOTE: Copied with cosmetic modifications from:
        #   - https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-demodulize
        #
        #   NOTE: Modifications.
        #   - `string` is `path.to_s` is the original implementation.
        #   - Fixed Rubocop complaint about `Style/SlicingWithRange`.
        #   - Fixed Rubocop complaint about `Lint/AssignmentInCondition`.
        #
        def call
          i = string.rindex("::")

          i ? string[(i + 2)..] : string
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.19.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.18.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.17.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.16.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.15.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.14.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.13.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.12.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.11.0 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.10.1 lib/convenient_service/utils/string/demodulize.rb
convenient_service-0.10.0 lib/convenient_service/utils/string/demodulize.rb