Skip to content

Instantly share code, notes, and snippets.

@naoyamakino
Last active December 16, 2015 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoyamakino/5499134 to your computer and use it in GitHub Desktop.
Save naoyamakino/5499134 to your computer and use it in GitHub Desktop.
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