github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

addywaddy / couchsurfer

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 21
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (4)
    • attachments
    • master ✓
    • query
    • typhoeus_backend
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

ORM based on CouchRest — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Bumped up to 0.3.3 
Adam Groves (author)
Wed Jan 27 04:07:36 -0800 2010
commit  955810ae692b17f4f4f9b3678e54481e98307a6b
tree    c816437136380a294329e16802f4cd9746f3f53d
parent  67b1431593fd50028607b770759215fb8dabccd3
couchsurfer /
name age
history
message
file .gitignore Thu Jan 22 04:20:04 -0800 2009 Updated .gitignore [addywaddy]
file CHANGELOG.md Loading commit data...
file History.txt Tue Jan 20 15:23:24 -0800 2009 Added markdown README [addywaddy]
file LICENSE Thu Jan 22 06:01:21 -0800 2009 Made license readable ;) [addywaddy]
file README.md
file Rakefile Wed Dec 09 15:32:34 -0800 2009 got lists to work again - couchsurfer now depe... [Adam Groves]
file VERSION.yml Wed Jan 27 04:07:36 -0800 2010 Bumped up to 0.3.3 - deleted commented-out line [Adam Groves]
file couch_surfer.gemspec Wed Jan 27 04:07:36 -0800 2010 Bumped up to 0.3.3 - deleted commented-out line [Adam Groves]
file github_gemtest.rb
directory lib/
directory spec/ Wed Jan 27 04:05:01 -0800 2010 Don't CGI escape the filename - results in " ... [Adam Groves]
directory tasks/ Tue Jan 20 14:03:12 -0800 2009 Initial commit [addywaddy]
README.md

CouchSurfer - CouchDB ORM


Description

CouchSurfer is an extraction of CouchRest::Model from the excellent CouchRest gem by J. Chris Anderson. In addition, it provides association and validation methods.

Associations

CouchSurfer provides the following 4 association kinds:

  • belongs_to
  • has_many
  • has_many :inline; and
  • has_many :through

All association kinds take an optional :class_name option should you want your association to be named differently to the associated model.

class Page
  …
  belongs_to :owner, :class_name => :user
  …
end

page = Page.create(…)
page.owner # returns an instance of user

The belongs_to associations accept two additional options - :view and query, enabling you to customise your associations to fit your needs. You must explicitly declare the view on the child model for associations to work

Example 1: basic

class User
  …
  has_many :pages
  …
end

class Page
  …
  view_by :user_id
  …
end

user = User.create(…)
10.times {Page.create(…, :user_id => user.id)}
user.pages

Example 2: with options

class Account
  …
  has_many :employees,
      :class_name, :user,
      :view  => :by_account_id_and_email,
      :query => lambda { {:startkey => [account_id, nil], :endkey => [account_id, {}]}   }
  …
end

class User
  …
  view_by :account_id, :email # see validation examples below
  …
end

account = Acccount.create(…)
10.times {User.create(…, :account_id => acount.id)}
account.employees

Example 3: :inline

class User
  …
  has_many :tasks, :inline => true
  …
end

class Task
  …
  view_by :user_id
  …
end

user = User.create(…)
10.times {user.tasks << Task.new(…)}
user.save
user.tasks

Example 4: :through

class Account
  …
  has_many :projects,
      :through => :memberships
  …
end

class Membership
  …
  view_by :account_id
  …
end

class Project
  …
  view_by :account_id, :email # see validation examples below
  …
end

account = Acccount.create(…)
10.times do
  p = Project.create(…)
  Membership.create(…, :account_id => account.id, :project_id => p.id)
end
account.projects

Note on HasManyThrough

With reference to the above example, HasManyThrough works by retrieving all memberships associated with the account, collecting their ids and running a bulk retrieval on the Project.all view, which is implicitly created for all models and, as of 0.0.4, emits it's id (see CHANGELOG).

Caveats

  • Sorting needs to be done client side
  • The results do not contain any extra information that may be present on the ':through' model.

Validations

Validations, with the exception of validates_uniqueness_of, have been implemented using the Validatable gem.

Example

class Employee
  include CouchSurfer::Model
  include CouchSurfer::Associations
  include CouchSurfer::Validations

  key_accessor :email, :account_id
  belongs_to :account

  view_by :account_id, :email
  view_by :name

  validates_presence_of :name
  validates_format_of   :email, 
    :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  validates_length_of :postcode, :is => 7, :message => "not the correct length"

  # Will use the Employee.by_name view with {:key => employee_instance.name}
  validates_uniqueness_of :name

  # Uses a custom view and key for uniqueness within a specific scope
  validates_uniqueness_of :email,
      :view => :by_account_id_and_email,
      :query => lambda{ {:key => [account_id, email]} },
      :message => "The email address for this account is taken"

end

Please check out the specs as well :)

Next

  • Error handling
  • association methods with arguments: @account.projects(:limit => 2, :offset => 1)
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server