FoGhost / SimpleResource

A simple DB mapper written in Ruby, offering ActiveRecord-like interface, indexing and write-locking functions with schemeless records. Currently supports MySQL and TokyoTyrant for the backends.

Home Page:http://d.hatena.ne.jp/kazuk_i

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Installation on Rails
=====================

0. Dependencies:
    * JSON gem : http://flori.github.com/json/
    * MySQL/Ruby
    * ActiveRecord
    * memcached gem : http://blog.evanweaver.com/files/doc/fauna/memcached/files/README.html
    * memcache-client gem : http://seattlerb.rubyforge.org/memcache-client/

1. Place all files into lib/simple_resource on your Rails project.

2. Create two new tables on your database that store entity records and index records respectively. An example below:

CREATE TABLE `entities` (
  `entity_group_entity_id` VARCHAR(250) NOT NULL PRIMARY KEY,
  `body` MEDIUMBLOB,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL
) ENGINE=InnoDB;

CREATE TABLE `indices` (
  `index_id` INT NOT NULL,
  `entity_id` VARCHAR(200) NOT NULL,
  `sort` INT NOT NULL,
  PRIMARY KEY  (`index_id`,`entity_id`),
  INDEX (`index_id`, `sort`)
) ENGINE=InnoDB;



3. Add two new db connection settings to your config/database.yml. An example below:

entity:
  adapter: mysql
  encoding: utf8
  shards:
    - {host: localhost, database: your_db_name, username: , password: , port: }

index:
  adapter: mysql
  encoding: utf8
  database: your_db_name
  pool: 5
  username: 
  password: 
  host: localhost


4. Add the following 3 lines to your config/development.rb

MEMCACHE_HOST = ['localhost:11211']
MEMCACHE_PREFIX = 'some_prefix_'

5. Check if it works correctly:

$ script/console

>> class SimpleTest < SimpleResource::Base
>>   include SimpleResource::MysqlEntityBackend
>> end
=> SimpleTest

?> e = SimpleTest.create("foo" => "bar")
=> #<SimpleTest:0xb7649670 @attributes={"id"=>1, "foo"=>"bar"}>
>> e.foo
=> "bar"
>> e.bar = "zoo"
=> "zoo"
>> e
=> #<SimpleTest:0xb7649670 @attributes={"id"=>1, "foo"=>"bar", "bar"=>"zoo"}>
>> e.save
=> nil
>> e.id
=> 1
>> SimpleTest.find(1)
=> #<SimpleTest:0xb762fa18 @attributes={"id"=>1, "foo"=>"bar", "bar"=>"zoo"}>
>> e.destroy
=> nil



Note
====

All keys of stored attributes should be String, not Symbol.
All values of stored attributes should be either String, Integer, Float, Hash or Array, recursively.
(Since SimpleResource uses json format for data store, attributes should be a form which can be represented in json.)


Licence
=======

 GNU General Public License


Author
======

 Kazki Matz (kazki.matz@gmail.com)


About

A simple DB mapper written in Ruby, offering ActiveRecord-like interface, indexing and write-locking functions with schemeless records. Currently supports MySQL and TokyoTyrant for the backends.

http://d.hatena.ne.jp/kazuk_i