module EnumerableConstants

This module adds a couple static-scope routines to any module that includes it, allowing them to enumerate the constants they define. Please help me make it better

An example:

module MyEnumeration
  include EnumerableConstants
  MY_FIRST_CONSTANT # defaults to 0
  MY_NEXT_CONSTANT # will be 1
  MY_THIRD_CONSTANT # will be 2, and the next would be 3 unless
  MY_FOURTH_CONSTANT = 100 # you assign a value.
  MY_FIFTH_CONSTANT # will be 101
  MY_SIXTH_CONSTANT = MY_THIRD_CONSTANT # There are no rules against duplicate values.
  MY_SEVENTH_CONSTANT # will be 3, be careful you don't unintentionally reuse values.
end

With this module, you can:

MyEnumeration.by_name #=> {:MY_FIRST_CONSTANT=>0,:MY_NEXT_CONSTANT=>1,:MY_...}

This is actually a standard function, nothing I added:

MyEnumeration.constants #=> [:MY_FIRST_CONSTANT,:MY_SECOND_CONSTANT]

Get the last value defined:

MyEnumeration.last_const_value #=> 3, based on the example above.

Get the next logical value, assuming the last value responds to .next, Otherwise, there will likely be an error. Note: This routine has no effect without assignment to a constant within this EnumerableConstants module/class. i.e.

MyEnumeration::MY_EIGTH_CONSTANT = MyEnumeration.next_const_value # 4

Or, you could just do:

MyEnumeration::MY_NINTH_CONSTANT #=> Automatically assigned value 5

However, successive calls to just ‘#next_const_value` will just result in the same value.

MyEnumeration.next_const_value # 6
MyEnumeration.next_const_value # 6

Public Class Methods

by_name() click to toggle source

Return the defined constants in a hash keyed by name.

# File lib/enumerable_constants.rb, line 53
def self.by_name
  result = {}
  self.constants.each do |constant|
    result[constant] = self.const_get(constant)
  end
  return result
end
by_value() click to toggle source

Return the defined constants in a hash keyed by value.

# File lib/enumerable_constants.rb, line 62
def self.by_value
  result = {}
  self.constants.each do |constant|
    result[self.const_get(constant)] = constant          
  end
  return result
end
const_missing(const) click to toggle source

Set the value of undefined constants to the next constant value.

# File lib/enumerable_constants.rb, line 94
def self.const_missing(const)
  self.const_set(const, next_const_value)
end
flatten_consts() click to toggle source

This routine returns a flattened array of alternating symbols and values. The symbols are the CONSTANT names and values are the values defined by that constant.

# File lib/enumerable_constants.rb, line 72
def self.flatten_consts
  by_name.to_a.flatten
end
included(base) click to toggle source

This is the method that injects the new static-scope routines.

# File lib/enumerable_constants.rb, line 48
def self.included(base)
  base.module_eval do

    ##
    # Return the defined constants in a hash keyed by name.
    def self.by_name
      result = {}
      self.constants.each do |constant|
        result[constant] = self.const_get(constant)
      end
      return result
    end
    
    # Return the defined constants in a hash keyed by value.
    def self.by_value
      result = {}
      self.constants.each do |constant|
        result[self.const_get(constant)] = constant          
      end
      return result
    end
    
    # This routine returns a flattened array of alternating symbols and values.
    # The symbols are the CONSTANT names and values are the values defined by that constant.
    def self.flatten_consts
      by_name.to_a.flatten
    end

    ##
    # Get the last defined value.
    def self.last_const_value
      if self.constants.empty?
        return -1
      else
        return self.const_get(self.constants.last)
      end
    end

    ##
    # Get the next value in order.
    def self.next_const_value
      last_const_value.next
    end
    
    ##
    # Set the value of undefined constants to the next constant value.
    def self.const_missing(const)
      self.const_set(const, next_const_value)
    end
  end
end
last_const_value() click to toggle source

Get the last defined value.

# File lib/enumerable_constants.rb, line 78
def self.last_const_value
  if self.constants.empty?
    return -1
  else
    return self.const_get(self.constants.last)
  end
end
next_const_value() click to toggle source

Get the next value in order.

# File lib/enumerable_constants.rb, line 88
def self.next_const_value
  last_const_value.next
end