Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Loading in …3
×
1 of 61

Tame Accidental Complexity with Ruby and MongoMapper

2

Share

Download to read offline

Gentle introduction to MongoDb with Ruby and MongoMapper

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Tame Accidental Complexity with Ruby and MongoMapper

  1. Tame accidental complexity Introduction to NoSQL with MongoMapper Giordano Scalzo
  2. I’m not here to talk about performance
  3. I’m not here to talk about scalability
  4. but I’m here to talk about simplicity
  5. Rails has been a first step
  6. Anatomy of a Rails Application
  7. Anatomy of a Rails Application view
  8. Anatomy of a Rails Application view controller
  9. Anatomy of a Rails Application view controller model
  10. Different languages html+css view controller model
  11. Different languages html+css oop view controller model
  12. Different languages html+css oop sql view controller model
  13. Impedance mismatch html+css oop sql view controller model
  14. 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)
  15. 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)
  16. We need persistent objects!
  17. We need persistent objects! class User def initialize(username, password) @username = username @password = password end end
  18. We need persistent objects! { username: "giordano", password: "123" }
  19. ActiveRecord tries its best
  20. We need something different
  21. Persistence
  22. Persistence class User include MongoMapper::Document end
  23. Persistence class User include MongoMapper::Document end user = User.create({ :username => "giordano", :password => "123" }) user.save
  24. Persistence class User include MongoMapper::Document end user = User.create({ :username => "giordano", :password => "123" }) user.save puts User.all.last.to_mongo
  25. Persistence { "_id"=>BSON::ObjectId('4d643a274d8ff683dd000001'), "username"=>"giordano", "password"=>"123" }
  26. Types
  27. Types class User include MongoMapper::Document key :username, String key :password , String end
  28. Built-in Types Array, Binary, Boolean, Date, Float, Hash, Integer, Nil, ObjectId, Set, String, Time
  29. 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
  30. Custom Types class User include MongoMapper::Document key :username, String key :password , String key :email, DowncasedString end
  31. 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
  32. Custom Types { "_id"=>BSON::ObjectId('4d6442d94d8ff684d3000001'), "username"=>"giordano", "password"=>"123", "email"=>"giordano.scalzo@cleancode.it" }
  33. Embedded Documents
  34. Embedded Documents class Task include MongoMapper::EmbeddedDocument key :description, String key :pomodori, Integer key :is_done, Boolean end
  35. Embedded Documents class User include MongoMapper::Document key :username, String key :password , String key :email, DowncasedString many :tasks end
  36. 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 })
  37. 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 }] }
  38. 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 }] }
  39. Documents class Task include MongoMapper::Document key :description, String key :pomodori, Integer key :is_done, Boolean end
  40. Documents p User.all.last.to_mongo { "_id"=>BSON::ObjectId('4d657e924d8ff6949c000001'), "username"=>"giordano", "password"=>"123", "email"=>"giordano.scalzo@cleancode.it" }
  41. 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') > ]
  42. Validations & Callbacks
  43. Validations & Callbacks class User include MongoMapper::Document key :username, String, validates_presence_of :username key :password, String validates_presence_of :password end
  44. Validations & Callbacks class User include MongoMapper::Document key :username, String, :required => true key :password, String, :required => true end
  45. 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
  46. 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
  47. Validations & Callbacks forked in current gem 0.8.6 using Rails3 ActiveModel in Rails3 branch just merged
  48. What about querying?
  49. 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
  50. 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>
  51. 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>
  52. What about plugins?
  53. 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
  54. Itʼs just a beginning
  55. http://mongomapper.com/documentation/
  56. http://mongoid.org/
  57. giordano.scalzo@cleancode.it @giordanoscalzo www.slideshare.net/giordano github.com/gscalzo

×