Sha256: a8bd8d45164979f7a246e39ea2cb8716f9b47addb5096954d48e1f7f34eb8c39

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module RubyLokaliseApi
  # Basic utilitiy methods
  module Utils
    module Classes
      refine Object do
        # Extracts the base name of a class, removing any module nesting.
        #
        # @return [String] The base class name
        def base_name
          name.split('::').last
        end

        # Converts the object to an array, unless it is already an array.
        #
        # @return [Array] The object wrapped in an array if not already an array
        def to_array
          is_a?(Array) ? self : [self]
        end

        # Converts the object to an array, then places this array inside a hash
        # under the provided key.
        #
        # @param key [Symbol, String] The key under which to place the array
        # @return [Hash] The hash with the array under the provided key
        def to_array_obj(key)
          return self if is_a?(Hash) && (key?(key) || key?(key.to_s))

          { key => to_array }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-lokalise-api-9.3.0 lib/ruby_lokalise_api/utils/classes.rb
ruby-lokalise-api-9.2.1 lib/ruby_lokalise_api/utils/classes.rb
ruby-lokalise-api-9.2.0 lib/ruby_lokalise_api/utils/classes.rb