This is a gem for adding enum attributes to classes.
Add this line to your application's Gemfile:
gem 'enum_attr', github: 'scorix/enum_attr'
And then execute:
$ bundle
class Package
# enum_attr :status, {out_of_stock: -1, ready: 0, selling: 1}, default: 0
# or you can use symbol as default value
enum_attr :status, {out_of_stock: -1, ready: 0, selling: 1}, default: :ready
end
package = Package.new
package.status # => 0
package.ready? # => true
package.out_of_stock! # => true
package.ready? # => false
package.status # => -1
Package.available_statuses # => {out_of_stock: -1, ready: 0, selling: 1}
package.available_statuses # => {out_of_stock: -1, ready: 0, selling: 1}
or you can use array, too
class Package
# enum_attr :status, [-1, 0, 1]
# you can also specify a default value
# NOTIFICATION: default value a real value, not index of array
enum_attr :status, [-1, 0, 1], default: 0
end
package = Package.new
package.status # => 0
Package.available_statuses # => [-1, 0, 1]
package.available_statuses # => [-1, 0, 1]
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request