@@ -4,15 +4,14 @@ module Attributes
4
4
module Builder
5
5
class Enum
6
6
7
- attr_accessor :klass , :attribute , :subtype , :initial , : options, :values
7
+ attr_accessor :klass , :attribute , :subtype , :options , :values
8
8
9
9
# Start a new builder of methods for composite values on
10
10
# ActiveRecord::Base
11
- def initialize ( klass , attribute , subtype , initial , options )
11
+ def initialize ( klass , attribute , subtype , options )
12
12
@klass = klass
13
13
@attribute = attribute . to_s
14
14
@subtype = subtype
15
- @initial = initial
16
15
@options = options
17
16
18
17
@values = subtype . klass . values
@@ -53,29 +52,24 @@ def values_methods
53
52
# with the base class methods
54
53
def conflicting?
55
54
return false if options [ :force ] == true
55
+ attributes = attribute . pluralize
56
56
57
- dangerous? ( attribute . pluralize , true )
58
- dangerous? ( attribute + '_text' )
57
+ dangerous? ( attributes , true )
58
+ dangerous? ( "#{ attributes } _options" , true )
59
+ dangerous? ( "#{ attributes } _texts" , true )
60
+ dangerous? ( "#{ attribute } _text" )
59
61
60
62
values_methods . each do |attr , list |
61
63
list . map ( &method ( :dangerous? ) )
62
64
end
63
65
64
66
return false
65
67
rescue Interrupt => err
66
- if !initial
67
- raise ArgumentError , <<-MSG . strip . gsub ( /\n +/ , ' ' )
68
- #{ subtype . class . name } was not able to generate requested
69
- methods because the method #{ err } already exists in
70
- #{ klass . name } .
71
- MSG
72
- else
73
- warn <<-MSG . strip . gsub ( /\n +/ , ' ' )
74
- #{ subtype . class . name } was not able to autoload on
75
- #{ klass . name } because the method #{ err } already exists.
76
- MSG
77
- return true
78
- end
68
+ raise ArgumentError , <<-MSG . strip . gsub ( /\n +/ , ' ' )
69
+ #{ subtype . class . name } was not able to generate requested
70
+ methods because the method #{ err } already exists in
71
+ #{ klass . name } .
72
+ MSG
79
73
end
80
74
81
75
# Create all methods needed
@@ -102,45 +96,60 @@ def dangerous?(method_name, class_method = false)
102
96
103
97
# Create the method that allow access to the list of values
104
98
def plural
105
- klass . singleton_class . module_eval <<-STR , __FILE__ , __LINE__ + 1
106
- def #{ attribute . pluralize } # def statuses
107
- ::#{ subtype . klass . name } .values # ::Enum::Status.values
108
- end # end
109
- STR
99
+ attr = attribute
100
+ enum_klass = subtype . klass
101
+ klass . singleton_class . module_eval do
102
+ # def self.statuses() statuses end
103
+ define_method ( attr . pluralize ) do
104
+ enum_klass . values
105
+ end
106
+
107
+ # def self.statuses_texts() members.map(&:text) end
108
+ define_method ( attr . pluralize + '_texts' ) do
109
+ enum_klass . members . map do |member |
110
+ member . text ( attr , self )
111
+ end
112
+ end
113
+
114
+ # def self.statuses_options() statuses_texts.zip(statuses) end
115
+ define_method ( attr . pluralize + '_options' ) do
116
+ enum_klass . values
117
+ end
118
+ end
110
119
end
111
120
112
121
# Create the method that turn the attribute value into text using
113
122
# the model scope
114
123
def text
115
- klass . module_eval <<-STR , __FILE__ , __LINE__ + 1
116
- def #{ attribute } _text # def status_text
117
- #{ attribute } .text(' #{ attribute } ', self) # status.text('status', self)
118
- end # end
119
- STR
124
+ attr = attribute
125
+ klass . module_eval do
126
+ # def status_text() status.text('status', self) end
127
+ define_method ( " #{ attr } _text" ) { send ( attr ) . text ( attr , self ) }
128
+ end
120
129
end
121
130
122
131
# Create all the methods that represent actions related to the
123
132
# attribute value
124
133
def all_values
125
- values_methods . each do | val , list |
126
- klass . module_eval <<-STR , __FILE__ , __LINE__ + 1
127
- scope : #{ list [ 0 ] } , -> do # scope :disabled, -> do
128
- where( #{ attribute } : ' #{ val } ') # where(status: 'disabled')
129
- end # end
130
- STR
131
- klass . module_eval <<-STR , __FILE__ , __LINE__ + 1
132
- def #{ list [ 1 ] } # def disabled?
133
- #{ attribute } . #{ val } ? # status.disabled?
134
- end # end
135
-
136
- def #{ list [ 2 ] } # def disabled!
137
- if enum_save_on_bang # if enum_save_on_bang
138
- update!(#{ attribute } : ' #{ val } ') # update!(status: 'disabled' )
139
- else # else
140
- #{ attribute } . #{ val } ! # status.disabled!
141
- end # end
142
- end # end
143
- STR
134
+ attr = attribute
135
+ vals = values_methods
136
+ klass . module_eval do
137
+ vals . each do | val , list |
138
+ # scope :disabled, -> { where(status: 'disabled') }
139
+ scope list [ 0 ] , -> { where ( attr => val ) }
140
+
141
+ # def disabled? status.disabled? end
142
+ define_method ( list [ 1 ] ) { send ( attr ) . public_send ( " #{ val } ?" ) }
143
+
144
+ # def disabled! enum_save_on_bang ? update!(status: 'disabled') : status.disabled! end
145
+ define_method ( list [ 2 ] ) do
146
+ if enum_save_on_bang
147
+ update! ( attr => val )
148
+ else
149
+ send ( attr ) . public_send ( " #{ val } !" )
150
+ end
151
+ end
152
+ end
144
153
end
145
154
end
146
155
0 commit comments