Skip to content

Instantly share code, notes, and snippets.

@simaob
Created June 7, 2012 22:55
Show Gist options
  • Save simaob/2892199 to your computer and use it in GitHub Desktop.
Save simaob/2892199 to your computer and use it in GitHub Desktop.
A quick dictionary module
class ChangeType < ActiveRecord::Base
attr_accessible :listing_change_id, :name
include Dictionary
build_dictionary :addition, :deletion, :reservation, :reservation_withdrawal
end
module Dictionary
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
#builds a set of constants like: KEY = 'KEY'
#as well as a method self.dict that returns all of those constants' values
def build_dictionary *keys
keys.each do |key|
const_set key.to_s.upcase, key.to_s.upcase
end
define_singleton_method("dict") { keys.map{|k| k.to_s.upcase } }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment