pokotarou 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pokotarou.rb +21 -38
  3. data/lib/pokotarou/additional_arguments/main.rb +24 -0
  4. data/lib/pokotarou/additional_methods/main.rb +49 -0
  5. data/lib/pokotarou/additional_variables/main.rb +28 -0
  6. data/lib/pokotarou/parser/const_parser.rb +25 -0
  7. data/lib/pokotarou/parser/loop_expression_parser.rb +32 -0
  8. data/lib/pokotarou/parser/parser.rb +108 -0
  9. data/lib/pokotarou/parser/parser_domain.rb +40 -0
  10. data/lib/pokotarou/parser/return_expression_parser..rb +27 -0
  11. data/lib/pokotarou/parser/seed_data_expression_parser.rb +18 -0
  12. data/lib/pokotarou/registration_config_maker/column_domain.rb +17 -0
  13. data/lib/pokotarou/registration_config_maker/config_domain.rb +43 -0
  14. data/lib/pokotarou/registration_config_maker/grouping_option_setter.rb +28 -0
  15. data/lib/pokotarou/registration_config_maker/import_option_setter.rb +13 -0
  16. data/lib/pokotarou/registration_config_maker/main.rb +58 -0
  17. data/lib/pokotarou/registration_config_maker/model_option_setter.rb +65 -0
  18. data/lib/pokotarou/registration_config_maker/template_option_setter.rb +63 -0
  19. data/lib/pokotarou/registration_config_updater/array_utils.rb +15 -0
  20. data/lib/pokotarou/registration_config_updater/convert_config.rb +28 -0
  21. data/lib/pokotarou/registration_config_updater/default_value_maker.rb +32 -0
  22. data/lib/pokotarou/registration_config_updater/main.rb +170 -0
  23. data/lib/pokotarou/registration_config_updater/option_config.rb +53 -0
  24. data/lib/pokotarou/seed_data_register/main.rb +81 -0
  25. data/lib/pokotarou/version.rb +1 -1
  26. metadata +24 -15
  27. data/lib/pokotarou/additional_methods.rb +0 -40
  28. data/lib/pokotarou/additional_variables/additional_variables.rb +0 -33
  29. data/lib/pokotarou/additional_variables/def_variable.rb +0 -3
  30. data/lib/pokotarou/arguments/arguments.rb +0 -24
  31. data/lib/pokotarou/arguments/def_args.rb +0 -1
  32. data/lib/pokotarou/array_operation.rb +0 -13
  33. data/lib/pokotarou/converter.rb +0 -23
  34. data/lib/pokotarou/data_register.rb +0 -233
  35. data/lib/pokotarou/data_structure.rb +0 -187
  36. data/lib/pokotarou/expression_parser.rb +0 -233
  37. data/lib/pokotarou/file_loader.rb +0 -16
  38. data/lib/pokotarou/option.rb +0 -46
  39. data/lib/pokotarou/seeder.rb +0 -29
