Giter VIP home page Giter VIP logo

sequel-enum_values's Introduction

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

Development

After checking out the repo, run bundle install to install dependencies.

Then, run toys rspec to run the tests.

To install this gem onto your local machine, run toys gem install.

To release a new version, run toys gem release %version%. See how it works here.

Contributing

Bug reports and pull requests are welcome on GitHub.

License

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

sequel-enum_values's People

Contributors

alexwayfer avatar depfu[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.