enum_machine 0.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,123 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EnumMachine
4
- module BuildAttribute
5
-
6
- def self.call(attr:, read_method:, enum_values:, i18n_scope:, machine: nil, aliases_keys: {})
7
- parent_attr = "@parent.#{read_method}"
8
-
9
- Class.new do
10
- def initialize(parent)
11
- @parent = parent
12
- end
13
-
14
- define_method(:machine) { machine } if machine
15
-
16
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
17
- # def to_s
18
- # @parent.__state
19
- # end
20
- #
21
- # def inspect
22
- # '<enum_machine :state>'
23
- # end
24
- #
25
- # def ==(other)
26
- # raise EnumMachine::Error, "use `state.\#{other}?` instead `state == '\#{other}'`"
27
- # end
28
-
29
- def to_s
30
- #{parent_attr}
31
- end
32
-
33
- def inspect
34
- '<enum_machine :#{attr}>'
35
- end
36
-
37
- def ==(other)
38
- raise EnumMachine::Error, "use `#{attr}.\#{other}?` instead `#{attr} == '\#{other}'`"
39
- end
40
- RUBY
41
-
42
- enum_values.each do |enum_value|
43
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
44
- # def active?
45
- # @parent.__state == 'active'
46
- # end
47
- #
48
- # def in?(values)
49
- # values.include?(@parent.__state)
50
- # end
51
-
52
- def #{enum_value}?
53
- #{parent_attr} == '#{enum_value}'
54
- end
55
-
56
- def in?(values)
57
- values.include?(#{parent_attr})
58
- end
59
- RUBY
60
-
61
- if machine&.transitions?
62
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
63
- # def can_active?
64
- # machine.possible_transitions(@parent.__state).include?('canceled')
65
- # end
66
- #
67
- # def to_canceled!
68
- # @parent.update!('state' => 'canceled')
69
- # end
70
-
71
- def can_#{enum_value}?
72
- machine.possible_transitions(#{parent_attr}).include?('#{enum_value}')
73
- end
74
-
75
- def to_#{enum_value}!
76
- @parent.update!('#{attr}' => '#{enum_value}')
77
- end
78
- RUBY
79
- end
80
- end
81
-
82
- if machine&.transitions?
83
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
84
- # def possible_transitions
85
- # machine.possible_transitions('active')
86
- # end
87
-
88
- def possible_transitions
89
- machine.possible_transitions(#{parent_attr})
90
- end
91
- RUBY
92
- end
93
-
94
- aliases_keys.each do |key|
95
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
96
- # def forming?
97
- # @parent.class::State.forming.include?('active')
98
- # end
99
-
100
- def #{key}?
101
- @parent.class::State.#{key}.include?(#{parent_attr})
102
- end
103
- RUBY
104
- end
105
-
106
- if i18n_scope
107
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
108
- # def i18n
109
- # enum_value = @parent.__state
110
- # ::I18n.t(enum_value, scope: "enums.product.state", default: enum_value)
111
- # end
112
-
113
- def i18n
114
- enum_value = #{parent_attr}
115
- ::I18n.t(enum_value, scope: "enums.#{i18n_scope}", default: enum_value)
116
- end
117
- RUBY
118
- end
119
- end
120
- end
121
-
122
- end
123
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EnumMachine
4
- class BuildClass
5
-
6
- attr_reader :values
7
-
8
- def initialize(values, aliases = {})
9
- @values = values
10
- @values.each { |v| memo_attr(v, v) }
11
- aliases.each { |k, v| memo_attr(k, v) }
12
- end
13
-
14
- def i18n_for(name)
15
- ::I18n.t(name, scope: "enums.#{i18n_scope}", default: name)
16
- end
17
-
18
- def method_missing(name)
19
- name_s = name.to_s
20
- return super unless name_s.include?('__')
21
-
22
- array_values = name_s.split('__').freeze
23
-
24
- unless (unexists_values = array_values - values).empty?
25
- raise EnumMachine::Error, "enums #{unexists_values} not exists"
26
- end
27
-
28
- memo_attr(name_s, array_values)
29
- end
30
-
31
- def respond_to_missing?(name_s, include_all)
32
- name_s.include?('__') || super
33
- end
34
-
35
- private def memo_attr(name, value)
36
- self.class.attr_reader(name)
37
- instance_variable_set("@#{name}", value)
38
- end
39
-
40
- end
41
- end