class DowncasedString def self.to_mongo(value) value.nil? ? nil : value.to_s.downcase end def self.from_mongo(value) DowncasedString.to_mongo(value) end end
class User include MongoMapper::Document scope :teen, where(:age.gte => 13, :age.lte => 19) def self.by_tag(tag) where(:tags => tag) end end User.teen.by_tag('bieber').count User.by_tag('bieber').teen.all
module MongoMapper module Plugins def plugins @plugins ||= [] end def plugin(mod) extend mod::ClassMethods if mod.const_defined?(:ClassMethods) include mod::InstanceMethods if mod.const_defined?(:InstanceMethods) mod.configure(self) if mod.respond_to?(:configure) plugins << mod end end end
module ActsAsListFu module ClassMethods def reorder(ids) # reorder ids... end end module InstanceMethods def move_to_top # move to top end end def self.configure(model) model.key :position, Integer, :default => 1 end end
module FancySchmancy def some_method puts 'some method' end end MongoMapper::Document.append_extensions(FancySchmancy) class Foo include MongoMapper::Document end Foo.some_method # puts 'some method' Foo.new.some_method # NoMethodError
module FancySchmancy def some_method puts 'some method' end end MongoMapper::Document.append_inclusions(FancySchmancy) class Foo include MongoMapper::Document end Foo.new.some_method # puts 'some method' Foo.some_method # NoMethodError
module FancySchmancy def some_method puts 'some method' end end class Foo include MongoMapper::Document end MongoMapper::Document.append_extensions(FancySchmancy) class Bar include MongoMapper::Document end Foo.some_method # puts 'some method' Bar.some_method # puts 'some method'
module IdentityMapAddition def self.included(model) model.plugin MongoMapper::Plugins::IdentityMap end end MongoMapper::Document.append_inclusions(IdentityMapAddition)