StrokeDB short intro

Posted by yrashk

I’ve decided to write a short introductory material about StrokeDB usage.

Disclaimer: StrokeDB is still pretty young (still less than one month of development), so I can’t promise that API shown will remain the same forever. In fact, some portions of it will definitely change

First, lets load StrokeDB and initialize it:


require "strokedb" 
StrokeDB::Config.build :default => true, :base_path => 'some_test'

I willn’t go into guts of config builder (that’s a bit complicated for those who are new to StrokeDB, but I’ll probably post some materials about it later).

So effectively, now we have a database initialized and it’s base path for file storages is some_test/

Now, we’re going to get into some fun. I’ll define several metas.

User:


User = StrokeDB::Meta.new do
  def to_s
    self[:name]
  end
end

Unlike ActiveRecord, StrokeDB uses a kind of mixin model. Each document can have any number of metadocuments it refer to. As I described previously metadocuments are documents that describe document’s essense and Ruby modules to extend Document’s behavior.

In the above code, I am defining User meta, which have only one method #to_s, which will render slot ‘name’.

Now, a little bit more complex example:


Buyer = StrokeDB::Meta.new do

  on_initialization do |buyer|
    unless buyer[:balance]
      puts "Providing $100 to #{buyer}, since he is a new buyer" 
      buyer.balance = 100 
    end
    unless buyer[:products_bought]
      buyer.products_bought = []
    end
  end

  after_save do |buyer|
    puts "Now #{buyer} has #{buyer.products_bought.empty? ? 'nothing' : buyer.products_bought.map(&:name).to_sentence} (and his balance is $#{buyer.balance})" 
  end

  def buy!(product)
    puts "#{self} is buying #{product}" 
    product.checkout!
    self.products_bought << product
    self.balance -= product.price
    save!
  end
end

Buyer is another metadocument that defines Buyer-specific functionality. Besides #buy! method it defines two callbacks: on_initialization and after_save. Their names are pretty self-descriptive (I hope)

And here is a last metadocument:


Product = StrokeDB::Meta.new do
  after_save do |product|
    puts "#{product.quantity} items of #{product} left" 
  end

  def to_s
    "'#{name}' for $#{price}" 
  end

  def checkout!
    self.quantity -= 1
    save!
  end
end

Nothing really new comparing to Buyer. So lets go further.


u = User.new(:name => "Yurii")
u.metas << Buyer
u.save!

Here I create a document with meta ‘User’ and add ‘Buyer’ meta to it, so it is both User and Buyer at the same time!


apple = Product.create!(:name => "green apple", :price => 2,:quantity => 100)
pizza = Product.create!(:name => "big pizza", :price => 15,:quantity => 5)
u.buy!(apple)
u.buy!(pizza)

In the above lines I create two products, apple and pizza and use Buyer’s #buy! method to purchase them.

Here is an output of this test code:


Providing $100 to Yurii, since he is a new buyer
Now Yurii has nothing (and his balance is $100)
100 items of 'green apple' for $2 left
5 items of 'big pizza' for $15 left
Yurii is buying 'green apple' for $2
99 items of 'green apple' for $2 left
Now Yurii has green apple (and his balance is $98)
Yurii is buying 'big pizza' for $15
4 items of 'big pizza' for $15 left
Now Yurii has green apple and big pizza (and his balance is $83)

Here we are, everything works!

Also, if you’ll inspect user’s document, you’ll see the following:


#<{User,Buyer} name: "Yurii", __version__: c33e..., products_bought: [#<{Product} name: "green apple", __version__: 933e..., price: 2, quantity: 99, __previous_version__: 733e...>, #<{Product} name: "big pizza", __version__: b33e..., price: 15, quantity: 4, __previous_version__: 833e...>], __previous_version__: a33e..., balance: 83>


Notice that in the beginning, it is defined as {User,Buyer} — both metas are displayed. This way you can easily understand what this document is actually.

Ok, I hope that’s enough for the beginning. You can see complete test source code at Gitorious

Also I would like to mention that we’ve added test console recently

Join our mailing list or get source code

P.S. I need to warn you: StrokeDB is quite immature, definitely has bugs and definitely will evolve. Period.

Comments

Leave a response