SlideShare a Scribd company logo
Tame accidental complexity
       Introduction to NoSQL with MongoMapper




Giordano Scalzo
Tame Accidental Complexity with Ruby and MongoMapper
I’m not here to talk about performance
I’m not here to talk about scalability
but I’m here to talk about simplicity
Rails has been a first step
Anatomy of a Rails Application
Anatomy of a Rails Application




view
Anatomy of a Rails Application




view       controller
Anatomy of a Rails Application




view       controller      model
Different languages
html+css




   view        controller       model
Different languages
html+css        oop




   view        controller       model
Different languages
html+css        oop               sql




   view        controller       model
Impedance mismatch
html+css        oop               sql




   view         controller      model
Origin of Sql

SQL
From Wikipedia, the free encyclopedia




SQL (...), is a database computer language
designed for managing data in relational
database management systems (RDBMS)
Origin of Sql

SQL
From Wikipedia, the free encyclopedia




SQL (...), is a database computer language
designed for managing data in relational
database management systems (RDBMS)
We need persistent objects!
We need persistent objects!




class User
  def initialize(username, password)
    @username = username
    @password = password
  end
end
We need persistent objects!




   {
       username: "giordano",
       password: "123"
   }
ActiveRecord tries its best
We need something different
Persistence
Persistence




class User
  include MongoMapper::Document
end
Persistence


class User
  include MongoMapper::Document
end

user = User.create({
   :username => "giordano",
   :password => "123"
})
user.save
Persistence


class User
  include MongoMapper::Document
end

user = User.create({
   :username => "giordano",
   :password => "123"
})
user.save

puts User.all.last.to_mongo
Persistence




{
    "_id"=>BSON::ObjectId('4d643a274d8ff683dd000001'),
    "username"=>"giordano",
    "password"=>"123"
}
Types
Types




class User
  include MongoMapper::Document
  key :username, String
  key :password , String
end
Built-in Types




Array, Binary, Boolean, Date,
Float, Hash, Integer, Nil,
ObjectId, Set, String, Time
Custom Types



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
Custom Types




class User
  include MongoMapper::Document
  key :username, String
  key :password , String
  key :email, DowncasedString
end
Custom Types



user = User.new
user.username = "giordano"
user.password = "123"
user.email = "Giordano.Scalzo@CleanCode.it"

user.save

puts User.all.last.to_mongo
Custom Types




{
 "_id"=>BSON::ObjectId('4d6442d94d8ff684d3000001'),
 "username"=>"giordano", "password"=>"123",
 "email"=>"giordano.scalzo@cleancode.it"
}
Embedded Documents
Embedded Documents



class Task
  include MongoMapper::EmbeddedDocument
  key :description, String
  key :pomodori, Integer
  key :is_done, Boolean
end
Embedded Documents



class User
  include MongoMapper::Document
  key :username, String
  key :password , String
  key :email, DowncasedString
  many :tasks
end
Embedded Documents

user.tasks << Task.new({
   description: 'refactor server',
   pomodori: 8,
   is_done: false
})

user.tasks << Task.new({
   description: 'timer sound',
   pomodori: 2,
   is_done: false
})
Embedded Documents
{
"_id"=>BSON::ObjectId('4d6575e84d8ff692e6000001'),
  "username"=>"giordano", "password"=>"123",
  "email"=>"giordano.scalzo@cleancode.it",
  "tasks"=>[{
    "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000002'),
    "description"=>"refactor server",
    "pomodori"=>8, "is_done"=>false
   }, {
    "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000003'),
    "description"=>"timer sound",
    "pomodori"=>2, "is_done"=>false
   }]
}
Embedded Documents
{
"_id"=>BSON::ObjectId('4d6575e84d8ff692e6000001'),
  "username"=>"giordano", "password"=>"123",
  "email"=>"giordano.scalzo@cleancode.it",
  "tasks"=>[{
    "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000002'),
    "description"=>"refactor server",
    "pomodori"=>8, "is_done"=>false
   }, {
    "_id"=>BSON::ObjectId('4d6575e84d8ff692e6000003'),
    "description"=>"timer sound",
    "pomodori"=>2, "is_done"=>false
   }]
}
Documents




