<commit_before>h1. classy_enum

ClassyEnum adds class-based enumerator functionality to your ActiveRecord model's attributes.

h2. Installation

ClassyEnum has only been testing with Rails 2.3.8, but should work with any version of Rails 2.3.x. The gem is hosted at "rubygems.org":https://rubygems.org/gems/classy_enum

It can be installed with:

<pre>
  gem install classy_enum
</pre>

h2. Example Usage

The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". In this example, I have an ActiveRecord model called @Alarm@ with an attribute called @priority@. Priority is stored as a string (VARCHAR) type in the database and is converted to an enum value when requested. Using the OPTIONS constant, I have defined three priority levels: low, medium, and high. Each priority level can have different properties and methods associated with it. 

*It is important that you include ClassyEnum AFTER declaring your OPTIONS and Default methods because they are used when creating the enum classes*

In my example, each enum value has a method called @email?@. By default this method returns false, but is overridden for high priority alarms and returns true.

Create a file in app/models called alarm_priority.rb 

<pre>
  module AlarmPriority
    OPTIONS = [:low, :medium, :high]

    module Defaults
      def email?
        false
      end
    end

    include ClassyEnum
  end

  class AlarmPriorityHigh
    def email?
      true
    end
  end
</pre>

Then in Alarm class, add a line that calls @classy_enum_attr@. The first argument is required, and is the name of the module defined above. The second argument is optional and specifies which attribute on Alarm will be used as an enumerable. In this case, I am using the module AlarmPriority, but the name of my attribute is priority. By default, it will use the name of module as the attribute name. If I wanted to do @alarm.alarm_priority@, I would not have included the second argument.

<pre>
  class Alarm < ActiveRecord::Base
    classy_enum_attr :alarm_priority, :priority
    
    delegate :email?, :to => :priority
  end
</pre>

With this setup, I can now do the following:

<pre>
  @alarm = Alarm.create(:priority => :medium)
  
  @alarm.priority _returns AlarmPriorityMedium_
  
  @alarm.email? _returns false_
  
  @alarm.update_attribute(:priority, :high)
  
  @alarm.email? _return true_
</pre>

h2. Notes

An ActiveRecord validator @validates_inclusion_of :field, :in => ENUM.all@ is automatically added to your model when you use @classy_enum_attr@. 

h2. Copyright

Copyright (c) 2010 <NAME>. See LICENSE for details.<commit_msg>README updates<commit_after>h1. classy_enum

ClassyEnum adds class-based enumerator functionality to your ActiveRecord model's attributes.

h2. Installation

ClassyEnum has only been tested with Rails 2.3.8, but should work with any version of Rails 2.3.x. The gem is hosted at "rubygems.org":https://rubygems.org/gems/classy_enum

It can be installed with:

<pre>
  gem install classy_enum
</pre>

h2. Example Usage

The most common use for ClassyEnum is to replace database lookup tables where the content and behavior is mostly static and has multiple "types". In this example, I have an ActiveRecord model called @Alarm@ with an attribute called @priority@. Priority is stored as a string (VARCHAR) type in the database and is converted to an enum value when requested. 

Using the OPTIONS constant, I have defined three priority levels: low, medium, and high. Each priority level can have different properties and methods associated with it. In my example, each enum value has a method called @email?@. By default this method returns false, but is overridden for high priority alarms and returns true.

*It is important that you include ClassyEnum AFTER declaring your OPTIONS and Default methods because they are used when creating the enum classes*

Create a file in app/models called alarm_priority.rb 

<pre>
  module AlarmPriority
    OPTIONS = [:low, :medium, :high]

    module Defaults
      def email?
        false
      end
    end

    include ClassyEnum
  end

  class AlarmPriorityHigh
    def email?
      true
    end
  end
</pre>

Then in my ActiveRecord model, Alarm, I've added a line that calls @classy_enum_attr@. The first argument is required, and is the name of the module defined above. The second argument is optional and specifies which Alarm attribute will be used as an enumerable. 

In this case, I am using the module AlarmPriority, but the name of my attribute is priority. By default, it will use the name of module as the attribute name. If I wanted to do @alarm.alarm_priority@, I would not have included the second argument.

<pre>
  class Alarm < ActiveRecord::Base
    classy_enum_attr :alarm_priority, :priority
    
    delegate :email?, :to => :priority
  end
</pre>

With this setup, I can now do the following:

<pre>
  @alarm = Alarm.create(:priority => :medium)
  
  @alarm.priority _returns AlarmPriorityMedium_
  
  @alarm.email? _returns false_
  
  @alarm.update_attribute(:priority, :high)
  
  @alarm.email? _return true_
</pre>

h2. Notes

An ActiveRecord validator @validates_inclusion_of :field, :in => ENUM.all@ is automatically added to your model when you use @classy_enum_attr@. 

h2. Copyright

Copyright (c) 2010 <NAME>. See LICENSE for details.
{'fraction_non_alphanumeric': 0.05966277561608301, 'fraction_numerical': 0.005188067444876783, 'mean_word_length': 3.9251824817518246, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1248598', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1492, 'n_words': 740}