Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoMapper - Mapping Ruby To and From Mongo

MongoMapper - Mapping Ruby To and From Mongo

Presentation at MongoChicago 2010 on mapping MongoDB to and from Ruby using MongoMapper.

John Nunemaker

October 20, 2010
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. Free Stuff Persistence Validations [presence, length, inclusion, ...] Callbacks [before/after

    validate, create, save, ...] Associations [many, belongs_to, one, ...]
  2. Free Stuff Persistence Validations [presence, length, inclusion, ...] Callbacks [before/after

    validate, create, save, ...] Associations [many, belongs_to, one, ...] Serialization [to_json]
  3. 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
  4. class Foo include MongoMapper::Document key :bar end foo = Foo.new

    foo.bar = 'Some text' # foo.bar => "Some text" foo.bar = 24 # foo.bar => 24
  5. class Item include MongoMapper::Document key :title, String key :path, String

    key :parent_id, ObjectId belongs_to :parent before_validation :set_path private def set_path self.path = parent.path + title.parameterize end end
  6. :before_save, :after_save, :before_create, :after_create, :before_update, :after_update, :before_validation, :after_validation, :before_validation_on_create, :after_validation_on_create,

    :before_validation_on_update, :after_validation_on_update, :before_destroy, :after_destroy, :validate_on_create, :validate_on_update, :validate
  7. class User include MongoMapper::Document scope :by_ages, lambda { |low, high|

    where(:age.gte => low, :age.lte => high) } end User.by_ages(13, 19).count User.by_ages(13, 19).all
  8. 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
  9. class Account include MongoMapper::Document many :sites end class Site include

    MongoMapper::Document key :account_id, ObjectId belongs_to :account end
  10. account = Account.create(:title => 'OL', :sites => [ Site.new(:title =>

    'OL', :domain => 'orderedlist.com'), Site.new(:title => 'RT', :domain => 'railstips.org'), ])
  11. [ { '_id' => ObjectID('...'), 'title' => 'OL', 'domain' =>

    'orderedlist.com' 'account_id' => ObjectID('...'), }, { '_id' => ObjectID('...'), 'title' => 'RT', 'domain' => 'railstips.org' 'account_id' => ObjectID('...'), } ]
  12. class Item include MongoMapper::Document many :data end class Datum include

    MongoMapper::EmbeddedDocument key :key, String key :value end
  13. { '_id' => ObjectID('...'), 'title' => 'MongoSF', 'data' => [

    { '_id' => ObjectID('...'), 'key' => 'description' 'value' => 'Awesome.', } ] }
  14. Powered by Plugins MongoMapper is associations, callbacks, clone, descendants, dirty,

    equality, identity_map, inspect, keys, logger, modifiers, pagination, persistence, protected, rails, serialization, timestamps, userstamps, validations
  15. 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
  16. 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
  17. asset = Asset.create({ :image => File.open('john.jpg', 'r'), :file => File.open('foo.txt',

    'r'), }) asset.image.id asset.image.name asset.image.type asset.image.size asset.image.read
  18. 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
  19. 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
  20. 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'