Skip to content

scorix/enum_attr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

enum_attr

This is a gem for adding enum attributes to classes.

Dependencies

Installation

Add this line to your application's Gemfile:

gem 'enum_attr', github: 'scorix/enum_attr'

And then execute:

$ bundle

Usage

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]

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages