lib/protocol_buffers/runtime/enum.rb in ruby-protocol-buffers-1.4.1 vs lib/protocol_buffers/runtime/enum.rb in ruby-protocol-buffers-1.5.0.beta1

- old
+ new

@@ -1,4 +1,36 @@ module ProtocolBuffers - module Enum # :nodoc: + module Enum + def self.included(clazz) + clazz.extend(EnumClassMethods) + end + end + + module EnumClassMethods + def set_fully_qualified_name(fully_qualified_name) + @fully_qualified_name = fully_qualified_name.dup.freeze + end + + def fully_qualified_name + @fully_qualified_name + end + + def value_to_names_map + @value_to_names_map ||= self.constants.inject(Hash.new) do |hash, constant| + # values do not have to be unique + value = self.const_get(constant) + hash[value] ||= Array.new + hash[value] << constant.to_sym + hash + end + @value_to_names_map + end + + def name_to_value_map + @name_to_value_map ||= self.constants.inject(Hash.new) do |hash, constant| + hash[constant.to_sym] = self.const_get(constant) + hash + end + @name_to_value_map + end end end