53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'foobara-0.0.110/projects/type_declarations/src/dsl/attributes.rb', line 53
def method_missing(attribute_name, *processor_symbols, **declaration, &block)
unless respond_to_missing?(attribute_name)
return super
end
_disable_method_missing do
declaration = declaration.dup
if block
if processor_symbols.first == :array
type, *processor_symbols = processor_symbols
end
else
type, *processor_symbols = processor_symbols
unless type
if processor_symbols.empty? && !declaration.empty?
type = :attributes
declaration = { element_type_declarations: declaration }
else
::Kernel.raise NoTypeGivenError, "Expected a type but attribute #{attribute_name} was declared " \
"without a type. (Perhaps you didn't mean for #{attribute_name} " \
"to be an attribute?)"
end
end
end
processor_symbols.each do |processor_symbol|
case processor_symbol
when ::String
description = processor_symbol
if declaration.key?(:description)
::Kernel.raise ArgumentError, "Expected only one description but " \
"got #{description.inspect} and #{declaration[:description].inspect}"
end
declaration[:description] = description
when ::Symbol
declaration[processor_symbol] = true
else
::Kernel.raise ArgumentError, "expected a Symbol, got #{processor_symbol.inspect}"
end
end
if declaration.delete(:required)
_add_to_required(attribute_name)
end
if declaration.key?(:default)
default = declaration.delete(:default)
_add_to_defaults(attribute_name, default)
end
if declaration.empty? && !block
_add_attribute(attribute_name, type)
else
declaration = if block
attributes_declaration = Attributes.to_declaration(&block).merge(declaration)
if type == :array
{
type: :array,
element_type_declaration: attributes_declaration
}
else
attributes_declaration
end
else
declaration.merge(type:)
end
_add_attribute(attribute_name, declaration)
end
_type_declaration
AttributeCreated.new(attribute_name)
end
end
|