In our trainee_attributes class, we have a long list of attributes and metadata for them split across multiple constants, and repeated in several places throughout the file.
What if we move this information out into a YAML file or something and organise it more like this:
first_names:
{
required: true,
nested: false,
internal: false,
type: "string",
options: {
default: "..."
},
}
date_of_birth:
{
required: true,
nested: false,
internal: false,
type: "date",
options: {}
}
We could then still use the constants we have in the attributes class, but make them much more concise:
ATTRIBUTES_PATH = File.join(__dir__, 'trainee_attributes.yml')
ATTRIBUTES = YAML.load_file(ATTRIBUTES_PATH).deep_symbolize_keys
REQUIRED_ATTRIBUTES = ATTRIBUTES.select { |_, config| config[:required] }.keys
INTERNAL_ATTRIBUTES = ATTRIBUTES.select { |_, config| config[:internal] }.keys
NESTED_ATTRIBUTES = ATTRIBUTES.select { |_, config| config[:nested] }.keys
That would mean we can keep the attributes class more focused on the validation logic, assigning attributes and that sort of thing, rather than housing all this data. It also means we can add new attributes, and change the metadata just by updating the yaml file.
If we like the pattern, we could also apply it to the other attribute classes.
In our trainee_attributes class, we have a long list of attributes and metadata for them split across multiple constants, and repeated in several places throughout the file.
What if we move this information out into a YAML file or something and organise it more like this:
We could then still use the constants we have in the attributes class, but make them much more concise:
That would mean we can keep the attributes class more focused on the validation logic, assigning attributes and that sort of thing, rather than housing all this data. It also means we can add new attributes, and change the metadata just by updating the yaml file.
If we like the pattern, we could also apply it to the other attribute classes.