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
|
# File 'lib/params_ready/extensions/collection.rb', line 6
def collection(
collection_name,
element_name,
freeze_collection: true,
freeze_value: true,
getter: true,
obligatory: false,
&block
)
full_name = "@#{collection_name}"
define_method "add_#{element_name}" do |value|
value = instance_exec(value, &block) unless block.nil?
next if value == Extensions::Undefined
collection = send collection_name
value.freeze if freeze_value
collection << value
end
if getter
define_method collection_name do
if instance_variable_defined? full_name
instance_variable_get full_name
elsif frozen?
[].freeze
else
instance_variable_set full_name, []
instance_variable_get full_name
end
end
end
obligatory! collection_name if obligatory
freeze_variable collection_name if freeze_collection
end
|