Sha256: bf4738b65094fe95ad3d3c50ae20d56f4b2cbb70ec2f589139f1b7b8b0a928f8

Contents?: true

Size: 1.6 KB

Versions: 7

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Support
    module Copyable
      ##
      # @example
      #   person = Person.new('John', 'Doe', age: 42, gender: :male) do |p|
      #     puts p
      #   end
      #
      #   copy = person.copy
      #   copy = person.copy(overrides: {args: ['Jane']})
      #   copy = person.copy(overrides: {args: {0 => 'Jane'}})
      #   copy = person.copy(overrides: {kwargs: {age: 18}})
      #   copy = person.copy(overrides: {block: proc { |p| print p }})
      #
      # @internal
      #   NOTE: Inline logic instead of private methods is used intentionally in order to NOT pollute the public interface.
      #   NOTE: This method is NOT likely to be ever changed, that is why inline logic is preferred over command classes in this particular case.
      #
      def copy(overrides: {})
        overrides[:args] ||= {}
        overrides[:kwargs] ||= {}

        ##
        # TODO: Refactor runtime `respond_to?`. Investigate before refactoring.
        #
        args =
          if respond_to?(:to_args)
            case overrides[:args]
            when ::Array
              overrides[:args]
            when ::Hash
              Utils::Array.merge(to_args, overrides[:args])
            end
          else
            []
          end

        kwargs =
          if respond_to?(:to_kwargs)
            to_kwargs.merge(overrides[:kwargs])
          else
            {}
          end

        block =
          if respond_to?(:to_block)
            overrides[:block] || to_block
          end

        self.class.new(*args, **kwargs, &block)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
convenient_service-0.6.0 lib/convenient_service/support/copyable.rb
convenient_service-0.5.0 lib/convenient_service/support/copyable.rb
convenient_service-0.4.0 lib/convenient_service/support/copyable.rb
convenient_service-0.3.1 lib/convenient_service/support/copyable.rb
convenient_service-0.3.0 lib/convenient_service/support/copyable.rb
convenient_service-0.2.1 lib/convenient_service/support/copyable.rb
convenient_service-0.2.0 lib/convenient_service/support/copyable.rb