EnumBehavior
EnumBehavior is simple polymorphic behavior with Enum related object and delegation.
Installation
Add this line to your application's Gemfile:
gem 'enum_behavior'
And then execute:
$ bundle
Or install it yourself as:
$ gem install enum_behavior
Usage
- Add
include EnumBehavior
to your ActiveRecord model class.
class Music < ActiveRecord::Base
include EnumBehavior
end
# You can do this at super class such as ApplicationRecord.
- Add
enum_behavior
after enum declaration.
class Music < ApplicationRecord
enum genre: { pop: 0, rock: 1, metal: 2 }
enum_behavior :genre
end
- Define class related enum values
module Musics # Pluralized behavior defined class name
class Pop < EnumBehavior::Base
def
'Love Musics'
end
end
class Metal
# you can define plane class instead of using EnumBehavior::Base.
# Require constructor with 1 argument. (passes related ActiveRecord instance)
def initialize(model)
@model = model
end
attr_reader :model
def
'Love HeavyMetal!'
end
end
end
- Simple polymorphic behavior!
m = Music.new(genre: :pop)
m.genre_behavior. # => Love Musics
m.genre = :metal
m.genre_behavior. # => Love HeavyMetal!
m.genre = :rock
m.genre_behavior. # => NameError uninitialized constant Musics::Rock
# You can use with delegate
# delegate :message, to: :genre_behavior, prefix: :genre
# m.genre_message
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 tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/yiyenene/enum_behavior. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant 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 EnumBehavior project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.