class Task
  include MongoMapper::Document
  key :description, String
  key :pomodori, Integer
  key :is_done, Boolean
end
Documents

p User.all.last.to_mongo


{
 "_id"=>BSON::ObjectId('4d657e924d8ff6949c000001'),
 "username"=>"giordano", "password"=>"123",
 "email"=>"giordano.scalzo@cleancode.it"
}
Documents

p User.all.last.tasks
[#<Task _id:
    BSON::ObjectId('4d65822b4d8ff69542000002'),
    description: "refactor server",
    pomodori: 8, is_done: false,
    user_id: BSON::ObjectId('4d65822b4d8ff69542000001')
  >,
  #<Task _id:
    BSON::ObjectId('4d65822b4d8ff69542000003'),
    description: "timer sound",
    pomodori: 2, is_done: false,
    user_id: BSON::ObjectId('4d65822b4d8ff69542000001')
  >
]
Validations & Callbacks
Validations & Callbacks



class User
  include MongoMapper::Document
  key :username, String,
  validates_presence_of :username
  key :password, String
  validates_presence_of :password
end
Validations & Callbacks




class User
  include MongoMapper::Document
  key :username, String, :required => true
  key :password, String, :required => true
end
Validations & Callbacks



validates_presence_of
validates_length_of
validates_format_of
validates_numericality_of
validates_acceptance_of
validates_confirmation_of
validates_inclusion_of
validates_exclusion_of
Validations & Callbacks


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
Validations & Callbacks




forked in current gem 0.8.6
using Rails3 ActiveModel in
Rails3 branch just merged
What about querying?
What about querying?




query = User.where(:last_name.exists => true,
                   :created_at.gte => from_date,
                   :created_at.lt => Time.now)
       Plucky: ActiveRecord-like language
            .skip(0).limit(5)

query.all
What about querying?




query = User.where(:last_name.exists => true,
                   :created_at.gte => from_date,
                   :created_at.lt => Time.now)
       Plucky: ActiveRecord-like language
            .skip(0).limit(5)
#<Plucky::Query created_at: {
  "$gte"=>"1",
  "$lt"=>2011-02-24 10:54:36 UTC},
  last_name: {"$exists"=>true}, limit: 5, skip: 0>
What about querying?




query = User.where(:last_name.exists => true)
            .where(:created_at.gte => from_date)
            .where(:created_at.lt => Time.now)
       Plucky: ActiveRecord-like language
            .skip(0).limit(5)
#<Plucky::Query created_at: {
  "$gte"=>"1",
  "$lt"=>2011-02-24 10:54:36 UTC},
  last_name: {"$exists"=>true}, limit: 5, skip: 0>
What about plugins?
What about plugins?
Accessible              Modifiers
Associations            Pagination
Caching                 Persistence
Callbacks               Protected
Clone                   Querying
Dirty                   Rails
Document                Safe
Dynamic Querying        Single
EmbeddedDocument        Collection
Equality                Inheritance
IdentityMap             Scopes
Indexes                 Serialization
Inspect                 Timestamps
Keys                    Userstamps
Logger                  Validations
Itʼs just a beginning
http://mongomapper.com/documentation/
http://mongoid.org/
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
Tame Accidental Complexity with Ruby and MongoMapper
giordano.scalzo@cleancode.it
@giordanoscalzo
www.slideshare.net/giordano
github.com/gscalzo

More Related Content

What's hot (20)

PDF
ZendCon2010 Doctrine MongoDB ODM
Jonathan Wage
 
ODP
Modularized Persistence - B Zsoldos
mfrancis
 
PDF
Using Dojo
Richard Paul
 
PDF
Node Access in Drupal 7 (and Drupal 8)
nyccamp
 
PDF
iOS State Preservation and Restoration
Robert Brown
 
