Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Building Extractable Libraries in Rails via Patrick Robertson #railsConf

Building Extractable Libraries in Rails

Avoiding the autoload trap

  • rails 3: removing /lib from automatically being loaded on application boot

create a namespace and setup a proper layout when we need to extract our code outside the application into a gem

Hide your credentials from your Library

#config/initializers/twitter_wrangler.rb
require_relative '../lib/twitter_wrangler'

TwitterWrangler.configure do |config|
  config.oauth_key - ENV[:twitter_ouath_key]
end

#lib/twitter_wrangler/configuration.rb
module TwitterWrangler
  class Configuration
    attr_accessor :oauth_key
  
    def initializers
      oauth_key = nil
    end
  end

  class << self
    attr_accessor :configuration
  end

  def self.configure
    self.configuration ||= Configuration.new
    yeild(configuration) if block_given?
  end
end

separation of credential concerns

Keeping your domain models focused

provides clear separation of domain models and outside code creates isolatable test easy path forward for gem extraction

https://speakerdeck.com/patricksroberts/building-extractable-libraries-in-rails-bostonrb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment