Permalink
Browse files

added sample example

  • Loading branch information...
1 parent 5733318 commit 54b48dda56a402849ddf3ef16fa691896bc7b12c @crapooze committed Apr 6, 2012
Showing with 60 additions and 2 deletions.
  1. +18 −2 README.md
  2. +42 −0 samples/redis.rb
View
@@ -1,6 +1,22 @@
# Welo::Data
-TODO: Write a gem description
+Welo::Data is a library for Welo persisting and retriving Welo resources.
+Welo::Data overloads initialize, hence you should not inject Welo::Data::Resource in an object unless you know what you're doing.
+
+Within Welo, all resources are identified by their path.
+
+Welo::Data resources do not have all the semantics you may dream of when
+building an application with lots of searches, but it fits well cases where you
+create/pick/save/delete items one by one.
+
+Welo::Data ships with (simple) adapaters for:
+- em-redis
+- em-mongodb
+- em-http-request
+
+These adapters use EventMachine and Fiber.
+A minimal knowledge of these is preferred but not mandatory.
+
## Installation
@@ -18,7 +34,7 @@ Or install it yourself as:
## Usage
-TODO: Write usage instructions here
+Please have a look at examples in ./samples/
## Contributing
View
@@ -0,0 +1,42 @@
+
+require 'welo'
+require 'welo-data'
+require 'welo-data/adapters/redis'
+require 'eventmachine'
+require 'fiber'
+
+class Post
+ include Welo::Resource
+ include Welo::Data::Resource
+ attr_accessor :title, :text
+ identify :default, [:id]
+ perspective :default, [:title, :text]
+end
+
+class User
+ include Welo::Resource
+ include Welo::Data::Resource
+ attr_accessor :name, :posts
+ identify :default, [:name]
+ perspective :default, [:name, :posts]
+ relationship :posts, :Post, [:many]
+end
+
+EM.run do
+ Fiber.new do
+ redis = Welo::Data::Adapters::Redis.new
+
+ jon = User.new(:name => 'jon', :posts => [])
+ post = Post.new(:title => 'hello', :text => 'Hello World')
+ jon.posts << post
+ redis.save jon
+ redis.save post # note that we need to save the post independently
+
+ jon = redis.read(User, '/user/jon')
+ # here we need to read posts because jon.posts is actually an array of Promise resources (i.e., pointer to other resources)
+ # posts could be retrieve from another place (e.g., via an http adapter)
+ jon.posts.map!{|promise| redis.read(Post, promise.path)}
+
+ puts jon.to_json(:default)
+ end.resume
+end

0 comments on commit 54b48dd

Please sign in to comment.