siuying / NanoStoreInMotion

RubyMotion wrapper for NanoStore, a lightweight schema-less key-value document database based on sqlite.
Other
103 stars 24 forks source link

save on the store doesn't seem to have any effect #16

Closed jetpad closed 12 years ago

jetpad commented 12 years ago

I may not quite understand how saving works. It seems that the only way to get objects saved to the database is to save each one using the save method on the object instead of delaying the save and calling the save method on the store. Replacing "user.save" in this code with "NanoStore.shared_store.save" breaks this case.

describe NanoStore::Model do

 class User < NanoStore::Model
    attribute :name
    attribute :age
    attribute :created_at
  end

  def stub_user(name, age, created_at)
    user = User.new
    user.name = name
    user.age  = age
    user.created_at = created_at
    user
  end

  before do
    NanoStore.shared_store = NanoStore.store
  end

  after do
    NanoStore.shared_store = nil
  end

  it "saving via the store instead of model" do

    user = stub_user("Bob", 10, Time.now)

    user.save
#NanoStore.shared_store.save

    user.info.keys.include?("name").should.be.true
    user.info.keys.include?("age").should.be.true
    user.info.keys.include?("created_at").should.be.true

    user.info["name"].should == "Bob"
    user.info["age"].should == 10
    user.info["created_at"].should == user.created_at

    User.count.should == 1
  end

end
siuying commented 12 years ago

You must put the user object into store to save it.

store = NanoStore.shared_store
user = stub_user("Bob", 10, Time.now)
store << user
store.save

I've added a test case to demo that:

https://github.com/siuying/NanoStoreInMotion/blob/master/spec/store_extension_spec.rb#L115-123

jetpad commented 12 years ago

Ah, great! Thanks for your work on this. It is really perfect for working with RubyMotion.