Class: Etna::Clients::Magma::AddModelValidator
Instance Attribute Summary collapse
#errors
Instance Method Summary
collapse
#check_in_set, #check_key, #check_key_empty, #check_valid_name_with_numbers, #format_errors, #model_exists_in_project?, #name_regex_no_numbers, #name_regex_with_numbers, #nil_or_empty?, #valid?, #validate!
Constructor Details
#initialize(models = Models.new, model_name = 'project') ⇒ AddModelValidator
Returns a new instance of AddModelValidator.
109
110
111
112
113
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 109
def initialize(models = Models.new, model_name = 'project')
super()
@model = models.build_model(model_name)
@models = models
end
|
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
107
108
109
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 107
def model
@model
end
|
Instance Method Details
#is_project? ⇒ Boolean
127
128
129
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 127
def is_project?
name == 'project'
end
|
#link_attributes ⇒ Object
190
191
192
193
194
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 190
def link_attributes
@model.template.attributes.all.select do |attribute|
attribute.link_model_name
end
end
|
#name ⇒ Object
119
120
121
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 119
def name
model&.name
end
|
#parent_reciprocal_attribute ⇒ Object
115
116
117
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 115
def parent_reciprocal_attribute
@models.find_reciprocal(model: @model, link_attribute_name: @model.template.parent)
end
|
#raw ⇒ Object
123
124
125
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 123
def raw
model&.template&.raw
end
|
#validate ⇒ Object
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 131
def validate
@errors << "Model name #{name} must be snake_case and can only consist of letters and \"_\"." unless name =~ name_regex_no_numbers
if parent_reciprocal_attribute&.attribute_type != Etna::Clients::Magma::ParentLinkType::TABLE
check_key("model #{name}", raw, 'identifier')
end
validate_links
validate_attributes
end
|
#validate_attributes ⇒ Object
#validate_links ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 163
def validate_links
if !is_project? && !@models.model_keys.include?(@model.template.parent)
@errors << "Parent model \"#{@model.template.parent}\" for #{name} does not exist in project."
end
if !is_project? && parent_reciprocal_attribute.nil?
@errors << "Parent link attributes not defined for model #{name}."
end
link_attributes.each do |attribute|
check_key("attribute #{attribute.attribute_name}", attribute.raw, 'link_model_name')
if attribute.attribute_name != attribute.link_model_name
@errors << "Linked model, \"#{attribute.link_model_name}\", does not match attribute #{attribute.attribute_name}, link attribute names must match the model name."
end
unless @models.model_keys.include?(attribute.link_model_name)
@errors << "Linked model, \"#{attribute.link_model_name}\", on attribute #{attribute.attribute_name} does not exist!"
end
reciprocal = @models.find_reciprocal(model: @model, attribute: attribute)
if reciprocal.nil?
@errors << "Linked model, \"#{attribute.link_model_name}\", on attribute #{attribute.attribute_name} does not have a reciprocal link defined."
end
end
end
|