Skip to content
something at the intersection of Rails3, CouchDB and rufus-jig (Ruby)
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
CHANGELOG.txt
LICENSE.txt
README.rdoc
Rakefile
TODO.txt
rufus-doric.gemspec

README.rdoc

rufus-doric

some Ruby lib at the intersection of Rails3, CouchDB and rufus-jig.

how does rufus-doric determine which Couch database to use ?

At first it determines which CouchDB server to use via this code :

def self.couch_url

  if defined?(Rails) # rails config/couch_url.txt
    return File.read(Rails.root.join('config', 'couch_url.txt')).strip
  end
  if File.exist?('couch_url.txt') # current working directory
    return File.read('couch_url.txt').strip
  end

  'http://127.0.0.1:5984' # the default
end

Then the database is determined by calling Rufus::Doric.db(name)

p Rufus::Doric.db('mydb', :url_only => true)
  # => "http://127.0.0.1:5984/mydb_test"

p Rufus::Doric.db('mydb', :env => 'camelia', :url_only => true)
  # => "http://127.0.0.1:5984/mydb_camelia"

p Rufus::Doric.db('mydb', :env => 'camelia')
  # => #<Rufus::Jig::Couch:0x00000102325120
  #      @http=#<Rufus::Jig::Http:0x00000102324908 @host="127.0.0.1",
  #        @port=5984, @path="/toot_test", ...>...>

In summary, you only have to create a couch_url.txt file that contains the ://{host}:{port} of your couch server. The rest is taken care of.

Rufus::Doric::Model usage

One document per instance.

class Item < Rufus::Doric::Model

  db :doric
    # in which db it goes (remember that an _env suffix is added)

  doric_type :items
    # 'doric_type' field

  _id_field :name
    # _id field is determined from field 'name'

  h_accessor :name
  h_accessor :supplier

  validates :supplier, :presence => true
end

There is more, please look at the test/ directory to discover Model.

Rufus::Doric::OneDocModel usage

I use this for 'users' models.

class User < Rufus::Doric::OneDocModel

  db 'doric'
  doc_id :users

  h_accessor :locale
  h_accessor :email
  h_reader :password

  validates :password, :presence => true
end

It places all the users in one document (whereas Rufus::Doric::Model has 1 document per instance/record).

Look at the test/ directory to learn more about OneDocModel.

Rufus::Doric::Value usage

Value is about Couch documents containing a single 'value' field (apart from _id and _rev)

Given

class Tuples < Rufus::Doric::Value

  doc_id :tuples
  db :doric

  def to_s
    value.sort.join(' ')
  end
end

and a document 'tuples' in the database 'doric' :

{
  "_id": "tuples",
  "value": [ "alpha", "bravo", "charly" ]
}

this can be done :

p Tuples.load.to_s
  # => "alpha bravo charly"

tuples = Tuples.load
tuples.value << 'borneo'
tuples.save!

p Tuples.load.to_s
  # => "alpha borneo bravo charly"

Also :

tuples = Tuples.new(
  '_id' => 'tuples', 'value' => %w[ alpha beta delta gamma ]).save!

p Tuples.load.to_s
  # => "alpha beta delta gamma"

Rufus::Doric::Value and h_shortcut

Most of the time, I use Rufus::Doric::Value to store a hash of things :

class Misc < Rufus::Doric::Value

  doc_id :misc
  db :doric

  h_shortcut :product_lines
  h_shortcut :purposes
end

with :

{
  "_id": "misc",
  "value": {
    "product_lines" : [
      "blue_coat", "ulticom", "znyx"
    ],
    "purposes": [
      "stock", "non_stock", "replace", "rma", "loan"
    ]
  }
}

then in my app, I just do

p Misc.product_lines
  # => [ "blue_coat", "ulticom", "znyx" ]

p Misc.purposes
  # => [ "stock", "non_stock", "replace", "rma", "loan" ]

'fixtures' usage

Given a fixture tree

fixtures
`-- test
    `-- doric
        |-- 69247b__picture.jpg
        |-- 69249__picture.jpg
        |-- product0.json
        |-- product1.json
        |-- users.json
        |-- users__jami.png
            `-- users__john.jpg

this

require 'rufus/doric/fixtures'
Rufus::Doric::Fixtures.load('http://127.0.0.1:5984', 'fixtures/test')

will load the documents (and their attachements) into 127.0.0.1:5984/doric_test

credits

Claudio Petasecca Donati - for all his help Austin Ziegler @halostatue - for the mime-types gem

license

MIT

Something went wrong with that request. Please try again.