Share
Embed
Building Extractable Libraries in Rail - Railsconf
Build better libraries in Rails! Talk given at Railsconf 2013
Patrick Robertson
April 30, 2013
Transcript
71 slides
-
In order to ship more code I believe it can
benefit from some conventions -
Namespace All The Things No Rails Application has a User
class right?? -
Avoid the Autoload Trap Maintain explicit entry points into your
library code using initializers -
Use the Configuration Pattern Keep credentials as far away from
your library code as possible -
Isolate Interaction with your Domain Use DCI at the glue
points of your application code -
# lib/twitter_user.rb class TwitterUser def initialize(user) @user = user end
end # lib/twitter_profile.rb class TwitterProfile def initialize(twitter_user, image_url) @user = twitter_user @image_url = image_url end end -
# lib/papa_smurf.rb class PapaSmurf end # lib/sleepy_smurf.rb class SleepySmurf end
# lib/ninja_smurf.rb class NinjaSmurf end -
# lib/smurf.rb module Smurf class Papa end class Sleepy end
class Ninja end end -
Rails 3 brought a wonderful convention for removing /lib from
automatically being loaded on application boot... -
... however our first instinct is to do this: #
config/application.rb # Custom directories with classes and modules you want to be autoloadable. config.autoload_paths += %W(#{config.root}/lib) Let’s try something different! -
We have created a namespace and setup a proper layout
when we need to extract our code outside the application into a gem. ... in ten seconds or less :) -
It’s so simple to do this: # lib/twitter_wrangler.rb module TwitterWrangler
OAUTH_KEY = ‘ABCDEFG1234ZXYZB2324’ end Or even this: # lib/twitter_wrangler.rb module TwitterWrangler OAUTH_KEY = ENV[‘TWITTER_KEY’] end -
But we already have an entry point for our library
code. So let’s exploit the heck out of it! -
# lib/twitter_wrangler/configuration.rb module TwitterWrangler class Configuration < Struct.new(:oauth_key) end class
<< self attr_accessor :configuration end def self.configure self.configuration ||= Configuration.new yield(configuration) if block_given? end end -
# lib/twitter_wrangler/configuration.rb module TwitterWrangler class Configuration < Struct.new(:oauth_key) end class
<< self attr_accessor :configuration end def self.configure self.configuration ||= Configuration.new yield(configuration) if block_given? end end -
# lib/twitter_wrangler/configuration.rb module TwitterWrangler class Configuration < Struct.new(:oauth_key) end class
<< self attr_accessor :configuration end def self.configure self.configuration ||= Configuration.new yield(configuration) if block_given? end end -
# lib/twitter_wrangler/configuration.rb module TwitterWrangler class Configuration < Struct.new(:oauth_key) end class
<< self attr_accessor :configuration end def self.configure self.configuration ||= Configuration.new yield(configuration) if block_given? end end -
# lib/twitter_wrangler/configuration.rb module TwitterWrangler class Configuration < Struct.new(:oauth_key) end class
<< self attr_accessor :configuration end def self.configure self.configuration ||= Configuration.new yield(configuration) if block_given? end end -
With one additional, boilerplate class we’ve established a separation of
credential concerns -
Web App body mass index tweet called Library Authorization Twitter
message Authentication Queueing -
# lib/twitter_wrangler/tweeting_patient.rb module TwitterWrangler::TweetingPatient def bmi_twitter_message “Today my BMI is
#{body_mass_index} and I’m #{percent_of_body_mass_index_goal} from my goal of #{body_mass_index_goal}!” end end -
Option #3 - Mock patient = mock(body_mass_index: 27, percent_of_body_mass_index_goal: ’95%’,
body_mass_index_goal: 25) -
# spec/twitter_wrangler/tweeting_patient_spec.rb describe TweetingPatient do let(:patient) { FakePatient.new } before
{ patient.extend TweetingPatient } describe ‘#bmi_twitter_message’ do let(:tweet) { patient.bmi_twitter_message } it ‘crafts a lovely tweet’ do expect(tweet).to include ‘Today my BMI is...’ end end end -
# spec/twitter_wrangler/tweeting_patient_spec.rb describe TweetingPatient do let(:patient) { FakePatient.new } before
{ patient.extend TweetingPatient } describe ‘#bmi_twitter_message’ do let(:tweet) { patient.bmi_twitter_message } it ‘crafts a lovely tweet’ do expect(tweet).to include ‘Today my BMI is...’ end end end -
# spec/twitter_wrangler/tweeting_patient_spec.rb describe TweetingPatient do let(:patient) { FakePatient.new } before
{ patient.extend TweetingPatient } describe ‘#bmi_twitter_message’ do let(:tweet) { patient.bmi_twitter_message } it ‘crafts a lovely tweet’ do expect(tweet).to include ‘Today my BMI is...’ end end end -
# spec/twitter_wrangler/tweeting_patient_spec.rb describe TweetingPatient do let(:patient) { FakePatient.new } before
{ patient.extend TweetingPatient } describe ‘#bmi_twitter_message’ do let(:tweet) { patient.bmi_twitter_message } it ‘crafts a lovely tweet’ do expect(tweet).to include ‘Today my BMI is...’ end end end -
# lib/twitter_wrangler/bmi_twitter_update.rb module TwitterWrangler class BMITwitterUpdate < Struct.new(:patient) def call
Queue.push tweeting_patient.bmi_twitter_message end private def tweeting_patient @tweeting_patient ||= patient.extend TweetingPatient end end end -
# lib/twitter_wrangler/bmi_twitter_update.rb module TwitterWrangler class BMITwitterUpdate < Struct.new(:patient) def call
Queue.push tweeting_patient.bmi_twitter_message end private def tweeting_patient @tweeting_patient ||= patient.extend TweetingPatient end end end -
# lib/twitter_wrangler/bmi_twitter_update.rb module TwitterWrangler class BMITwitterUpdate < Struct.new(:patient) def call
Queue.push tweeting_patient.bmi_twitter_message end private def tweeting_patient @tweeting_patient ||= patient.extend TweetingPatient end end end -
# lib/twitter_wrangler/bmi_twitter_update.rb module TwitterWrangler class BMITwitterUpdate < Struct.new(:patient) def call
Queue.push tweeting_patient.bmi_twitter_message end private def tweeting_patient @tweeting_patient ||= patient.extend TweetingPatient end end end -
# lib/twitter_wrangler/bmi_twitter_update.rb module TwitterWrangler class BMITwitterUpdate < Struct.new(:patient) def call
Queue.push tweeting_patient.bmi_twitter_message end private def tweeting_patient @tweeting_patient ||= patient.extend TweetingPatient end end end -
# spec/twitter_wrangler/bmi_twitter_update_spec.rb describe BMITwitterUpdate do describe ‘#call’ do let(:patient) {
mock() } let(:update) { BMITwitterUpdate.new(patient) } before { Queue.stubs(:push) } it ‘extends TweetingPatient’ do update.call expect(patient).to have_received(:extend).with TweetingPatient end it ‘queues the twitter message’ do update.call expect(Queue).to have_received(:push).with patient.bmi_twitter_message end end end -
# spec/twitter_wrangler/bmi_twitter_update_spec.rb describe BMITwitterUpdate do describe ‘#call’ do let(:patient) {
mock() } let(:update) { BMITwitterUpdate.new(patient) } before { Queue.stubs(:push) } it ‘extends TweetingPatient’ do update.call expect(patient).to have_received(:extend).with TweetingPatient end it ‘queues the twitter message’ do update.call expect(Queue).to have_received(:push).with patient.bmi_twitter_message end end end -
# spec/twitter_wrangler/bmi_twitter_update_spec.rb describe BMITwitterUpdate do describe ‘#call’ do let(:patient) {
mock() } let(:update) { BMITwitterUpdate.new(patient) } before { Queue.stubs(:push) } it ‘extends TweetingPatient’ do update.call expect(patient).to have_received(:extend).with TweetingPatient end it ‘queues the twitter message’ do update.call expect(Queue).to have_received(:push).with patient.bmi_twitter_message end end end -
# spec/twitter_wrangler/bmi_twitter_update_spec.rb describe BMITwitterUpdate do describe ‘#call’ do let(:patient) {
mock() } let(:update) { BMITwitterUpdate.new(patient) } before { Queue.stubs(:push) } it ‘extends TweetingPatient’ do update.call expect(patient).to have_received(:extend).with TweetingPatient end it ‘queues the twitter message’ do update.call expect(Queue).to have_received(:push).with patient.bmi_twitter_message end end end -
# spec/twitter_wrangler/bmi_twitter_update_spec.rb describe BMITwitterUpdate do describe ‘#call’ do let(:patient) {
mock() } let(:update) { BMITwitterUpdate.new(patient) } before { Queue.stubs(:push) } it ‘extends TweetingPatient’ do update.call expect(patient).to have_received(:extend).with TweetingPatient end it ‘queues the twitter message’ do update.call expect(Queue).to have_received(:push).with patient.bmi_twitter_message end end end -
# spec/twitter_wrangler/bmi_twitter_update_spec.rb describe BMITwitterUpdate do describe ‘#call’ do let(:patient) {
mock() } let(:update) { BMITwitterUpdate.new(patient) } before { Queue.stubs(:push) } it ‘extends TweetingPatient’ do update.call expect(patient).to have_received(:extend).with TweetingPatient end it ‘queues the twitter message’ do update.call expect(Queue).to have_received(:push).with patient.bmi_twitter_message end end end -
# app/controllers/bmi_controller.rb class BmiController < ApplicationController def create bmi =
patient.bmis.create(:bmi) Resque.enqueue :bmi_twitter_update_job, patient.id respond_with bmi end private def patient @patient ||= current_user.patients.find(:patient_id) end end -
# app/jobs/bmi_twitter_update_job.rb class BmiTwitterUpdateJob @queue = :twitter def self.perform_job(patient_id) patient
= Patient.find(patient_id) BMITwitterUpdate.new(patient).call end end