Module: ConvenientService::Support::Copyable

Included in:
Core::Entities::Config::Entities::MethodMiddlewares::Entities::Caller::Concern::InstanceMethods, Core::Entities::Config::Entities::MethodMiddlewares::Entities::Container::Concern::InstanceMethods, ConvenientService::Service::Plugins::CanHaveConnectedSteps::Entities::Expressions::Base, ConvenientService::Service::Plugins::CanHaveConnectedSteps::Entities::StepCollection, ConvenientService::Service::Plugins::CanHaveSequentialSteps::Entities::StepCollection, ConvenientService::Service::Plugins::CanHaveSteps::Entities::Method::Entities::Callers::Base, ConvenientService::Service::Plugins::CanHaveSteps::Entities::Method::Entities::Directions::Base, ConvenientService::Service::Plugins::CanHaveSteps::Entities::Method::Entities::Key, ConvenientService::Service::Plugins::CanHaveSteps::Entities::Method::Entities::Name, ConvenientService::Service::Plugins::CanHaveSteps::Entities::Step::Concern::InstanceMethods, ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Concern::InstanceMethods, ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Code::Concern::InstanceMethods, ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Data::Concern::InstanceMethods, ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Message::Concern::InstanceMethods, ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Status::Concern::InstanceMethods, DependencyContainer::Entities::Method
Defined in:
lib/convenient_service/support/copyable.rb

Instance Method Summary collapse

Instance Method Details

#copy(overrides: {}) ⇒ Object

Examples:

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 }})


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/convenient_service/support/copyable.rb', line 24

def copy(overrides: {})
  defaults = {args: {}, kwargs: {}}

  ##
  # IMPORTANT: Do not mutate `overrides`.
  #
  overrides = defaults.merge(overrides)

  args =
    case overrides[:args]
    when ::Array then overrides[:args]
    when ::Hash then Utils::Array.merge(to_arguments.args, overrides[:args])
    end

  kwargs = to_arguments.kwargs.merge(overrides[:kwargs])
  block = overrides[:block] || to_arguments.block

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