PPTX
Django cryptography
Erik LaBianca
 
PDF
Headless Js Testing
Brian Moschel
 
PDF
Improving RDF Search Performance with Lucene and SIREN
Mike Hugo
 
DOC
Functions
G.C Reddy
 
PDF
Semantic code transformations in MetaJS
Dmytro Dogadailo
 
KEY
The Ruby/mongoDB ecosystem
Harold Giménez
 
PDF
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Andrii Lashchenko
 
PDF
SQLAlchemy Seminar
Yury Yurevich
 
PDF
[2019] Spring JPA의 사실과 오해
NHN FORWARD
 
PDF
How to survive in a BASE world
Uwe Friedrichsen
 
PDF
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
 
PDF
How to Design a Great API (using flask) [ploneconf2017]
Devon Bernard
 
PDF
Automatically Spotting Cross-language Relations
Federico Tomassetti
 
PDF
BDD, ATDD, Page Objects: The Road to Sustainable Web Testing
John Ferguson Smart Limited
 
PPTX
HTML5 - Pedro Rosa
Comunidade NetPonto
 
ZendCon2010 Doctrine MongoDB ODM
Jonathan Wage
 
Modularized Persistence - B Zsoldos
mfrancis
 
Using Dojo
Richard Paul
 
Node Access in Drupal 7 (and Drupal 8)
nyccamp
 
iOS State Preservation and Restoration
Robert Brown
 
Django cryptography
Erik LaBianca
 
Headless Js Testing
Brian Moschel
 
Improving RDF Search Performance with Lucene and SIREN
Mike Hugo
 
Functions
G.C Reddy
 
Semantic code transformations in MetaJS
Dmytro Dogadailo
 
The Ruby/mongoDB ecosystem
Harold Giménez
 
Spray Json and MongoDB Queries: Insights and Simple Tricks.
Andrii Lashchenko
 
SQLAlchemy Seminar
Yury Yurevich
 
[2019] Spring JPA의 사실과 오해
NHN FORWARD
 
How to survive in a BASE world
Uwe Friedrichsen
 
The Ring programming language version 1.8 book - Part 49 of 202
Mahmoud Samir Fayed
 
How to Design a Great API (using flask) [ploneconf2017]
Devon Bernard
 
Automatically Spotting Cross-language Relations
Federico Tomassetti
 
BDD, ATDD, Page Objects: The Road to Sustainable Web Testing
John Ferguson Smart Limited
 
HTML5 - Pedro Rosa
Comunidade NetPonto
 

Viewers also liked (6)

PDF
Better Software: introduction to good code
Giordano Scalzo
 
PDF
Refactoring In Tdd The Missing Part
Gabriele Lana
 
PDF
Tdd iPhone For Dummies
Giordano Scalzo
 
PDF
JavaScript Patterns
Giordano Scalzo
 
PDF
JavaScript Survival Guide
Giordano Scalzo
 
PPTX
JavaScript Coding Guidelines
Oleksii Prohonnyi
 
Better Software: introduction to good code
Giordano Scalzo
 
Refactoring In Tdd The Missing Part
Gabriele Lana
 
Tdd iPhone For Dummies
Giordano Scalzo
 
JavaScript Patterns
Giordano Scalzo
 
JavaScript Survival Guide
Giordano Scalzo
 
JavaScript Coding Guidelines
Oleksii Prohonnyi
 
Ad

Similar to Tame Accidental Complexity with Ruby and MongoMapper (20)

KEY
SOLID Ruby, SOLID Rails
Jens-Christian Fischer
 
PDF
Introduction to Active Record at MySQL Conference 2007
Rabble .
 
PPTX
Mongoose and MongoDB 101
Will Button
 
PPTX
Quality code by design
WP Developers Club
 
PDF
Passkeys and cbSecurity Led by Eric Peterson.pdf
Ortus Solutions, Corp
 
KEY
Building Web Service Clients with ActiveModel
pauldix
 
KEY
Building Web Service Clients with ActiveModel
pauldix
 
