• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Tenacity
 

Tenacity

on

  • 1,884 views

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

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

Statistics

Views

Total Views
1,884
Views on SlideShare
1,522
Embed Views
362

Actions

Likes
0
Downloads
2
Comments
0

4 Embeds 362

http://johnpwood.net 358
url_unknown 2
http://www.linkedin.com 1
https://www.linkedin.com 1

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Tenacity Tenacity Presentation Transcript

  • 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::Base # car_id defined on the table def car Car.find(self.car_id) endend
  • 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
  • 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
  • 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
  • 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
  • class Car include MongoMapper::Document include Tenacity t_has_many :wheelsendclass Wheel < ActiveRecord::Base include Tenacity t_belongs_to :carend
  • 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
  • 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
  • # 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]
  • 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
  • Supported Libraries• ActiveRecord • MongoMapper (2.x and 3.x) • Mongoid• CouchRest • Ripple (CouchModel and ExtendedDocument) • Sequel• DataMapper • Toystore
  • 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
  • Thanks!john@johnpwood.net @johnpwood