nifty "microgem" pattern

published
permalink
https://accidental.cc/notes/2022/nifty-microgem-pattern/

By having a gemspec which parses the yaml frontmatter of it’s own readme, you can drive a gemspec from a really simple template, and get a nice small footprint gem, hostable as a gist.

Basically, I found out that since YAML frontmatter in a markdown file uses the same separators as YAML itself for document separation (which I’m certain was on purpose), using YAML.load_file on a markdown file will return the frontmatter!

SO, define important parts of the gem in front matter, and take README driven development to the next level!

require "yaml"

header = YAML.safe_load_file("readme.md")
Gem::Specification.new do |spec|
  spec.name          = header["name"]
  spec.version       = header["version"]
  spec.authors       = ["Jon Raphaelson"]
  spec.email         = ["jon@accidental.cc"]

  spec.summary       = header["summary"]
  spec.license       = "MIT"

  spec.files         = Dir.glob("**/*.rb", base: __dir__)
  spec.require_paths = ["."]
end