Sha256: 9933c7f8cfbfac54e13a5df6bc44fb1c9f7c018abb2e8dd8c64428ea263231a2

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

EnumeratedField is a library that provides some nice methods when a string column is used like an enumeration, meaning there is a list of allowable values for the string column. Typically you want the display value as seen by the end user to differ from the stored value, allowing you to easily change the display value at anytime without migrating data, and this little gem helps you with that.

Example Usage:
This example demonstrates usage within an ActiveRecord::Base object, however this gem does not depend on ActiveRecord.

class Hike < ActiveRecord::Base
   
  include EnumeratedField
  
  enum_field :trail, [['Pacific Crest Trail', 'pct'],
                      ['Continental Divide Trail', 'cdt'],
                      ['Superior Hiking Trail', 'sht']]

end

> hike = Hike.create(:trail => 'pct')

> hike.trail_sht?
=> false

> hike.trail_pct?
=> true

> hike.trail_display
=> "Pacific Crest Trail"

> hike.trail_values   # useful to provide to options_for_select when constructing forms
=> [['Pacific Crest Trail', 'pct'], ['Continental Divide Trail', 'cdt'], ['Superior Hiking Trail', 'sht']]


These methods are all prefixed with the field name by design, which allows multiple fields on a model to exist which potentially have the same values.

Run tests by:  ruby test/enumerated_field_test.rb

TODO:
* Provide option to enum_field to setup validation methods that validate the value of the field against the allowed values.
* Provide any support needed for defining columns on MySQL databases as enum columns instead of string columns.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enumerated_field-0.0.1 README