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
|
# File 'lib/amber_component/props.rb', line 19
def prop(*names, type: nil, required: false, default: nil, allow_nil: false)
@prop_definitions ||= {}
include(@prop_methods_module = ::Module.new) if @prop_methods_module.nil?
names.each do |name|
@prop_definitions[name] = prop_def = PropDefinition.new(
name: name,
type: type,
required: required,
default: default,
allow_nil: allow_nil
)
raise IncorrectPropTypeError, " `type` should be a class but received `\#{type.inspect}` (`\#{type.class}`)\n MSG\n\n @prop_methods_module.attr_reader name\n next @prop_methods_module.attr_writer(name) unless prop_def.type?\n\n @prop_methods_module.class_eval( # rubocop:disable Style/DocumentDynamicEvalDefinition\n # def phone=(val)\n # raise IncorrectPropTypeError, <<~MSG unless val.nil? || val.is_a?(String)\n # \#{self.class} received `\#{val.class}` instead of `String` for `phone` prop\n # MSG\n #\n # @phone = val\n # end\n <<~RUB, __FILE__, __LINE__ + 1\n def \#{name}=(val)\n raise IncorrectPropTypeError, <<~MSG unless \#{allow_nil ? 'val.nil? ||' : nil} val.is_a?(\#{prop_def.type})\n \\\#{self.class} received `\\\#{val.class}` instead of `\#{prop_def.type}` for `\#{name}` prop\n MSG\n\n @\#{name} = val\n end\n RUB\n ) # rubocop:disable Layout/HeredocArgumentClosingParenthesis\n end\nend\n" unless type.nil? || type.is_a?(::Class)
|