lib/enumeration.rb in enumeration-0.0.1 vs lib/enumeration.rb in enumeration-1.0.0

- old
+ new

@@ -1,3 +1,45 @@ +module Enumeration; end +require 'enumeration/collection' + module Enumeration - # Your code goes here... + + module ClassMethods + def enum(name, map_or_list) + # TODO: validate name + c = Collection.new(map_or_list) + + # define an anonymous Module to extend on + # defining a class level map reader + class_methods = Module.new do + define_method(name) {|k| class_variable_get("@@#{name}")[k]} if c.map? + define_method(name.to_s+'_set') { c.set } + end + + # set a class variable to store the enum map (used by above reader) + # extend the anonymous module to get tne above class + # level reader for the map + class_eval do + class_variable_set("@@#{name}", c) + extend class_methods + end + + # instance writer for the enum value + define_method("#{name}=") do |value| + c = self.class.send(:class_variable_get, "@@#{name}") + instance_variable_set("@#{name}", c[value]) + end + + # instance reader for the enum value + define_method(name) do + instance_variable_get("@#{name}") + end + end + end + + class << self + def included(receiver) + receiver.send :extend, ClassMethods + end + end + end \ No newline at end of file