Skip to content

Instantly share code, notes, and snippets.

@rfunduk
Created August 16, 2011 16:17
Show Gist options
  • Save rfunduk/1149458 to your computer and use it in GitHub Desktop.
Save rfunduk/1149458 to your computer and use it in GitHub Desktop.
Code snippets for http://ryanfunduk.com/mongorb
class Document
class NotFound < StandardError; end
class InvalidFieldName < StandardError; end
@@__cache = {}
@@connection = nil
@@database_name = nil
@@database = nil
def self.cached
class_eval %(class << self; @@__cache['#{name}'] = {}; end)
end
def self.references( obj, klass=nil )
obj = obj.to_s
klass ||= obj.capitalize
class_eval %{
def #{obj}
from_extra '#{obj}', #{klass}
end
def #{obj}=( val )
@doc['#{obj}'] = val.id
@extra.delete '#{obj}'
#{obj}
end
}
end
def self.field( name )
raise InvalidFieldName if self.instance_methods.include?( name.to_s )
class_eval %{
def #{name};@doc['#{name}'];end
def #{name}=( val );@doc['#{name}'] = val;end
}
end
def self.connection=(value, db_name)
raise "You must supply a database name!" unless db_name.is_a? String
@@database_name = db_name
@@connection = value
end
def self.connection
@@connection
end
def self.database
@@database ||= self.connection.db( @@database_name )
end
def initialize( doc, no_save=false )
raise NotFound unless doc.is_a? Hash
@doc = doc
@extra = {}
@extra['no_save'] = no_save
end
def []( field )
@doc.has_key?( field ) ? @doc[field] : @extra[field]
end
def []=( field, value )
if @extra.has_key?( field )
@extra[field] = value
else
@doc[field] = value
end
end
def ==( other )
id == other.id rescue false
end
def id
@doc['_id']
end
def merge!( hash )
@doc.merge! hash.stringify_keys
end
def save
raise "Saving is disabled for light model instances!" if @extra['no_save']
collection.save( @doc )
self
end
def save!
collection.insert( @doc )
self
end
def delete
collection.remove( { '_id' => @doc['_id'] } )
end
def self.collection( override=nil )
coll = override || self.name.tableize
self.database.collection( coll )
end
def collection
self.class.collection
end
def self.find( query, options={}, bust_cache=false )
query = { '_id' => query } unless query.is_a? Hash
if @@__cache.has_key? name
query_hash = (query.inspect + options.inspect).hash
@@__cache[name][query_hash] = nil if bust_cache
@@__cache[name][query_hash] ||= self.new( collection.find( query, options ).next_document )
else
self.new( collection.find( query, options ).next_document )
end
end
def self.all( query={}, options={}, sort=nil )
no_save = options.delete( :no_save ) || false
result = collection.find( query, options ).to_a.collect do |d|
self.new( d, no_save )
end
sort = '_id' if sort == true # default sort key is _id
result = result.sort { |a,b| a[sort] <=> b[sort] } if sort
result
end
private
def from_extra( field, klass )
return nil unless @doc[field]
if @extra.has_key? field
@extra[field]
else
@extra[field] = klass.find( @doc[field] )
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment