lib/abstractivator/enum.rb in abstractivator-0.0.28 vs lib/abstractivator/enum.rb in abstractivator-0.0.29
- old
+ new
@@ -6,10 +6,12 @@
def self.included(base)
base.extend ClassMethods
# base.extend Dsl
end
+ module Value; end # used to mark enum values
+
module ClassMethods
def values
self.constants.map{|sym| self.const_get(sym)}.reject{|x| x.is_a?(Class) || x.is_a?(Module)}
end
@@ -20,11 +22,15 @@
def from_symbol(sym)
safe_constantize("#{self.name}::#{sym.to_s.upcase}")
end
def from(x)
- values.find{|v| v.value == x}
+ if x.is_a?(Enum::Value)
+ x
+ else
+ values.find{|v| v.value == x}
+ end
end
private
def safe_constantize(str)
@@ -51,9 +57,10 @@
Module.new do
include Enum
value_class =
const_set(:Value,
Class.new do
+ include Enum::Value
attr_reader :enum_type, :value
define_method(:initialize) do |enum_type, value|
@enum_type, @value = enum_type, value
end
define_method(:inspect) do