No commit activity in last 3 years
No release in over 3 years
SimpleEnum::Persistence is extension of SimpleEnum, which brings data persistence support to SimpleEnum.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 4.0.0
>= 4.0.0
~> 10.0
~> 2.14

Runtime

 Project Readme

SimpleEnum::Persistence

Travis Gem

SimpleEnum::Persistence is extension of SimpleEnum, which brings data persistence support to SimpleEnum. Sometimes, we need to use one of our Model as enum data source, in order to add/remove enum type dynamically, SimpleEnum::Persistence is built for this!

Why not ActiveRecord association?

  1. SimpleEnum is a great library, which brings a lot of benefits to create enum-like fields;

  2. SimpleEnum::Persistence cache all enums in memory for better performance, and it will reload when database changes (with after_save callback).

ActiveRecord Quick start

Let's say we have a model Tag, we want to use it as enum data, just add this to a model:

class Post
  as_enum :tag, Tag, persistence: true
end

Then SimpleEnum will use Tag.all as enum data, tag.name as enum key and tag.id as enum value, if we want to use a field rather than :name as enum key:

class Post
  as_enum :tag, Tag, persistence: { key: :name, value: :id }
end

License

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