PDF
Python RESTful webservices with Python: Flask and Django solutions
Solution4Future
 
PDF
Android DevConference - Android Clean Architecture
iMasters
 
PDF
Fun Teaching MongoDB New Tricks
MongoDB
 
PDF
Angular JS2 Training Session #1
Paras Mendiratta
 
PDF
Functional Reactive Programming - RxSwift
Rodrigo Leite
 
PDF
The Ring programming language version 1.4.1 book - Part 13 of 31
Mahmoud Samir Fayed
 
PDF
10 Rules for Safer Code [Odoo Experience 2016]
Olivier Dony
 
PDF
10 Rules for Safer Code
Quang Ngoc
 
PDF
Ruby Development and MongoMapper (John Nunemaker)
MongoSF
 
PDF
Securing Your Application with Passkeys and cbSecurity
Ortus Solutions, Corp
 
PDF
Mongoid in the real world
Kevin Faustino
 
KEY
前端概述
Ethan Zhang
 
PPTX
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
SOLID Ruby, SOLID Rails
Jens-Christian Fischer
 
Introduction to Active Record at MySQL Conference 2007
Rabble .
 
Mongoose and MongoDB 101
Will Button
 
Quality code by design
WP Developers Club
 
Passkeys and cbSecurity Led by Eric Peterson.pdf
Ortus Solutions, Corp
 
Building Web Service Clients with ActiveModel
pauldix
 
Building Web Service Clients with ActiveModel
pauldix
 
Python RESTful webservices with Python: Flask and Django solutions
Solution4Future
 
Android DevConference - Android Clean Architecture
iMasters
 
Fun Teaching MongoDB New Tricks
MongoDB
 
Angular JS2 Training Session #1
Paras Mendiratta
 
Functional Reactive Programming - RxSwift
Rodrigo Leite
 
The Ring programming language version 1.4.1 book - Part 13 of 31
Mahmoud Samir Fayed
 
10 Rules for Safer Code [Odoo Experience 2016]
Olivier Dony
 
10 Rules for Safer Code
Quang Ngoc
 
Ruby Development and MongoMapper (John Nunemaker)
MongoSF
 
Securing Your Application with Passkeys and cbSecurity
Ortus Solutions, Corp
 
Mongoid in the real world
Kevin Faustino
 
前端概述
Ethan Zhang
 
Dev Jumpstart: Build Your First App with MongoDB
MongoDB
 
Ad

More from Giordano Scalzo (10)

PDF
The Joy Of Server Side Swift Development
Giordano Scalzo
 
PDF
How to Clone Flappy Bird in Swift
Giordano Scalzo
 
PDF
A swift introduction to Swift
Giordano Scalzo
 
PDF
Code kata
Giordano Scalzo
 
PDF
Better Software Developers
Giordano Scalzo
 
KEY
Agile Iphone Development
Giordano Scalzo
 
KEY
XpUg Coding Dojo: KataYahtzee in Ocp way
Giordano Scalzo
 
PPS
Bdd: Tdd and beyond the infinite
Giordano Scalzo
 
PDF
10 minutes of me: Giordano Scalzo's Visual Resume
Giordano Scalzo
 
PPT
Scrum in an hour
Giordano Scalzo
 
The Joy Of Server Side Swift Development
Giordano Scalzo
 
How to Clone Flappy Bird in Swift
Giordano Scalzo
 
A swift introduction to Swift
Giordano Scalzo
 
Code kata
Giordano Scalzo
 
Better Software Developers
Giordano Scalzo
 
Agile Iphone Development
Giordano Scalzo
 
XpUg Coding Dojo: KataYahtzee in Ocp way
Giordano Scalzo
 
Bdd: Tdd and beyond the infinite
Giordano Scalzo
 
10 minutes of me: Giordano Scalzo's Visual Resume
Giordano Scalzo
 
Scrum in an hour
Giordano Scalzo
 

Tame Accidental Complexity with Ruby and MongoMapper