Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add docs for CRUD

  • Loading branch information...
commit 39f118347a0d1ecd1751dae205fd5291730fdce1 1 parent f0ad58a
@siuying authored
Showing with 123 additions and 29 deletions.
  1. +26 −0 README.md
  2. +49 −0 lib/nano_store/model.rb
  3. +48 −29 spec/model_spec.rb
View
26 README.md
@@ -58,3 +58,29 @@ user.age = age
user.created_at = created_at
user.save
````
+
+### Retrieve
+
+````ruby
+# find all models
+User.all => [<User#1>, <User#2>]
+
+# find model by criteria
+users = User.find(:name, NSFEqualTo, "Bob")
+````
+
+### Update
+
+````ruby
+user = User.find(:name, NSFEqualTo, "Bob").first
+user.name = "Dom"
+user.save
+````
+
+### Delete
+
+````ruby
+user = User.find(:name, NSFEqualTo, "Bob").first
+user.delete
+````
+
View
49 lib/nano_store/model.rb
@@ -53,11 +53,60 @@ def store
def store=(store)
@store = store
end
+
+ def all
+ search = NSFNanoSearch.searchWithStore(self.store)
+ error_ptr = Pointer.new(:id)
+ searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr)
+ raise NanoStoreError, error_ptr[0].description if error_ptr[0]
+ searchResults.values
+ end
+
+ # find model by criteria
+ #
+ # Return array of models
+ #
+ # Example:
+ #
+ # User.find(:name, NSFEqualTo, "Bob") => [<User#1>]
+ #
+ def find(attribute, match, value)
+ search = search_with_store(self.store, attribute, match, value)
+ error_ptr = Pointer.new(:id)
+ searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr)
+ raise NanoStoreError, error_ptr[0].description if error_ptr[0]
+ searchResults.values
+ end
+
+ # find model keys by criteria
+ #
+ # Return array of model keys
+ #
+ # Example:
+ #
+ # User.find(:name, NSFEqualTo, "Bob") => [<User#1>]
+ #
+ def find_keys(attribute, match, value)
+ search = search_with_store(self.store, attribute, match, value)
+ error_ptr = Pointer.new(:id)
+ searchResults = search.searchObjectsWithReturnType(NSFReturnKeys, error:error_ptr)
+ raise NanoStoreError, error_ptr[0].description if error_ptr[0]
+ searchResults
+ end
def inherited(subclass)
subclass.instance_variable_set(:@attributes, [])
subclass.instance_variable_set(:@store, nil)
end
+
+ private
+ def search_with_store(store, attribute, match, value)
+ search = NSFNanoSearch.searchWithStore(self.store)
+ search.attribute = attribute.to_s
+ search.match = match
+ search.value = value
+ search
+ end
end
class Model < NSFNanoObject
View
77 spec/model_spec.rb
@@ -38,32 +38,59 @@ def stub_user(name, age, created_at)
user.age.should == 10
end
+ it "update objects" do
+ user = stub_user("Bob", 10, Time.now)
+ user.save
+
+ user1 = User.find(:name, NSFEqualTo, "Bob").first
+ user1.name = "Dom"
+ user1.save
+
+ user2 = User.find(:name, NSFEqualTo, "Dom").first
+ user2.key.should == user.key
+ end
+
+ it "find object" do
+ user = stub_user("Bob", 10, Time.now)
+ user.save
+
+ users = User.find(:name, NSFEqualTo, "Bob")
+ users.should.not.be.nil
+ users.count.should == 1
+ users.first.name.should == user.name
+
+ users = User.find_keys(:name, NSFEqualTo, "Bob")
+ users.should.not.be.nil
+ users.count.should == 1
+ users.first.should == user.key
+ end
+
it "delete object" do
user = stub_user("Bob", 10, Time.now)
user.save
- search = NSFNanoSearch.searchWithStore(NanoStore.shared_store)
- search.attribute = "name"
- search.match = NSFEqualTo
- search.value = "Bob"
- error_ptr = Pointer.new(:id)
- searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr)
- raise NanoStoreError, error_ptr[0].description if error_ptr[0]
- searchResults.should.not.be.nil
- searchResults.count.should == 1
+ users = User.find(:name, NSFEqualTo, "Bob")
+ users.should.not.be.nil
+ users.count.should == 1
user.delete
- search = NSFNanoSearch.searchWithStore(NanoStore.shared_store)
- search.attribute = "name"
- search.match = NSFEqualTo
- search.value = "Bob"
- error_ptr = Pointer.new(:id)
- searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr)
- raise NanoStoreError, error_ptr[0].description if error_ptr[0]
- searchResults.should.not.be.nil
- searchResults.count.should == 0
+ users = User.find(:name, NSFEqualTo, "Bob")
+ users.should.not.be.nil
+ users.count.should == 0
end
+ it "find all objects" do
+ user = stub_user("Bob", 10, Time.now)
+ user.save
+
+ user2 = stub_user("Amy", 11, Time.now)
+ user2.save
+
+ users = User.all
+ users.size.should == 2
+ users[0].key.should == user.key
+ users[1].key.should == user2.key
+ end
it "search object" do
user = stub_user("Bob", 10, Time.now)
@@ -72,17 +99,9 @@ def stub_user(name, age, created_at)
user2 = stub_user("Amy", 11, Time.now)
user2.save
- search = NSFNanoSearch.searchWithStore(NanoStore.shared_store)
- search.attribute = "name"
- search.match = NSFEqualTo
- search.value = "Bob"
-
- error_ptr = Pointer.new(:id)
- searchResults = search.searchObjectsWithReturnType(NSFReturnObjects, error:error_ptr)
- raise NanoStoreError, error_ptr[0].description if error_ptr[0]
-
- searchResults.should.not.be.nil
- user = searchResults.values.first
+ users = User.find(:name, NSFEqualTo, "Bob")
+ users.should.not.be.nil
+ user = users.first
user.should.not.be.nil
user.name.should.be == "Bob"
user.age.should.be == 10
Please sign in to comment.
Something went wrong with that request. Please try again.