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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/enum_machine/build_attribute.rb', line 6
def self.call(enum_values:, i18n_scope:, machine: nil)
aliases = machine&.instance_variable_get(:@aliases) || {}
Class.new(String) do
define_method(:machine) { machine } if machine
def inspect
"#<EnumMachine:BuildAttribute value=#{self}>"
end
if machine&.transitions?
def possible_transitions
machine.possible_transitions(self)
end
def can?(enum_value)
possible_transitions.include?(enum_value)
end
end
enum_values.each do |enum_value|
enum_name = enum_value.underscore
class_eval <<-RUBY, __FILE__, __LINE__ + 1
# def active?
# self == 'active'
# end
def #{enum_name}?
self == '#{enum_value}'
end
RUBY
if machine&.transitions?
class_eval <<-RUBY, __FILE__, __LINE__ + 1
# def can_active?
# possible_transitions.include?('canceled')
# end
def can_#{enum_name}?
possible_transitions.include?('#{enum_value}')
end
RUBY
end
end
aliases.each_key do |key|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
# def forming?
# machine.fetch_alias('forming').include?(self)
# end
def #{key}?
machine.fetch_alias('#{key}').include?(self)
end
RUBY
end
if i18n_scope
class_eval <<-RUBY, __FILE__, __LINE__ + 1
# def human_name
# ::I18n.t(self, scope: "enums.product.state", default: self)
# end
def human_name
::I18n.t(self, scope: "enums.#{i18n_scope}", default: self)
end
RUBY
end
end
end
|