6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/enum_machine/build_class.rb', line 6
def self.call(enum_values:, i18n_scope:, machine: nil)
aliases = machine&.instance_variable_get(:@aliases) || {}
Class.new do
define_singleton_method(:machine) { machine } if machine
define_singleton_method(:values) { enum_values }
if i18n_scope
def self.values_for_form(specific_values = nil) (specific_values || values).map { |v| [human_name_for(v), v] }
end
class_eval <<-RUBY, __FILE__, __LINE__ + 1
# def self.human_name_for(name)
# ::I18n.t(name, scope: "enums.test_model", default: name)
# end
def self.human_name_for(name)
::I18n.t(name, scope: "enums.#{i18n_scope}", default: name)
end
RUBY
end
enum_values.each do |enum_value|
const_set enum_value.underscore.upcase, enum_value.freeze
end
aliases.each_key do |key|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
# def self.forming
# @alias_forming ||= machine.fetch_alias('forming').freeze
# end
def self.#{key}
@alias_#{key} ||= machine.fetch_alias('#{key}').freeze
end
RUBY
end
private_class_method def self.const_missing(name)
name_s = name.to_s
return super unless name_s.include?('__')
const_set name_s, name_s.split('__').map { |i| const_get(i) }.freeze
end
end
end
|