active_document
文件大小: unknow
源码售价: 5 个金币 积分规则     积分充值
资源说明:Active models in Berkeley DB.
= ActiveDocument

ActiveDocument is a persistent Model store built on Berkeley DB. It was inspired by
ActiveRecord, and in some cases, it can be used as a drop-in replacement. The performance
of ActiveDocument can exceed a traditional ORM for many applications, because the database
is stored locally and all lookups must use a predefined index. Also, attributes do not
have to be cast after they are read from the database like in ActiveRecord. Instead, Ruby
objects are stored directly in Berkeley DB and loaded using Marshal, which makes loading
objects much faster. For more information on the diffences between Berkeley DB and a
relational Database, see (http://www.oracle.com/database/docs/Berkeley-DB-v-Relational.pdf).

== Usage:

  require 'rubygems'
  require 'active_document'

  class User < ActiveDocument::Base
    path '/data/bdb'
    accessor :first_name, :last_name, :username, :email_address, :tags

    primary_key :username
    index_by [:last_name, :first_name]
    index_by :email_address, :unique => true
    index_by :tags, :multi_key => true
  end

  User.create(
    :first_name => 'John',
    :last_name  => 'Stewart',
    :username   => 'lefty',
    :email_address => 'john@thedailyshow.com',
    :tags => [:funny, :liberal]
  )

  User.create(
    :first_name => 'Martha',
    :last_name  => 'Stewart',
    :username   => 'helen',
    :email_address => 'martha@marthastewart.com',
    :tags => [:conservative, :convict]
  )

  User.create(
    :first_name => 'Stephen',
    :last_name  => 'Colbert',
    :username   => 'steve',
    :email_address => 'steve@thereport.com',
    :tags => [:conservative, :funny]
  )
  
  User.find('lefty').attributes
  => {"username"=>"lefty", "last_name"=>"Stewart", "email_address"=>"john@thedailyshow.com", "first_name"=>"John"}

  User.find_by_email_address("john@thedailyshow.com").username
  => "lefty"

  User.find_all_by_last_name("Stewart").collect {|u| u.first_name}
  => ["John", "Martha"]

  User.find_all_by_tag(:funny).collect {|u| u.username}
  => ["lefty", "steve"]

=== Complex finds:

Any find can take multiple keys, a key range, or multiple key ranges.

  User.create(
    :first_name => 'Will',
    :last_name  => 'Smith',
    :username   => 'legend',
    :email_address => 'will@smith.com',
    :tags => [:actor, :rapper]
  )

  User.find_all_by_last_name("Stewart", "Smith").collect {|u| u.username}
  => ["lefty", "helen", "legend"]

  User.find_all_by_last_name("Smith".."Stuart").collect {|u| u.username}
  => ["legend", "lefty", "helen"]

  User.find_all_by_last_name("Smith".."Stuart", "Colbert").collect {|u| u.username}
  => ["legend", "lefty", "helen", "steve"]

  User.find_all_by_last_name("Aardvark".."Daisy", "Smith".."Stuart").collect {|u| u.username}
  => ["steve", "legend", "lefty", "helen"]

=== Limit and Offset:

Any find can also take :limit, :offset, :page, and :per_page as options. These can be used for paginating large lists.

  User.find_all_by_username(:limit => 2).collect {|u| u.username}
  => ["helen", "lefty"]

  User.find_all_by_username(:limit => 2, :offset => 2).collect {|u| u.username}
  => ["legend", "steve"]

  User.find_all_by_username(:per_page => 2, :page => 1).collect {|u| u.username}
  => ["helen", "lefty"]

  User.find_all_by_username(:per_page => 2, :page => 2).collect {|u| u.username}
  => ["legend", "steve"]

== Install:

  sudo gem install active_document -s http://gemcutter.org

== Dependencies:

* {berkeley-db}[http://www.oracle.com/technology/software/products/berkeley-db/db/index.html] (4.8 recommended)
* {bdb}[http://github.com/ninjudd/bdb]
* {tuple}[http://github.com/ninjudd/tuple]

== License:

Copyright (c) 2009 Justin Balthrop, Geni.com; Published under The MIT License, see LICENSE

本源码包内暂不包含可直接显示的源代码文件,请下载源码包。