Created
June 7, 2012 22:55
-
-
Save simaob/2892199 to your computer and use it in GitHub Desktop.
A quick dictionary module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChangeType < ActiveRecord::Base | |
attr_accessible :listing_change_id, :name | |
include Dictionary | |
build_dictionary :addition, :deletion, :reservation, :reservation_withdrawal | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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