Mongo mapper

  • View
    5.045

  • Download
    6

Embed Size (px)

DESCRIPTION

 

Text of Mongo mapper

  • 1. MongoMapper Mapping Ruby To and From Mongo MongoSF San Francisco, CA John Nunemaker April 30, 2010 Ordered List
  • 2. Using Extending Prophesying
  • 3. Using Extending Prophesying
  • 4. ...and many more.
  • 5. class Item end
  • 6. class Item include MongoMapper::Document end
  • 7. class Datum include MongoMapper::EmbeddedDocument end
  • 8. Free Stu
  • 9. Free Stu Persistence
  • 10. Free Stu Persistence Validations [presence, length, inclusion, ...]
  • 11. Free Stu Persistence Validations [presence, length, inclusion, ...] Callbacks [before/after validate, create, save, ...]
  • 12. Free Stu Persistence Validations [presence, length, inclusion, ...] Callbacks [before/after validate, create, save, ...] Associations [many, belongs_to, one, ...]
  • 13. Free Stu Persistence Validations [presence, length, inclusion, ...] Callbacks [before/after validate, create, save, ...] Associations [many, belongs_to, one, ...] Serialization [to_json]
  • 14. Persistence Never gonna give you up
  • 15. item = Item.create({ :title => 'MongoSF', :location => 'San Fran', :when => Time.now })
  • 16. puts item.to_mongo { "_id" => ObjectID('4bd8cc5cbcd1b313b3000001'), "title" => "MongoSF", "location" => "San Fran", "when" => Wed Apr 28 17:01:32 -0700 2010 }
  • 17. item = Item.new item[:title] = 'MongoSF' item[:location] = 'San Fran' item[:when] = Time.now item.save
  • 18. puts item.to_mongo { "_id" => ObjectID('4bd8cc5cbcd1b313b3000001'), "title" => "MongoSF", "location" => "San Fran", "when" => Wed Apr 28 17:01:32 -0700 2010 }
  • 19. Types What you be baby boo?
  • 20. class Item include MongoMapper::Document key :title, String key :path, String end
  • 21. But Mongo is Schema-less?
  • 22. Instead of database schema Think App Schema
  • 23. Built-in Types Array, Binary, Boolean, Date, Float, Hash, Integer, Nil, ObjectId, Set, String, Time
  • 24. Custom Types Its shake and bake and I helped!
  • 25. class Set def self.to_mongo(value) value.to_a end def self.from_mongo(value) Set.new(value || []) end end
  • 26. 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
  • 27. class User include MongoMapper::Document key :email, DowncasedString end
  • 28. Typeless I do not know who I am
  • 29. 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
  • 30. Validations Currently using fork of validatable
  • 31. class Item include MongoMapper::Document key :title, String validates_presence_of :title end
  • 32. class Item include MongoMapper::Document key :title, String, :required => true end
  • 33. validates_presence_of validates_length_of validates_format_of validates_numericality_of validates_acceptance_of validates_confirmation_of validates_inclusion_of validates_exclusion_of
  • 34. Callbacks Ripped from AS2s cold, dead ngers
  • 35. 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
  • 36. :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
  • 37. Associations I belong to you
  • 38. to Docs belongs_to, one, many, many :in
  • 39. class Account include MongoMapper::Document many :sites end class Site include MongoMapper::Document key :account_id, ObjectId belongs_to :account end
  • 40.