Pluck It: Extracting Micro-libraries into RubyGems

Fff206665889a0f5baeedf5b75483edd?s=47 Adam Cuppy
November 15, 2015

Pluck It: Extracting Micro-libraries into RubyGems

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.

Fff206665889a0f5baeedf5b75483edd?s=128

Adam Cuppy

November 15, 2015
Tweet

Transcript

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

  2. None
  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. Add /lib/gravatar.rb

  10. Moved from app/models/user.rb

  11. None
  12. None
  13. None
  14. None
  15. Question:

  16. Problem

  17. https://rubygems.org/

  18. “Pay no attention to the man behind the curtain!”

  19. It’s a simple package: a self-contained collection of one or

    more files. rubygem:
  20. http://bundler.io/

  21. /path/to/myapp/Gemfile

  22. $ bundle install

  23. $ bundle install RubyGems

  24. Bundler + RubyGems + MyApp

  25. Bundler + RubyGems + MyApp RubyGems.org rails-4.2 rspec-core-3.2

  26. Bundler + RubyGems + MyApp RubyGems.org rails-4.2 rspec-core-3.2 Private Gem

    Server priv_gem-1.0 priv_gem-1.0
  27. 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
  28. App Bundle $ bundle install Bundler + RubyGems + MyApp

  29. None
  30. None
  31. $ bundle gem hash_diff ——test

  32. $ bundle gem hash_diff ——test

  33. $ bundle gem hash_diff ——test

  34. $ bundle gem hash_diff ——test

  35. $ bundle gem hash_diff ——test

  36. $ bundle help gem

  37. None
  38. None
  39. None
  40. $ cd /path/to/my_gem $ bundle install $ rake

  41. None
  42. None
  43. None
  44. None
  45. Build: $ cd /path/to/my_gem $ gem build hash_diff.gemspec

  46. Publish: $ gem push hash_diff-0.1.0.gem

  47. https://rubygems.org/gems/hash_diff

  48. /path/to/myapp/Gemfile

  49. $ cd /path/to/myapp $ bundle install

  50. I’m Famous like @the_zenspider!

  51. None
  52. None
  53. 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
  54. 1. Isolation from app code 2. Manage upgrades 3. Community

    support Pros…
  55. Extraction encourages isolation; isolation encourages a simple interface; and, a

    simple interface is more maintainable.
  56. 1. External dependencies to manage 2. Add’l development overhead 3.

    Community support Cons…
  57. When is the “right time?”

  58. It’s when the cost of maintaining an internal library, exceeds

    the cost of maintaining an external dependency.
  59. https://rubygems.org/gems/trueman

  60. https://rubygems.org/gems/gravasaurus_hex

  61. https://rubygems.org/gems/hash_diff

  62. http://guides.rubygems.org/

  63. github.com/acuppy @adamcuppy

  64. codingzeal.com @adamcuppy Adam Cuppy