Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[NOTES] Pluck It: Extracting Micro-libraries into RubyGems

[NOTES] Pluck It: Extracting Micro-libraries into RubyGems

## SPEAKER NOTES INCLUDED ##
How many times have you written the same bits of code, over and over, and thought, “You know, if only this was big enough to be a gem, I would pluck it out." Often, we think of a RubyGem as a larger library of code that we “bolt on” to an app. And, these smaller code blobs become a hassle to distribute to the multiple apps that use them.

A small micro-library, done the right way, at the right time, can greatly improve an app.

But, when can you benefit from extracting a micro-library? And, how do you build and publish that code into a RubyGem? I'll go through the process, from A to Z.

Adam Cuppy (he/him)

November 15, 2015
Tweet

More Decks by Adam Cuppy (he/him)

Other Decks in Programming

Transcript

  1. Pluck It Extracting micro-libraries and building rubygems @AdamCuppy Covering: 1.

    Identifying opportunities for extraction (using Rails, but don’t need to) 2. Building a gem and publishing it to RubyGems. 3. Pros/Cons
  2. Question: * How many times have you written the same

    bits of code, over and over, and thought, “You know, if only this was big enough to be a gem, I would pluck it out.” * Most engineers don’t know how to craft a gem… Who does?
  3. Problem * Copy lib files around? * What about updates?

    * What if you lose direct access to the application?
  4. It’s a simple package: a self-contained collection of one or

    more files. rubygem: * RubyGems then modifies the $LOAD_PATH to include the `lib` directory (and files) of the package. * When you require a file from the package, your app knows where to look.
  5. $ bundle install RubyGems Who knows what RubyGem is? *

    No: Packaged code libraries * RubyGems.org is a database of RubyGems
  6. App Bundle Bundler + RubyGems + MyApp RubyGems.org rails-4.2 rspec-core-3.2

    Private Gem Server priv_gem-1.0 priv_gem-1.0 RubyGems have version dependencies, as well. So, bundler makes sure the proper gem versions are included. NEXT: Let’s extract a simple gem.
  7. App Bundle $ bundle install Bundler + RubyGems + MyApp

    RubyGems have version dependencies, as well. So, bundler makes sure the proper gem versions are included. NEXT: Let’s extract a simple gem.
  8. The function of this code is to find differences between

    two hashes. Looking the model there is a lot of extra code - let’s fix that. Well this is useful!
  9. $ bundle gem hash_diff ——test Created a lib directory with

    the first files Remember: it’s a package of one or more files
  10. $ bundle gem hash_diff ——test Templates files (options flags on

    build can change this behavior) * Readme, Code of Conduct, License
  11. $ bundle gem hash_diff ——test “— test” flag added resources

    to support automated testing (default RSpec, but can use MiniTest) Included .travis.yml defaults
  12. Open ‘hash_diff/version’ => Sets a default version This is used

    to track releases of the gem. Default is semantic versioning
  13. 1. Modify code 2. Increment VERSION constant 3. Rebuild gem

    4. Publish new version 5. Update apps: $ cd /path/to/app; bundle update my_gem New Release
  14. Extraction encourages isolation; isolation encourages a simple interface; and, a

    simple interface is more maintainable. * “Encourages” is the operative word
  15. It’s when the cost of maintaining an internal library, exceeds

    the cost of maintaining an external dependency.