Skip to content

Commit

Permalink
Set propval rename #24
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Dec 25, 2020
1 parent 06381d6 commit b1c8ec0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions lib/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -398,23 +400,21 @@ 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();
}

#[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();
Expand Down

0 comments on commit b1c8ec0

Please sign in to comment.