Share
Embed
Building Extractable Libraries in Rails - DCRUG
version 2.0 of the slidedeck presented for DCRUG on 7/12/2012
Patrick Robertson
July 12, 2012
Transcript
49 slides
-
Hello There! I’m @patricksroberts. I work at @iorahealth. I co-organize
@bostonrb. I maintain @copycopter. -
I think it can benefit from some conventions So we
can ship some more code -
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 Models Use the DCI pattern
at the glue points of your application code -
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 attr_accessor :oauth_key def initialize
self.oauth_key = nil end 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 attr_accessor :oauth_key def initialize
self.oauth_key = nil end 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 attr_accessor :oauth_key def initialize
self.oauth_key = nil end 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 attr_accessor :oauth_key def initialize
self.oauth_key = nil end 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 attr_accessor :oauth_key def initialize
self.oauth_key = nil end 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 attr_accessor :oauth_key def initialize
self.oauth_key = nil end 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/roles/tweeting_patient.rb module TwitterWrangler::Roles::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 -
# lib/twitter_wrangler/support/fake_patient.rb class FakePatient def body_mass_index;27;end def percent_of_body_mass_index_goal;”95%”;end def body_mass_index_goal;25;end
end # spec/twitter_wrangler/roles/tweeting_patient_spec.rb describe TweetingPatient do let(:patient) { FakePatient.new } before { FakePatient.extend TweetingPatient } it ‘asserts some fantastic things’ do #GLORIOUS SPECS end end -
# spec/twitter_wrangler/roles/tweeting_patient_spec.rb describe TweetingPatient do let(:patient) { mock() } before
do patient.stubs({body_mass_index: 27, percent_of_body_mass_index_goal: “95%” body_mass_index_goal: 25 }) patient.extend TweetingPatient end it ‘asserts some fantastic things’ do #GLORIOUS SPECS end end -
# lib/twitter_wrangler/Contexts/BMITwitterUpdate.rb module TwitterWrangler::Contexts class BMITwitterUpdate attr_reader: patient def initialize(patient)
@patient = patient @patient.extend TweetingPatient end def call TwitterWranglerQueue.add @patient.bmi_twitter_message end end end -
# lib/twitter_wrangler/Contexts/BMITwitterUpdate.rb module TwitterWrangler::Contexts class BMITwitterUpdate attr_reader: patient def initialize(patient)
@patient = patient @patient.extend TweetingPatient end def call TwitterWranglerQueue.add @patient.bmi_twitter_message end end end -
# lib/twitter_wrangler/Contexts/BMITwitterUpdate.rb module TwitterWrangler::Contexts class BMITwitterUpdate attr_reader: patient def initialize(patient)
@patient = patient @patient.extend TweetingPatient end def call TwitterWranglerQueue.add @patient.bmi_twitter_message end end end -
# lib/twitter_wrangler/Contexts/BMITwitterUpdate.rb module TwitterWrangler::Contexts class BMITwitterUpdate attr_reader: patient def initialize(patient)
@patient = patient @patient.extend TweetingPatient end def call TwitterWranglerQueue.add @patient.bmi_twitter_message end end end -
# spec/models/patient_bmi_observer_spec.rb describe PatientBMIObserver do let(:bmi_update) { mock() } before
do BMITwitterUpdate.stubs(:new).returns bmi_update bmi_update.stubs :call end it ‘sends a twitter update on mah BMI’ do # expect #call to have been called once end end -
# spec/models/patient_bmi_observer_spec.rb describe PatientBMIObserver do let(:bmi_update) { mock() } before
do BMITwitterUpdate.stubs(:new).returns bmi_update bmi_update.stubs :call end it ‘sends a twitter update on mah BMI’ do # expect #call to have been called once end end -
Provides clear separation of Domain Model and outside code Creates
isolatable tests Easy path forward for gem extraction Boilerplate :( YAGNI :(