Sha256: f2cf64f3c346e7576990e7aa005a322b43b7c7f3122a060d19629e75e5f27616

Contents?: true

Size: 826 Bytes

Versions: 2

Compression:

Stored size: 826 Bytes

Contents

---
title: Container Version
layout: gem-single
name: dry-initializer
---

Instead of extending a class with the `Dry::Initializer`, you can include a container with the `initializer` method only. This method should be preferred when you don't need subclassing.

```ruby
require 'dry-initializer'

class User
  # notice `-> do .. end` syntax
  include Dry::Initializer.define -> do
    param  :name,  proc(&:to_s)
    param  :role,  default: proc { 'customer' }
    option :admin, default: proc { false }
  end
end
```

If you still need the DSL (`param` and `option`) to be inherited, use the direct extension:

```ruby
require 'dry-initializer'

class BaseService
  extend Dry::Initializer
  alias_method :dependency, :param
end

class ShowUser < BaseService
  dependency :user

  def call
    puts user&.name
  end
end
```

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-initializer-3.0.3 docsite/source/container-version.html.md
dry-initializer-3.0.2 docsite/source/container-version.html.md