@@ -1,187 +0,0 @@
1
- require "pokotarou/additional_methods.rb"
2
-
3
- class DataStructure
4
- class << self
5
- def gen data
6
- execute_template_option_setting(data)
7
- execute_import_setting(data)
8
- # return data structure bellow
9
- # [{ block_name => { model_name => { column_configration }}}, ...]
10
- data.reduce({}) do |acc, r|
11
- if is_dush?(r.first.to_s)
12
- acc[r.first] = r.second
13
- else
14
- set_reshape_data_to_acc(acc, r)
15
- end
16
-
17
- acc
18
- end
19
- end
20
-
21
- private
22
-
23
- def execute_import_setting data
24
- return unless data.has_key?(:"import'")
25
- AdditionalMethods.import_from_yml(data[:"import'"])
26
- data.delete(:"import'")
27
- end
28
-
29
- def execute_template_option_setting data
30
- return unless data.has_key?(:"template'")
31
- templates = data[:"template'"]
32
- data.delete(:"template'")
33
- data.each do |key, val|
34
- next if is_dush?(key.to_s)
35
- set_template_option(val, templates)
36
- end
37
- end
38
-
39
- def set_template_option model_data, templates
40
- model_data.each do |key, val|
41
- next unless has_template?(val)
42
- template_name = val[:template]
43
- template = templates[template_name.to_sym]
44
- copy_template = template.deep_dup
45
- # when a new key is generated, it is added behind
46
- # so, overwrite config_data to template first
47
-
48
- # from val to copy_template
49
- deep_overwrite(val, copy_template)
50
- # update config data
51
- model_data[key] = copy_template
52
- end
53
- end
54
-
55
- def deep_overwrite from_hash, to_hash
56
- from_hash.each do |key, val|
57
- if val.kind_of?(Hash)
58
- to_hash[key] ||= {}
59
- deep_overwrite(val, to_hash[key])
60
- else
61
- to_hash[key] = val
62
- end
63
- end
64
- end
65
-
66
- def set_reshape_data_to_acc acc, r
67
- execute_grouping_option_setting(r.second)
68
- # r.first is block_name
69
- # r.second is model_data, like { Pref: {loop: 3}, Member: {loop: 3}... }
70
- acc[r.first] = gen_structure(r.second)
71
- end
72
-
73
- def execute_grouping_option_setting model_data
74
- model_data.each do |key, val|
75
- set_grouping_option(val) if has_grouping?(val)
76
- end
77
- end
78
-
79
- def set_grouping_option val
80
- val[:grouping].each do |grouping_key, cols|
81
- apply_grouping_col(:col, val, grouping_key, cols)
82
- apply_grouping_col(:option, val, grouping_key, cols)
83
- apply_grouping_col(:convert, val, grouping_key, cols)
84
- end
85
-
86
- val.delete(:grouping)
87
- end
88
-
89
- def apply_grouping_col config_name, val, grouping_key, cols
90
- return if val[config_name].blank?
91
- return unless val[config_name].has_key?(grouping_key)
92
- cols.each do |e|
93
- val[config_name][e.to_sym] = val[config_name][grouping_key]
94
- end
95
-
96
- val[config_name].delete(grouping_key)
97
- end
98
-
99
- def gen_structure model_data
100
- model_data.reduce({}) do |acc, r|
101
- # r.second is config_data, like {loop: 3, ...}
102
- set_col_type(r.second, r[0].to_s)
103
- acc[r[0]] = r.second
104
-
105
- acc
106
- end
107
- end
108
-
109
- def set_col_type config_data, str_model
110
- model = eval(str_model)
111
- foreign_key_data = get_foreign_key_data(model)
112
-
113
- config_data[:col] ||= {}
114
- model.columns.each do |e|
115
- symbol_col_name = e.name.to_sym
116
-
117
- unless exists_seed_data?(config_data, symbol_col_name)
118
- # prepare setting to run default seed
119
- # set nil to seed data
120
- config_data[:col][symbol_col_name] = nil
121
-
122
- # set type info
123
- config_data[:type] ||= {}
124
- config_data[:type][symbol_col_name] = e.type.to_s
125
-
126
- # set enum info
127
- config_data[:enum] ||= {}
128
- if is_enum?(e.sql_type.to_s)
129
- config_data[:enum][symbol_col_name] =
130
- e.sql_type.to_s[5..-2].tr("'", "").split(",")
131
- end
132
-
133
- # set foreign_key info
134
- if is_foreign_key?(symbol_col_name, foreign_key_data)
135
- # delete type val for don't run default seeder
136
- config_data[:type].delete(symbol_col_name)
137
- # use F function for set foreign key
138
- config_data[:col][symbol_col_name] = "F|#{foreign_key_data[symbol_col_name].to_s}"
139
- end
140
- end
141
-
142
- end
143
- end
144
-
145
- def get_foreign_key_data model
146
- associations = model.reflect_on_all_associations(:belongs_to)
147
- return { } if associations.empty?
148
- associations.reduce({})do |acc, r|
149
- model = r.name.to_s.camelize
150
- if Object.const_defined?(model.to_sym)
151
- acc[r.foreign_key.to_sym] = eval(model)
152
- end
153
-
154
- acc
155
- end
156
- end
157
-
158
- def is_foreign_key? symbol_col_name, foreign_key_data
159
- foreign_key_data[symbol_col_name].present?
160
- end
161
-
162
- def exists_seed_data? config_data ,symbol_col_name
163
- config_data[:col].has_key?(symbol_col_name)
164
- end
165
-
166
- ENUM = /^enum(\s*.)*$/
167
- def is_enum? val
168
- return false unless val.kind_of?(String)
169
- ENUM =~ val
170
- end
171
-
172
- DUSH_OPTION = /^.*\'$/
173
- def is_dush? val
174
- return false unless val.kind_of?(String)
175
- DUSH_OPTION =~ val
176
- end
177
-
178
- def has_grouping? config_data
179
- return false if config_data.blank?
180
- config_data.has_key?(:grouping)
181
- end
182
-
183
- def has_template? config_data
184
- config_data.has_key?(:template)
185
- end
186
- end
187
- end
@@ -1,233 +0,0 @@
1
- require "pokotarou/additional_methods.rb"
2
- require "pokotarou/additional_variables/additional_variables.rb"
3
- require "pokotarou/arguments/arguments.rb"
4
- require "pokotarou/p_tool.rb"
5
-
6
- def load_filepath
7
- require AdditionalVariables.filepath
8
- require Arguments.filepath
9
- AdditionalMethods.filepathes.each do |filepath|; require filepath end
10
- AdditionalMethods.filepathes_from_yml.each do |filepath|; require filepath end
11
- end
12
-
13
- class ParseError < StandardError; end
14
- FOREIGN_KEY_SYMBOL = "F|"
15
- COLUMN_SYMBOL = "C|"
16
-
17
- class ExpressionParser
18
- class << self
19
-
20
- def parse config_val, maked = nil, maked_col = nil
21
- begin
22
- case
23
- # Array
24
- when is_array?(config_val)
25
- array_procees(config_val)
26
-
27
- # ForeignKey
28
- when is_foreign_key?(config_val)
29
- foreign_key_process(config_val, maked_col)
30
-
31
- # Column
32
- when is_column_symbol?(config_val)
33
- column_symbol_process(config_val, maked_col)
34
-
35
- # Expression
36
- when is_expression?(config_val)
37
- expression_process(config_val, maked, maked_col)
38
-
39
- # Integer
40
- when is_integer?(config_val)
41
- integer_process(config_val)
42
-
43
- # NeedUpdate
44
- when is_need_update?(config_val)
45
- need_update_process(config_val, maked, maked_col)
46
-
47
- # Nil
48
- when is_nil?(config_val)
49
- nil_process(config_val)
50
-
51
- # Other
52
- else
53
- nothing_apply_process(config_val)
54
- end
55
- rescue => e
56
- output_error(e)
57
- end
58
- end
59
-
60
- private
61
- def array_procees val
62
- return val
63
- end
64
-
65
- def foreign_key_process val, maked_col
66
- # remove 'F|'
67
- str_model = val.sub(FOREIGN_KEY_SYMBOL, "")
68
- model = eval(str_model)
69
- return model.pluck(:id)
70
- end
71
-
72
- def column_symbol_process val, maked_col
73
- # remove 'C|'
74
- str_model, column = val.sub(COLUMN_SYMBOL, "").split("|")
75
- model = eval(str_model)
76
- elemnts = model.pluck(column.to_sym)
77
- return elemnts.concat(maked_col[str_model.to_sym][column.to_sym])
78
- end
79
-
80
- def expression_process val, maked, maked_col
81
- # remove '<>'
82
- expression = val.strip[1..-2]
83
- load_filepath()
84
- return self.parse(eval(expression), maked, maked_col)
85
- end
86
-
87
- def integer_process val
88
- nothing_apply_process(val)
89
- end
90
-
91
- def need_update_process val, maked, maked_col
92
- return self.parse(val[:NeedUpdate], maked, maked_col)
93
- end
94
-
95
- def nil_process val
96
- nothing_apply_process(val)
97
- end
98
-
99
- def nothing_apply_process val
100
- # for escape \\
101
- val.instance_of?(String) ? val.tr("\\","") : val
102
- end
103
-
104
- def output_error e
105
- raise ParseError.new("Failed Expression parse:#{e.message}")
106
- end
107
- end
108
- end
109
-
110
- # for seed data
111
- class SeedExpressionParser < ExpressionParser
112
- class << self
113
- private
114
- def nothing_apply_process val
115
- # for escape \\
116
- val.instance_of?(String) ? [val.tr("\\","")] : [val]
117
- end
118
-
119
- def output_error e
120
- raise ParseError.new("Failed Seed Expression parse:#{e.message}")
121
- end
122
- end
123
- end
124
-
125
- # for return variables
126
- class ReturnExpressionParser < ExpressionParser
127
- class << self
128
- private
129
- def output_error e
130
- ParseError.new("Failed Const Expression parse:#{e.message}")
131
- end
132
-
133
- def foreign_key_process val, _
134
- # remove 'F|'
135
- str_model = val.sub(FOREIGN_KEY_SYMBOL, "")
136
- model = eval(str_model)
137
- return model.pluck(:id)
138
- end
139
-
140
- def column_symbol_process val, _
141
- # remove 'C|'
142
- str_model, column = val.sub(COLUMN_SYMBOL, "").split("|")
143
- model = eval(str_model)
144
- return model.pluck(column.to_sym)
145
- end
146
- end
147
- end
148
-
149
- # for loop data
150
- class LoopExpressionParser < ExpressionParser
151
- class << self
152
- private
153
- def array_procees val
154
- val.size
155
- end
156
-
157
- def foreign_key_process val, maked_col
158
- # remove 'F|'
159
- str_model = val.sub(FOREIGN_KEY_SYMBOL, "")
160
- model = eval(str_model)
161
- return model.pluck(:id).size
162
- end
163
-
164
- def integer_process val
165
- val
166
- end
167
-
168
- def nil_process _
169
- 1
170
- end
171
-
172
- def output_error e
173
- ParseError.new("Failed Loop Expression parse: #{e.message}")
174
- end
175
- end
176
- end
177
-
178
- # for const variables
179
- class ConstParser < ExpressionParser
180
- class << self
181
- private
182
- def expression_process val, _, _
183
- # remove '<>'
184
- expression = val.strip[1..-2]
185
- load_filepath()
186
- return self.parse(eval(expression))
187
- end
188
-
189
- def nothing_apply_process val
190
- # for escape \\
191
- val.instance_of?(String) ? val.tr("\\","") : val
192
- end
193
-
194
- def output_error
195
- ParseError.new("Failed Const Expression parse: #{e.message}")
196
- end
197
- end
198
- end
199
-
200
- FOREIGN_KEY_REGEXP = /^F\|[A-Z][:A-Za-z0-9]*$/
201
- def is_foreign_key? val
202
- return false unless val.kind_of?(String)
203
- FOREIGN_KEY_REGEXP =~ val
204
- end
205
-
206
- COLUMN_REGEXP = /^C\|[A-Z][:A-Za-z0-9]*\|[a-z][:a-z0-9]*$/
207
- def is_column_symbol? val
208
- return false unless val.kind_of?(String)
209
- COLUMN_REGEXP =~ val
210
- end
211
-
212
- EXPRESSION_REGEXP = /^\s*<.*>\s*$/
213
- def is_expression? val
214
- return false unless val.kind_of?(String)
215
- EXPRESSION_REGEXP =~ val
216
- end
217
-
218
- def is_array? val
219
- val.instance_of?(Array)
220
- end
221
-
222
- def is_integer? val
223
- val.instance_of?(Integer)
224
- end
225
-
226
- def is_need_update? val
227
- return false unless val.kind_of?(Hash)
228
- val.has_key?(:NeedUpdate)
229
- end
230
-
231
- def is_nil? val
232
- val.nil?
233
- end
@@ -1,16 +0,0 @@
1
- class FileLoader
2
- class << self
3
- # load data with the use of indicated filepath
4
- def load filepath
5
- end
6
- end
7
- end
8
-
9
- class YmlLoader < FileLoader
10
- class << self
11
- def load filepath
12
- # convert 'str_key' to 'symbol_key'
13
- YAML.load_file(filepath).deep_symbolize_keys!
14
- end
15
- end
16
- end
@@ -1,46 +0,0 @@
1
- class Option
2
- class << self
3
- def gen option
4
- # shape option data
5
- # [option1, option2] => { select: [option2], add: [option1] }
6
- # [] => { select: [], add: [] }
7
- option.nil? ? { select: [], add: [] } : separate(option)
8
- end
9
-
10
- def apply arr, size, option_conf, cnt = 0
11
- selected_val = select(option_conf[:select], arr, size, cnt)
12
-
13
- add(option_conf[:add], selected_val, cnt)
14
- end
15
-
16
- private
17
-
18
- def separate option
19
- # separate option to 'select' and 'add'
20
- # { select = >[], add => [] }
21
- select_filter = ->(name){ ["rotate", "random"].include?(name) }
22
- add_filter = ->(name){ ["add_id", "sequence"].include?(name) }
23
-
24
- {
25
- select: option.find{|s| select_filter.call(s)},
26
- add: option.find{|s| add_filter.call(s)}
27
- }
28
- end
29
-
30
- def select option, arr, size, cnt
31
- return arr.sample if option == "random"
32
-
33
- # default return rotate
34
- ArrayOperation.get_rotated_val(arr, size, cnt)
35
- end
36
-
37
- def add option, val, cnt
38
- # if val is nil, return nil
39
- return nil if val.nil?
40
- # not use '<<' to avoid destructive effect
41
- return "#{val}_#{cnt}" if option == "add_id" || option == "sequence"
42
-
43
- val
44
- end
45
- end
46
- end