Skip to content
Sequel plugin that provides method for getting `pg_enum` values
Ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/sequel/plugins
spec
.cirrus.yml
.editorconfig
.gitignore
.rspec
.rubocop.yml
CODE_OF_CONDUCT.md
Gemfile
LICENSE.txt
README.md
Rakefile
sequel-enum_values.gemspec

README.md

Sequel enum_values plugin

Cirrus CI - Base Branch Build Status Codecov branch Code Climate Depfu Inline docs Gem license

A Sequel plugin that provides enum_values(field) method to your models.

Installation

Bundler

Add this line to your project's Gemfile:

gem 'sequel-enum_values', require: 'sequel/plugins/enum_values'

And then execute:

$ bundle

Manually

Install this gem as:

$ gem install sequel-enum_values

And then require it in your project:

require 'sequel/plugins/enum_values'

Usage

If you have database schema like this:

create_enum :item_type, %w[first second third]
create_enum :item_status, %w[created selected canceled]

create_table :items do
  primary_key :id
  column :type, :item_type
  column :status, :item_status
end

Then you can use it like this:

class Item < Sequel::Model
  plugin :enum_values
end

Item.enum_values(:type) # => ["first", "second", "third"]
Item.enum_values(:status) # => ["created", "selected", "canceled"]

Caching

Plugin caches enum values for each field by default.

But you can disable it:

Item.plugin :enum_values, caching: false

Predicate methods

Plugin can define instance methods for all enum values:

Item.plugin :enum_values, predicate_methods: true # default is `false`

item = Item.new(type: 'first', status: 'created')

item.first? # => true
item.second? # => false

item.created? # => true
item.selected? # => false

Or just for specific fields:

Item.plugin :enum_values, predicate_methods: %i[status]
# or just `:status` for single value

item = Item.new(type: 'first', status: 'created')

item.first? # => NoMethodError

item.created? # => true
item.selected? # => false

Contributing

  1. Fork it
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create new Pull Request
You can’t perform that action at this time.