From b1c8ec0172db049249fcc9f12f4b7e67ba434aec Mon Sep 17 00:00:00 2001 From: Joep Meindertsma Date: Mon, 23 Nov 2020 10:45:55 +0100 Subject: [PATCH] Set propval rename #24 --- lib/examples/basic.rs | 2 +- lib/src/db.rs | 2 +- lib/src/resources.rs | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/examples/basic.rs b/lib/examples/basic.rs index efe0de035..d6371da8c 100644 --- a/lib/examples/basic.rs +++ b/lib/examples/basic.rs @@ -33,7 +33,7 @@ fn main() { // Let's make a new Property instance! let mut new_property = atomic_lib::Resource::new_instance("https://atomicdata.dev/classes/Property", &store).unwrap(); // And add a description for that Property - new_property.set_by_shortname("description", "the age of a person").unwrap(); + new_property.set_propval_by_shortname("description", "the age of a person").unwrap(); // The modified resource is saved to the store after this // A subject URL has been created automatically. diff --git a/lib/src/db.rs b/lib/src/db.rs index 51e5390d0..4306a37a5 100644 --- a/lib/src/db.rs +++ b/lib/src/db.rs @@ -221,7 +221,7 @@ mod test { .unwrap(); // And add a description for that Property new_property - .set_by_shortname("description", "the age of a person") + .set_propval_by_shortname("description", "the age of a person") .unwrap(); // Changes are only applied to the store after calling `.save()` new_property.save().unwrap(); diff --git a/lib/src/resources.rs b/lib/src/resources.rs index 48620b173..0d6214d69 100644 --- a/lib/src/resources.rs +++ b/lib/src/resources.rs @@ -183,14 +183,15 @@ impl<'a> Resource<'a> { /// Overwrites existing. /// Does not validate property / datatype combination pub fn set_propval(&mut self, property: String, value: Value) -> AtomicResult<()> { - self.propvals.insert(property, value); + self.propvals.insert(property.clone(), value.clone()); + self.commit.set(property, value.to_string()); Ok(()) } /// Sets a property / value combination. /// Property can be a shortname (e.g. 'description' instead of the full URL), if the Resource has a Class. /// Validates the datatype. - pub fn set_by_shortname(&mut self, property: &str, value: &str) -> AtomicResult<()> { + pub fn set_propval_by_shortname(&mut self, property: &str, value: &str) -> AtomicResult<()> { let fullprop = if is_url(property) { self.store.get_property(property)? } else { @@ -208,6 +209,7 @@ impl<'a> Resource<'a> { pub fn set_subject(&mut self, url: String) { self.subject = url; + // TODO: change subject URL in commit, introduce 'move' command? https://github.com/joepio/atomic/issues/44 } pub fn get_subject(&self) -> &String { @@ -398,11 +400,11 @@ mod test { let mut resource = store.get_resource(urls::CLASS).unwrap(); assert!(resource.get_shortname("shortname").unwrap().to_string() == "class"); resource - .set_by_shortname("shortname", "something-valid") + .set_propval_by_shortname("shortname", "something-valid") .unwrap(); assert!(resource.get_shortname("shortname").unwrap().to_string() == "something-valid"); resource - .set_by_shortname("shortname", "should not contain spaces") + .set_propval_by_shortname("shortname", "should not contain spaces") .unwrap_err(); } @@ -410,11 +412,9 @@ mod test { fn new_instance() { let store = init_store(); let mut new_resource = Resource::new_instance(urls::CLASS, &store).unwrap(); - new_resource - .set_by_shortname("shortname", "person") - .unwrap(); + new_resource.set_propval_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.set_propval_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();