Tenacityhttps://github.com/jwood/tenacity          ChicagoDB         June 20, 2011
Polyglot Persistence is Great!    Managing relationships...not so much
class Car  include MongoMapper::Document def wheels  Wheel.where(:car_id => self.id) endendclass Wheel < ActiveRecord::Bas...
Problems• Lots of boilerplate code• Bogus values can be stored in car_id• What happens if the Car is deleted?• Objects fet...
Introducing Tenacity• Heavily based on ActiveRecord::Associations• Support for has one, belongs to, and has many  relation...
Introducing Tenacity• Easily work with collections of associated  objects• Polymorphic associations• Read only association...
class Car  include MongoMapper::Document def wheels  Wheel.where(:car_id => self.id) endendclass Wheel < ActiveRecord::Bas...
class Car  include MongoMapper::Document  include Tenacity t_has_many :wheelsendclass Wheel < ActiveRecord::Base  include ...
class Car  include MongoMapper::Document  include Tenacity t_has_many :wheels t_has_one :dashboardendclass Wheel < ActiveR...
car = Car.create# Set the related objectdashboard = Dashboard.createcar.dashboard = dashboardcar.save# Fetch related objec...
# Set related objectswheel_1 = Wheel.createwheel_2 = Wheel.createwheel_3 = Wheel.createwheels = [wheel_1, wheel_2, wheel_3...
How Does It Work?•   Extends supported libraries with a common set of    methods•   Tenacity uses these methods to work wi...
Supported Libraries• ActiveRecord        • MongoMapper  (2.x and 3.x)                      • Mongoid• CouchRest           ...
What’s On Deck?• Add support for many to many associations• Add support for build and create methods • Author.posts.build•...
Thanks!john@johnpwood.net    @johnpwood
Upcoming SlideShare
Loading in...5
×

Tenacity

1,894

Published on

A brief presentation describing Tenacity, a ruby gem that provides a database client independent way of managing relationships between models backed by different databases.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,894
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Tenacity

    1. 1. Tenacityhttps://github.com/jwood/tenacity ChicagoDB June 20, 2011
    2. 2. Polyglot Persistence is Great! Managing relationships...not so much
    3. 3. class Car include MongoMapper::Document def wheels Wheel.where(:car_id => self.id) endendclass Wheel < ActiveRecord::Base # car_id defined on the table def car Car.find(self.car_id) endend
    4. 4. Problems• Lots of boilerplate code• Bogus values can be stored in car_id• What happens if the Car is deleted?• Objects fetched from the database each time• Adding new wheels is awkward
    5. 5. Introducing Tenacity• Heavily based on ActiveRecord::Associations• Support for has one, belongs to, and has many relationships• Adds foreign key like behavior• Can “clean up” when objects are deleted• Related objects are fetched from the database once and then cached
    6. 6. Introducing Tenacity• Easily work with collections of associated objects• Polymorphic associations• Read only associations• Support for a wide array of popular Ruby ORM/ODM libraries
    7. 7. class Car include MongoMapper::Document def wheels Wheel.where(:car_id => self.id) endendclass Wheel < ActiveRecord::Base # car_id defined on the table def car Car.find(self.car_id) endend
    8. 8. class Car include MongoMapper::Document include Tenacity t_has_many :wheelsendclass Wheel < ActiveRecord::Base include Tenacity t_belongs_to :carend
    9. 9. class Car include MongoMapper::Document include Tenacity t_has_many :wheels t_has_one :dashboardendclass Wheel < ActiveRecord::Base include Tenacity t_belongs_to :carendclass Dashboard < CouchRest::Model::Base include Tenacity use_database MY_DATABASE t_belongs_to :carend
    10. 10. car = Car.create# Set the related objectdashboard = Dashboard.createcar.dashboard = dashboardcar.save# Fetch related object from the respective databasecar.dashboard# Fetch related object idcar.dashboard.car_id
    11. 11. # Set related objectswheel_1 = Wheel.createwheel_2 = Wheel.createwheel_3 = Wheel.createwheels = [wheel_1, wheel_2, wheel_3]car.wheels = wheelscar.savewheel_1.car_id # car.id# Fetch array of related objects from the respective databasecar.wheels # [wheel_1, wheel_2, wheel_3]# Fetch ids of related objects from the databasecar.wheel_ids # [wheel_1.id, wheel_2.id, wheel_3.id]# Add a related object to the collectionnew_wheel = Wheel.createcar.wheels << new_wheelcar.savecar.wheels # [wheel_1, wheel_2, wheel_3, new_wheel]
    12. 12. How Does It Work?• Extends supported libraries with a common set of methods• Tenacity uses these methods to work with the objects in a generic fashion• Methods are added to the model class to interact with the relationship• ORM/ODM callback functionality is used to trigger Tenacity to save associated objects, cleanup associations, etc
    13. 13. Supported Libraries• ActiveRecord • MongoMapper (2.x and 3.x) • Mongoid• CouchRest • Ripple (CouchModel and ExtendedDocument) • Sequel• DataMapper • Toystore
    14. 14. What’s On Deck?• Add support for many to many associations• Add support for build and create methods • Author.posts.build• Improve performance• Continue adding support for additional ORM/ ODM libraries
    15. 15. Thanks!john@johnpwood.net @johnpwood
    1. Gostou de algum slide específico?

      Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

    ×