Project

enm

0.0
No release in over 3 years
Parse and serialize PostgreSQL enum sets in Ruby, powered by Rust.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

ENM

ENM is a Ruby gem that allows for static analysis of PostgreSQL enum types defined a database. Presently, it only supports reading from something along the lines of Rails' structure.sql, but future versions may support connecting to a live database or schema.rb.

It relies upon the pgenum_parser crate, which it compiles against via magnus.

Installation

This gem requires ruby 3.4 or later.

Install the gem and add to the application's Gemfile by executing:

bundle add enm

If bundler is not being used to manage dependencies, install the gem by executing:

gem install enm

Usage

This is intended to be used as a global singleton, so it requires a minor bit of setup in an initializer or similar:

require "enm"

ENM.configure_current do |builder|
  builder.from_sql_file!("primary", "db/structure.sql")
end

If only one database is provided, it will be used as the default for lookups.

If you have multiple databases, you can call builder.from_sql_file! multiple times and optionally specify which one is your default:

require "enm"

ENM.configure_current do |builder|
  builder.from_sql_file!("primary", "db/structure.sql", default: true)
  builder.from_sql_file!("analytics", "db/analytics_structure.sql")
end

Once ENM is configured, you can look up enums in your code like so:

ENM.get("access_management") #=> ENM::Enum

ENM.get("some_analytics_enum", on: :analytics) #=> ENM::Enum

# To build a Dry::Types::Type for an enum:
ENM.dry("access_management") #=> Dry::Types["string"].enum("foo", "bar", "baz")

# To build a symbolized version of an enum, with a default
ENM.dry("access_management", mode: :symbol, default: :bar) #=> Dry::Types["symbol"].default(:bar).enum(:foo, :bar, :baz)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/scryptmouse/enm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ENM project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.