Top Level Namespace
Defined Under Namespace
Modules: Abstractivator, Enum, Enumerable, Kernel, MethodAndProcExtensions, Numbers
Classes: Array, Enumerator, Exception, Method, Module, Object, Proc, Symbol, UnboundMethod
Instance Method Summary
collapse
Instance Method Details
#define_enum(name, *fields) ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/abstractivator/enum.rb', line 63
def define_enum(name, *fields)
if respond_to?(:const_set)
const_set(name, make_enum(*fields))
else Object.send(:const_set, name, make_enum(*fields))
end
end
|
#make_enum(*fields) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/abstractivator/enum.rb', line 71
def make_enum(*fields)
if fields.size == 1 && fields.first.is_a?(Hash) && fields.first.keys.all?{|f| f.is_a?(Symbol)}
fields = fields.first
Class.new do
include Enum
private_class_method :new
fields.each_pair do |k, v|
val = new(v)
fld = k.to_s.upcase.to_sym
const_set(fld, val)
end
end
elsif fields.all?{|f| f.is_a?(Symbol)}
make_enum(fields.hash_map(&:to_s))
else
raise 'Arguments must be one or more symbols or a single symbol-keyed hash'
end
end
|