Slide 1

Slide 1 text

Ordered List John Nunemaker Mongo Chicago October 20, 2010 MongoMapper Mapping Ruby To and From Mongo

Slide 2

Slide 2 text

Using Extending Prophesying

Slide 3

Slide 3 text

Using Extending Prophesying

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

class Item end

Slide 6

Slide 6 text

class Item include MongoMapper::Document end

Slide 7

Slide 7 text

class Datum include MongoMapper::EmbeddedDocument end

Slide 8

Slide 8 text

Free Stuff

Slide 9

Slide 9 text

Free Stuff Persistence

Slide 10

Slide 10 text

Free Stuff Persistence Validations [presence, length, inclusion, ...]

Slide 11

Slide 11 text

Free Stuff Persistence Validations [presence, length, inclusion, ...] Callbacks [before/after validate, create, save, ...]

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Persistence Never gonna give you up

Slide 15

Slide 15 text

item = Item.create({ :title => 'MongoSF', :location => 'San Fran', :when => Time.now })

Slide 16

Slide 16 text

puts item.to_mongo { "_id" => ObjectID('4bd8cc5cbcd1b313b3000001'), "title" => "MongoSF", "location" => "San Fran", "when" => Wed Apr 28 17:01:32 -0700 2010 }

Slide 17

Slide 17 text

item = Item.new item[:title] = 'MongoSF' item[:location] = 'San Fran' item[:when] = Time.now item.save

Slide 18

Slide 18 text

puts item.to_mongo { "_id" => ObjectID('4bd8cc5cbcd1b313b3000001'), "title" => "MongoSF", "location" => "San Fran", "when" => Wed Apr 28 17:01:32 -0700 2010 }

Slide 19

Slide 19 text

Types What you be baby boo?

Slide 20

Slide 20 text

class Item include MongoMapper::Document key :title, String key :path, String end

Slide 21

Slide 21 text

But Mongo is Schema-less?

Slide 22

Slide 22 text

Think App Schema Instead of database schema

Slide 23

Slide 23 text

Built-in Types Array, Binary, Boolean, Date, Float, Hash, Integer, Nil, ObjectId, Set, String, Time

Slide 24

Slide 24 text

Custom Types Its shake and bake and I helped!

Slide 25

Slide 25 text

class Set def self.to_mongo(value) value.to_a end def self.from_mongo(value) Set.new(value || []) end end

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

class User include MongoMapper::Document key :email, DowncasedString end

Slide 28

Slide 28 text

Typeless I do not know who I am

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Validations Currently using fork of validatable

Slide 31

Slide 31 text

class Item include MongoMapper::Document key :title, String validates_presence_of :title end

Slide 32

Slide 32 text

class Item include MongoMapper::Document key :title, String, :required => true end

Slide 33

Slide 33 text

validates_presence_of validates_length_of validates_format_of validates_numericality_of validates_acceptance_of validates_confirmation_of validates_inclusion_of validates_exclusion_of

Slide 34

Slide 34 text

Callbacks Ripped from AS2’s cold, dead fingers

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

: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

Slide 37

Slide 37 text

Scopes Plucky Power!

Slide 38

Slide 38 text

class User include MongoMapper::Document scope :johns, where(:name => 'John') end User.johns.all User.johns.first

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Associations I belong to you

Slide 42

Slide 42 text

to Docs belongs_to, one, many, many :in

Slide 43

Slide 43 text

class Account include MongoMapper::Document many :sites end class Site include MongoMapper::Document key :account_id, ObjectId belongs_to :account end

Slide 44

Slide 44 text

account = Account.create(:title => 'OL', :sites => [ Site.new(:title => 'OL', :domain => 'orderedlist.com'), Site.new(:title => 'RT', :domain => 'railstips.org'), ])

Slide 45

Slide 45 text

[ { '_id' => ObjectID('...'), 'title' => 'OL', 'domain' => 'orderedlist.com' 'account_id' => ObjectID('...'), }, { '_id' => ObjectID('...'), 'title' => 'RT', 'domain' => 'railstips.org' 'account_id' => ObjectID('...'), } ]

Slide 46

Slide 46 text

to Embedded Docs many, one

Slide 47

Slide 47 text

class Item include MongoMapper::Document many :data end class Datum include MongoMapper::EmbeddedDocument key :key, String key :value end

Slide 48

Slide 48 text

Item.create(:title => 'MongoSF', :data => [ Datum.new(:key => 'description', :value => 'Awesome.') ])

Slide 49

Slide 49 text

{ '_id' => ObjectID('...'), 'title' => 'MongoSF', 'data' => [ { '_id' => ObjectID('...'), 'key' => 'description' 'value' => 'Awesome.', } ] }

Slide 50

Slide 50 text

Using Extending Prophesying

Slide 51

Slide 51 text

Plugins Conventional way to extend

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

class Foo include MongoMapper::Document plugin ActsAsListFu end Foo.reorder(...) Foo.new.move_to_top

Slide 58

Slide 58 text

Good Example Joint: github.com/jnunemaker/joint

Slide 59

Slide 59 text

class Asset include MongoMapper::Document plugin Joint attachment :image attachment :file end

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Descendant Appends Fancy Schmancy and Stolen

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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'

Slide 65

Slide 65 text

module IdentityMapAddition def self.included(model) model.plugin MongoMapper::Plugins::IdentityMap end end MongoMapper::Document.append_inclusions(IdentityMapAddition)

Slide 66

Slide 66 text

Using Extending Prophesying

Slide 67

Slide 67 text

Active Model rails3 branch

Slide 68

Slide 68 text

Documentation http://mongomapper.com

Slide 69

Slide 69 text

“ Brian Ford @brixen Documentation: n. paradoxically repeating yourself so you do not need to repeat yourself.

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Ordered List Thank you! [email protected] John Nunemaker Mongo Chicago October 20, 2010 @jnunemaker