SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
Tenacity
https://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)
 end
end

class Wheel < ActiveRecord::Base
 # car_id defined on the table
 def car
  Car.find(self.car_id)
 end
end
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)
 end
end

class Wheel < ActiveRecord::Base
 # car_id defined on the table
 def car
  Car.find(self.car_id)
 end
end
class Car
  include MongoMapper::Document
  include Tenacity
 t_has_many :wheels
end

class Wheel < ActiveRecord::Base
  include Tenacity
 t_belongs_to :car
end
class Car
  include MongoMapper::Document
  include Tenacity
 t_has_many :wheels
 t_has_one :dashboard
end

class Wheel < ActiveRecord::Base
  include Tenacity
 t_belongs_to :car
end

class Dashboard < CouchRest::Model::Base
  include Tenacity
  use_database MY_DATABASE
 t_belongs_to :car
end
car = Car.create

# Set the related object
dashboard = Dashboard.create
car.dashboard = dashboard
car.save

# Fetch related object from the respective database
car.dashboard

# Fetch related object id
car.dashboard.car_id
# Set related objects
wheel_1 = Wheel.create
wheel_2 = Wheel.create
wheel_3 = Wheel.create
wheels = [wheel_1, wheel_2, wheel_3]
car.wheels = wheels
car.save

wheel_1.car_id    # car.id

# Fetch array of related objects from the respective database
car.wheels        # [wheel_1, wheel_2, wheel_3]

# Fetch ids of related objects from the database
car.wheel_ids      # [wheel_1.id, wheel_2.id, wheel_3.id]

# Add a related object to the collection
new_wheel = Wheel.create
car.wheels << new_wheel
car.save
car.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

More Related Content

PDF
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
PPTX
List and images in html
PPTX
Css intro
PPTX
WEBD 162: display property, Float and Clear
PDF
Php Online Training
PDF
Basics of Web Designing Languages
PPTX
Binary Studio Academy PRO. JS course. Lecture 2. backbone
PPTX
Javascript
Entity API in Drupal 8 (Drupal Tech Talk October 2014)
List and images in html
Css intro
WEBD 162: display property, Float and Clear
Php Online Training
Basics of Web Designing Languages
Binary Studio Academy PRO. JS course. Lecture 2. backbone
Javascript

Similar to Tenacity (20)

PDF
walkmod - JUG talk
PPTX
SPSNH 2014 - The SharePoint & jQueryGuide
PDF
MongoDB Basics
PDF
MongoDB and Ruby on Rails
PPTX
Building Your First Store App with XAML and C#
PDF
Mongodb at-gilt-groupe-seattle-2012-09-14-final
PDF
Web Development with Delphi and React - ITDevCon 2016
PPTX
SPTechCon DevDays - SharePoint & jQuery
PDF
Drupal8 for Symfony Developers (PHP Day Verona 2017)
PDF
33rd degree talk: open and automatic coding conventions with walkmod
PDF
Stencil the time for vanilla web components has arrived
PPTX
SharePoint Cincy 2012 - jQuery essentials
PPTX
Atlanta Drupal User Group (ADUG)
PDF
Angular mobile angular_u
PDF
Advanced guide to develop ajax applications using dojo
PDF
GDG Addis - An Introduction to Django and App Engine
PPTX
DSpace 4.2 Transmission: Import/Export
PDF
Apache Cayenne for WO Devs
PPTX
SPTechCon Boston 2015 - Utilizing jQuery in SharePoint
PPTX
Wheel.js
walkmod - JUG talk
SPSNH 2014 - The SharePoint & jQueryGuide
MongoDB Basics
MongoDB and Ruby on Rails
Building Your First Store App with XAML and C#
Mongodb at-gilt-groupe-seattle-2012-09-14-final
Web Development with Delphi and React - ITDevCon 2016
SPTechCon DevDays - SharePoint & jQuery
Drupal8 for Symfony Developers (PHP Day Verona 2017)
33rd degree talk: open and automatic coding conventions with walkmod
Stencil the time for vanilla web components has arrived
SharePoint Cincy 2012 - jQuery essentials
Atlanta Drupal User Group (ADUG)
Angular mobile angular_u
Advanced guide to develop ajax applications using dojo
GDG Addis - An Introduction to Django and App Engine
DSpace 4.2 Transmission: Import/Export
Apache Cayenne for WO Devs
SPTechCon Boston 2015 - Utilizing jQuery in SharePoint
Wheel.js
Ad

Recently uploaded (20)

PDF
Hybrid model detection and classification of lung cancer
PDF
Mushroom cultivation and it's methods.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Approach and Philosophy of On baking technology
PDF
project resource management chapter-09.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
A Presentation on Artificial Intelligence
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Enhancing emotion recognition model for a student engagement use case through...
Hybrid model detection and classification of lung cancer
Mushroom cultivation and it's methods.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Chapter 5: Probability Theory and Statistics
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Approach and Philosophy of On baking technology
project resource management chapter-09.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
OMC Textile Division Presentation 2021.pptx
1 - Historical Antecedents, Social Consideration.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
DP Operators-handbook-extract for the Mautical Institute
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Encapsulation_ Review paper, used for researhc scholars
A Presentation on Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
Enhancing emotion recognition model for a student engagement use case through...
Ad

Tenacity

  • 2. Polyglot Persistence is Great! Managing relationships...not so much
  • 3. class Car include MongoMapper::Document def wheels Wheel.where(:car_id => self.id) end end class Wheel < ActiveRecord::Base # car_id defined on the table def car Car.find(self.car_id) end end
  • 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. 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. 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. class Car include MongoMapper::Document def wheels Wheel.where(:car_id => self.id) end end class Wheel < ActiveRecord::Base # car_id defined on the table def car Car.find(self.car_id) end end
  • 8. class Car include MongoMapper::Document include Tenacity t_has_many :wheels end class Wheel < ActiveRecord::Base include Tenacity t_belongs_to :car end
  • 9. class Car include MongoMapper::Document include Tenacity t_has_many :wheels t_has_one :dashboard end class Wheel < ActiveRecord::Base include Tenacity t_belongs_to :car end class Dashboard < CouchRest::Model::Base include Tenacity use_database MY_DATABASE t_belongs_to :car end
  • 10. car = Car.create # Set the related object dashboard = Dashboard.create car.dashboard = dashboard car.save # Fetch related object from the respective database car.dashboard # Fetch related object id car.dashboard.car_id
  • 11. # Set related objects wheel_1 = Wheel.create wheel_2 = Wheel.create wheel_3 = Wheel.create wheels = [wheel_1, wheel_2, wheel_3] car.wheels = wheels car.save wheel_1.car_id # car.id # Fetch array of related objects from the respective database car.wheels # [wheel_1, wheel_2, wheel_3] # Fetch ids of related objects from the database car.wheel_ids # [wheel_1.id, wheel_2.id, wheel_3.id] # Add a related object to the collection new_wheel = Wheel.create car.wheels << new_wheel car.save car.wheels # [wheel_1, wheel_2, wheel_3, new_wheel]
  • 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. Supported Libraries • ActiveRecord • MongoMapper (2.x and 3.x) • Mongoid • CouchRest • Ripple (CouchModel and ExtendedDocument) • Sequel • DataMapper • Toystore
  • 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

Editor's Notes