$30 off During Our Annual Pro Sale. View Details »

MongoMapper - Mapping Ruby to and from Mongo

MongoMapper - Mapping Ruby to and from Mongo

John Nunemaker

April 30, 2010
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. Ordered List John Nunemaker MongoSF San Francisco, CA April 30,

    2010 MongoMapper Mapping Ruby To and From Mongo
  2. Free Stuff Persistence Validations [presence, length, inclusion, ...] Callbacks [before/after

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

    validate, create, save, ...] Associations [many, belongs_to, one, ...] Serialization [to_json]
  4. class DowncasedString def self.to_mongo(value) value.nil? ? nil : value.to_s.downcase end

    def self.from_mongo(value) value.nil? ? nil : value.to_s.downcase end end
  5. 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
  6. 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
  7. :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
  8. class Account include MongoMapper::Document many :sites end class Site include

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

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

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

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

    { '_id' => ObjectID('...'), 'key' => 'description' 'value' => 'Awesome.', } ] }
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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'