Created
February 24, 2011 14:38
-
-
Save bdarcus/842225 to your computer and use it in GitHub Desktop.
how one might model something like zotero with riak and ripple dsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose') | |
, Schema = mongoose.Schema; | |
var User = new Schema({ | |
name : String | |
, username : { type: String, unique: true } | |
}); | |
var Comment = new Schema({ | |
author : User | |
, body : String | |
, updated : Date | |
}); | |
var Contributor = new Schema({ | |
name : String | |
}); | |
var Source = new Schema({ | |
id : { type: String, unique: true } | |
, type : { type: String, enum: ['article', 'book', 'chapter', 'periodical'] } | |
, authors : [Contributor] | |
, editors : [Contributor] | |
, title : { type: String, index: true } | |
, container : Source | |
, issued : { type: Date } | |
, doi : String | |
, uri : String | |
, volume : String | |
, issue : String | |
, pages : String | |
, tags : [String] | |
, comments : [Comment] | |
}); | |
function slugGenerator (options){ | |
options = options || {}; | |
var key = options.key || 'title'; | |
return function slugGenerator(schema){ | |
schema.path(key).set(function(v){ | |
this.slug = v.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/-+/g, ''); | |
return v; | |
}); | |
}; | |
}; | |
Source.plugin(slugGenerator()); | |
mongoose.model('Source', Source); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# similar (though incomplete) model with redis object-mapper | |
require 'rubygems' | |
require 'ohm' | |
class Source < Ohm::Model | |
attribute :title | |
attribute :issued | |
attribute :periodical | |
attribute :volume | |
attribute :issue | |
attribute :uri | |
attribute :accessed | |
reference :publisher, Organization | |
set :authors, Contributor | |
set :editors, Contributor | |
index :title | |
end | |
book = Reference.create(:title => "Book Title") | |
book.authors << Contributor(:name => "Jane Doe") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'ripple' | |
# Experiment in using a Ruby DSL on top of Riak. | |
# One thing a little dicey now is notes. | |
client = Riak::Client.new(:protocol => "pbc") | |
bucket = client.bucket("references") | |
class Collection | |
include Ripple::Document | |
one :owner, :class_name => "User" | |
# we use Riak links, which are ordered, for relations | |
many :items | |
end | |
class Item | |
include Ripple::Document | |
one :collection | |
one :source | |
property :updated, Time, :default => proc { Time.now } | |
many :notes | |
# tags? | |
end | |
class Note | |
include Ripple::Document | |
# we attach notes to Source to more easily aggregate notes across users | |
one :source | |
property :updated, Time, :default => proc { Time.now } | |
property :content, String | |
property :private, Boolean, :default => true # by default, notes are private | |
# tags? | |
end | |
class Source | |
include Ripple::Document | |
property :title, String | |
property :issued, String | |
property :containerTitle, String | |
property :volume, String | |
property :issue, String | |
property :pages, String | |
property :doi, String | |
property :uri, String | |
property :isbn, String | |
many :authors, :class_name => "Contributor" | |
many :editors, :class_name => "Contributor" | |
many :translators, :class_name => "Contributor" | |
many :notes | |
end | |
class Contributor | |
# we need a flexible object to allow for international name forms and sorting, corporate names, etc; let's keep it simple | |
include Ripple::Document | |
property :name, String # display form of name | |
property :sortString, String # sort form of name, if different than the display name | |
property :person, Boolean, :default => true # we assume by default a person, but allow corporate authors | |
end | |
class User | |
include Ripple::Document | |
property :name, String | |
end | |
collection = Collection.new | |
item = Item.new | |
source = Source.new(:title => "The Title") | |
source.authors << Contributor.new(:name => "John Doe", :sortString => "Doe, John") | |
source.authors << Contributor.new(:name => "Jane Smith", :sortString => "Smith, Jane") | |
item.source = source | |
collection.items << item | |
puts item.source.title | |
item.source.authors.each { |author| puts author.name } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment