-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathinitializer.rb
More file actions
68 lines (56 loc) · 1.4 KB
/
initializer.rb
File metadata and controls
68 lines (56 loc) · 1.4 KB
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
64
65
66
67
68
module Dry::Initializer::Builders
# @private
class Initializer
require_relative 'signature'
require_relative 'attribute'
def self.[](config)
new(config).call
end
def call
lines.flatten.compact.join("\n")
end
private
def initialize(config)
@config = config
@definitions = config.definitions.values
end
def lines
[
undef_line,
define_line,
params_lines,
options_lines,
rest_lines,
end_line
]
end
def undef_line
'undef :__dry_initializer_initialize__' \
' if private_method_defined? :__dry_initializer_initialize__'
end
def define_line
"private def __dry_initializer_initialize__(#{Signature[@config]})"
end
def params_lines
@definitions.reject(&:option)
.flat_map { |item| Attribute[item] }
.map { |line| ' ' << line }
end
def options_lines
@definitions.select(&:option)
.flat_map { |item| Attribute[item] }
.map { |line| ' ' << line }
end
def rest_lines
lines = []
lines << "@#{@config.rest_params} = #{@config.rest_params}" if @config.rest_params
if @config.rest_params && @definitions.any?(&:option)
lines << "@#{@config.rest_options} = #{@config.rest_options}"
end
lines.map { |line| " " << line }
end
def end_line
'end'
end
end
end