Sha256: c8feb4eed1f7f5c881c4954465f515098b9bb09b8aac18bf1e241d6357bb9ad8

Contents?: true

Size: 730 Bytes

Versions: 2

Compression:

Stored size: 730 Bytes

Contents

# Finalization

To apply custom transformations to the cloned record, you can use the `finalize` declaration:

```ruby
class UserCloner < Clowne::Cloner
  finalize do |_source, record, **_params|
    record.name = "This is copy!"
  end

  trait :change_email do
    finalize do |_source, record, **params|
      record.email = params[:email]
    end
  end
end

cloned = UserCloner.call(user).to_record
cloned.name
# => 'This is copy!'
cloned.email == "clone@example.com"
# => false

cloned2 = UserCloner.call(user, traits: :change_email).to_record
cloned2.name
# => 'This is copy!'
cloned2.email
# => 'clone@example.com'
```

Finalization blocks are called at the end of the [cloning process](getting_started?id=execution-order).

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clowne-1.4.0 docs/finalize.md
clowne-1.3.0 docs/finalize.md