Skip to content

Commit

Permalink
Explicit resource saving #24
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Oct 30, 2020
1 parent b1fa823 commit d5ad37c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ mod test {
new_property
.set_by_shortname("description", "the age of a person")
.unwrap();
// Changes are only applied to the store after calling `.save()`
new_property.save().unwrap();
// The modified resource is saved to the store after this

// A subject URL has been created automatically.
Expand Down
10 changes: 4 additions & 6 deletions lib/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::collections::HashMap;

/// A Resource is a set of Atoms that shares a single Subject.
/// A Resource only contains valid Values, but it _might_ lack required properties.
/// All changes to the Resource are immediately applied to the Store as well.
/// All changes to the Resource are applied after calling `.save()`.
// #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Resource<'a> {
/// A hashMap of all the Property Value combinations
Expand Down Expand Up @@ -125,8 +125,6 @@ impl<'a> Resource<'a> {
/// Validates the datatype.
pub fn remove_propval(&mut self, property_url: &str) {
self.propvals.remove_entry(property_url);
// Could fail, but unlikely
self.save().ok();
}

/// Tries to resolve the shortname of a Property to a Property URL.
Expand All @@ -150,8 +148,8 @@ impl<'a> Resource<'a> {
}

/// Saves the resource (with all the changes) to the store
/// Should be called automatically, but we might add some form of batching later
fn save(&self) -> AtomicResult<()> {
/// Should be run after any (batch of) changes to the Resource!
pub fn save(&self) -> AtomicResult<()> {
self.store.add_resource(self)
}

Expand All @@ -169,7 +167,6 @@ impl<'a> Resource<'a> {
/// Overwrites existing.
pub fn set_propval(&mut self, property: String, value: Value) -> AtomicResult<()> {
self.propvals.insert(property, value);
self.save()?;
Ok(())
}

Expand Down Expand Up @@ -272,6 +269,7 @@ mod test {
new_resource.set_by_shortname("shortname", "person").unwrap();
assert!(new_resource.get_shortname("shortname").unwrap().to_string() == "person");
new_resource.set_by_shortname("shortname", "human").unwrap();
new_resource.save().unwrap();
assert!(new_resource.get_shortname("shortname").unwrap().to_string() == "human");
let mut resource_from_store = store.get_resource(new_resource.get_subject()).unwrap();
assert!(resource_from_store.get_shortname("shortname").unwrap().to_string() == "human");
Expand Down

0 comments on commit d5ad37c

Please sign in to comment.