Sha256: be44eb805b458c4629d048ea3ff7829006aca63adc24daeae668f6cefc60dcf8

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module EnumeratedField

  def self.included(klass)
    klass.extend ClassMethods
  end

  module ClassMethods

    # ex. enum_field(:league, [['National Football League', :nfl], ['Major League Baseball', :mlb]])
    # field_name typically corresponds to the database column name
    # values_array is a double array (not a hash to preserve order for when order matters.. ie select options)    
    def enum_field(field_name, values_array)
      values_hash = {}
      values_array.each { |value, key| values_hash[key] = value }
      
      class_eval do

        # returns the values_array for this field, useful for providing to 
        # options_for_select when constructing forms
        define_method("#{field_name}_values") do |*options|
          options = options.first || {}
          if options[:first_option]
            [[options[:first_option], '']] + values_array
          else
            values_array
          end
        end

        # returns display value for the current value of the field
        define_method("#{field_name}_display") do
          values_hash[send(field_name)]
        end

        # returns display value for the given value of the field
        define_method("#{field_name}_display_for") do |key|
          values_hash[key]
        end
        
        define_method("#{field_name}_value_for") do |key|
          values_hash.invert[key]
        end

        # defines question methods for each possible value of the field
        # ex.  object.league_nfl?  which returns true if the objects league 
        # field is currently set to nfl otherwise false
        values_hash.keys.each do |key|
          define_method("#{field_name}_#{key}?") { send(field_name) == key }
        end

      end
    end

  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
enumerated_field-0.0.1 lib/enumerated_field.rb