/
initializer.rb
63 lines (54 loc) · 1.74 KB
/
initializer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
require "set"
# Namespace for gems in a dry-rb community
module Dry
#
# DSL for declaring params and options of class initializers
#
module Initializer
require_relative "initializer/undefined"
require_relative "initializer/dsl"
require_relative "initializer/definition"
require_relative "initializer/builders"
require_relative "initializer/config"
require_relative "initializer/mixin"
require_relative "initializer/dispatchers"
# Adds methods [.[]] and [.define]
extend DSL
# Gem-related configuration
# @return [Dry::Initializer::Config]
def dry_initializer
@dry_initializer ||= Config.new(self)
end
# Adds or redefines a parameter of [#dry_initializer]
# @param [Symbol] name
# @param [#call, nil] type (nil)
# @option opts [Proc] :default
# @option opts [Boolean] :optional
# @option opts [Symbol] :as
# @option opts [true, false, :protected, :public, :private] :reader
# @yield block with nested definition
# @return [self] itself
def param(name, type = nil, **opts, &block)
dry_initializer.param(name, type, **opts, &block)
self
end
# Adds or redefines an option of [#dry_initializer]
# @param (see #param)
# @option (see #param)
# @yield (see #param)
# @return (see #param)
def option(name, type = nil, **opts, &block)
dry_initializer.option(name, type, **opts, &block)
self
end
private
def inherited(klass)
super
config = Config.new(klass, null: dry_initializer.null)
klass.send(:instance_variable_set, :@dry_initializer, config)
dry_initializer.children << config
end
require_relative "initializer/struct"
end
end