I have the following instance of a simple Person
object represented in yaml.
id: Father
person_type: PrimaryOwner
birth_year: 1968
age_assumptions:
retirement_age: 65
death_age: 85
I’m new to and just learning elixir and trying to understand some capabilities in terms of modeling data.
Below is the rust definition corresponding to this instance. Effectively that is one representation of the schema. My questions are:
-
Assuming I don’t want any database dependencies, how can
ecto
and specificallyembedded_schema
be used to model such a schema? The answer to this makes me think it’s straightforward but since I don’t know answers to questions below I’m not sure how to get started. -
If it can, is there an adapter that can just keep the repository in memory and read/write it in toto as
json
oryaml
on demand? -
If that is possible, is all the meta-data of the ecto ebmedded schema available for programmatic access from elixir? For example, could elixir code find all enumerations or all schema fields with a default value?
#[derive(
Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
pub enum PersonType {
/// The primary owner of the `Dossier`
PrimaryOwner,
/// The secondary owner of the `Dossier`
SecondaryOwner,
/// A dependent - as in child
Dependent,
}
/// Assumptions regarding the ages of a person _at retirement_, _at death_, etc.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct AgeAssumptions {
/// Age of retirement - where labor incomes are ended.
///
pub retirement_age: i32,
/// Age at death.
pub death_age: i32,
}
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct Person {
/// Identifier for the person (eg name or nom de guerre)
pub id: String,
/// Type of person
pub person_type: PersonType,
/// Year of birth of person
pub birth_year: i32,
/// Important ages for person
#[serde(skip_serializing_if = "is_default")]
pub age_assumptions: Option<AgeAssumptions>,
}
/// Implementation of trait `Default` for type `AgeAssumptions`
impl Default for AgeAssumptions {
/// A trait for giving a type a useful default value.
///
/// * _return_ - The default value for the type
///
fn default() -> Self {
AgeAssumptions {
retirement_age: 65,
death_age: 95,
}
}
}