pokotarou 1.1.7 → 1.2.2

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pokotarou.rb +146 -73
  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 +48 -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 +65 -0
  17. data/lib/pokotarou/registration_config_maker/model_option_setter.rb +65 -0
  18. data/lib/pokotarou/registration_config_maker/preset_option_setter.rb +45 -0
  19. data/lib/pokotarou/registration_config_maker/template_option_setter.rb +63 -0
  20. data/lib/pokotarou/registration_config_updater/array_utils.rb +15 -0
  21. data/lib/pokotarou/registration_config_updater/convert_config.rb +28 -0
  22. data/lib/pokotarou/registration_config_updater/default_value_maker.rb +32 -0
  23. data/lib/pokotarou/registration_config_updater/main.rb +170 -0
  24. data/lib/pokotarou/registration_config_updater/option_config.rb +53 -0
  25. data/lib/pokotarou/seed_data_register/main.rb +81 -0
  26. data/lib/pokotarou/version.rb +1 -1
  27. metadata +26 -17
  28. data/lib/pokotarou/additional_methods.rb +0 -40
  29. data/lib/pokotarou/additional_variables/additional_variables.rb +0 -33
  30. data/lib/pokotarou/additional_variables/def_variable.rb +0 -3
  31. data/lib/pokotarou/arguments/arguments.rb +0 -24
  32. data/lib/pokotarou/arguments/def_args.rb +0 -1
  33. data/lib/pokotarou/array_operation.rb +0 -13
  34. data/lib/pokotarou/converter.rb +0 -23
  35. data/lib/pokotarou/data_register.rb +0 -230
  36. data/lib/pokotarou/data_structure.rb +0 -187
  37. data/lib/pokotarou/expression_parser.rb +0 -233
  38. data/lib/pokotarou/file_loader.rb +0 -16
  39. data/lib/pokotarou/option.rb +0 -46
  40. data/lib/pokotarou/pokotarou_handler.rb +0 -37
  41. data/lib/pokotarou/seeder.rb +0 -29
@@ -1,40 +0,0 @@
1
- module AdditionalMethods
2
- class << self
3
- attr_reader :filepathes
4
- attr_reader :filepathes_from_yml
5
-
6
- @filepathes = []
7
- @filepathes_from_yml = []
8
-
9
- def init
10
- @filepathes ||= []
11
- @filepathes_from_yml = []
12
- end
13
-
14
- def import filepath
15
- add(@filepathes, filepath)
16
- end
17
-
18
- def import_from_yml filepath
19
- add(@filepathes_from_yml, filepath)
20
- end
21
-
22
- def remove
23
- @filepathes = []
24
- @filepathes_from_yml = []
25
- end
26
-
27
- def remove_filepathes_from_yml
28
- @filepathes_from_yml = []
29
- end
30
-
31
- private
32
- def add filepath_arr, filepath
33
- if filepath.instance_of?(Array)
34
- filepath_arr.concat(filepath)
35
- else
36
- filepath_arr.push(filepath)
37
- end
38
- end
39
- end
40
- end
@@ -1,33 +0,0 @@
1
- module AdditionalVariables
2
- class << self
3
- CONST_KEY = :"const'"
4
- @const = {}
5
- attr_reader :const
6
-
7
- def set_const data
8
- set_const_variables(data)
9
- end
10
-
11
- def remove
12
- @const = {}
13
- end
14
-
15
- def filepath
16
- "pokotarou/additional_variables/def_variable.rb"
17
- end
18
-
19
- private
20
- def set_const_variables data
21
- return {} unless data.has_key?(CONST_KEY)
22
- @const = data[CONST_KEY]
23
-
24
- # parse expression configlation
25
- @const.each do |key, val|
26
- @const[key] = ConstParser.parse(val)
27
- end
28
-
29
- data.delete(CONST_KEY)
30
- end
31
-
32
- end
33
- end
@@ -1,3 +0,0 @@
1
- def const; AdditionalVariables.const end
2
-
3
- def let; AdditionalVariables.let end
@@ -1,24 +0,0 @@
1
- class MisMatchArgType < StandardError; end
2
- module Arguments
3
- class << self
4
- @args = nil
5
- attr_reader :args
6
-
7
- def import hash_data
8
- unless hash_data.kind_of?(Hash)
9
- raise MisMatchArgType.new("Please use Hash for args")
10
- end
11
-
12
- @args = hash_data
13
- end
14
-
15
- def remove
16
- @args = nil
17
- end
18
-
19
- def filepath
20
- "pokotarou/arguments/def_args.rb"
21
- end
22
-
23
- end
24
- end
@@ -1 +0,0 @@
1
- def args; Arguments.args end
@@ -1,13 +0,0 @@
1
- class NothingDataError < StandardError; end
2
- class ArrayOperation
3
- class << self
4
- # return rotated val in passed array
5
- def get_rotated_val array, size, cnt
6
- raise NothingDataError.new("Nothing seed data") if array.nil?
7
- raise NothingDataError.new("Seed data is empty") if size.zero?
8
- x = (size + cnt) / size
9
-
10
- array[ size + cnt - size * x ]
11
- end
12
- end
13
- end
@@ -1,23 +0,0 @@
1
- class Converter
2
- class << self
3
- PARENTHESES = /\(.*\)/
4
- def convert arr, config
5
- return arr if config.nil?
6
- config.each do |e|
7
- range = eval(e.slice(PARENTHESES).delete("()"))
8
- convert_name = e.gsub(PARENTHESES, "")
9
- arr[range] = get_val(convert_name, range.size)
10
- end
11
-
12
- arr
13
- end
14
-
15
- def get_val convert_name, size
16
- return [""] * size if convert_name == "empty"
17
- return [nil] * size if convert_name == "nil"
18
- return ["text\n" * 5] * size if convert_name == "br_text"
19
- return ["text" * 50] * size if convert_name == "big_text"
20
- end
21
-
22
- end
23
- end
@@ -1,230 +0,0 @@
1
- require "pokotarou/additional_variables/additional_variables.rb"
2
-
3
- class RegisterError < StandardError; end
4
- class SeedError < StandardError; end
5
-
6
- class DataRegister
7
- class << self
8
- def register data
9
- # init maked to accumulate maded data
10
- maked = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
11
- maked_col = Hash.new { |h,k| h[k] = {} }
12
- # init model_data to cache data of model
13
- model_cache = {}
14
- ActiveRecord::Base.transaction do
15
- begin
16
- data.each do |sym_block, model_data|
17
- next if is_dush?(sym_block.to_s)
18
- register_val_by_bulk(sym_block, model_data, maked, model_cache, maked_col)
19
- end
20
- rescue => e
21
- raise StandardError.new("#{e.message}")
22
- end
23
- end
24
-
25
- ReturnExpressionParser.parse(data[:"return'"], maked, maked_col)
26
- end
27
-
28
- private
29
-
30
- def register_val_by_bulk sym_block, model_data, maked, model_cache, maked_col
31
- model_data.each do |e|
32
- begin
33
- str_model = e.first.to_s
34
- save_model_cache(model_cache, str_model)
35
-
36
- model = model_cache[str_model][:model]
37
- sym_model = model_cache[str_model][:sym_model]
38
- # model_data.values is config_data
39
- config_data = e.second
40
- # set expand expression for loop '<>' and ':' and so on...
41
- set_loop_expand_expression(config_data, maked, maked_col)
42
- # if there is no setting data, set default seed data
43
- set_default_seed(config_data)
44
- # seed_arr: [[col1_element, col1_element], [col2_element, col2_element]...]
45
- set_seed_arr(model, sym_block, sym_model, config_data, maked, maked_col)
46
-
47
- output_log(config_data[:log])
48
- insert_record(sym_block, str_model, config_data ,model_cache)
49
- rescue => e
50
- print "\e[31m"
51
- puts "[Pokotarou ERROR]"
52
- puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
53
- puts "Failed Register record"
54
- puts "BLOCK: #{sym_block}"
55
- puts "MODEL: #{str_model}"
56
- puts "MESSAGE: #{e.message}"
57
- puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
58
- print "\e[0m"
59
- puts ""
60
-
61
- raise RegisterError.new
62
- end
63
- end
64
- end
65
-
66
- def insert_record sym_block, str_model, config_data, model_cache
67
- # col_arr: [:col1, :col2, :col3]
68
- col_arr = config_data[:col].keys
69
- # seed_arr: [[elem1, elem2, elem3...]]
70
- seed_arr = config_data[:col].map{|_, val| val }.transpose
71
- model_cache[str_model][:model].import(col_arr, seed_arr, validate: config_data[:validate], timestamps: false)
72
- end
73
-
74
- def save_model_cache model_cache, str_model
75
- return if model_cache[str_model].present?
76
- model = eval(str_model)
77
- model_cache[str_model] = {
78
- model: model,
79
- sym_model: str_model.to_sym,
80
- table_name: model.table_name
81
- }
82
- end
83
-
84
- def set_default_seed config_data
85
- block = ->(symbol){ :id == symbol }
86
- # each column type, key is symbolize column name
87
- config_data[:type].each do |key, _|
88
- # if it is id, skip
89
- next if block.call(key)
90
- # if there is data already, skip
91
- next if config_data[:col][key].present?
92
-
93
- config_data[:col][key] = Seeder.gen(config_data, key)
94
- end
95
- end
96
-
97
- def set_seed_arr model, sym_block, sym_model, config_data, maked, maked_col
98
- options = config_data[:option]
99
- convert_conf = config_data[:convert]
100
- loop_size = config_data[:loop]
101
-
102
- if apply_autoincrement?(config_data[:autoincrement])
103
- prev_id_arr =
104
- if maked_col[sym_model].present? && maked_col[sym_model].has_key?(:id)
105
- maked_col[sym_model][:id]
106
- else
107
- []
108
- end
109
-
110
- set_autoincrement(config_data, model, loop_size, prev_id_arr)
111
- end
112
-
113
- config_data[:col].each do |key, val|
114
- begin
115
- # set expand expression '<>' and ':' and so on...
116
- set_expand_expression(config_data, key, val, maked, maked_col)
117
- expanded_val = config_data[:col][key]
118
- expanded_val_size = expanded_val.size
119
-
120
- # apply converter
121
- if convert_conf.present?
122
- expanded_val = Converter.convert(expanded_val, convert_conf[key])
123
- end
124
-
125
- # get option configration
126
- option_conf = options.nil? ? nil : Option.gen(options[key])
127
-
128
- # Take count yourself, because .with_index is slow
129
- cnt = 0
130
- seeds =
131
- loop_size.times.map do
132
- seed =
133
- option_conf.nil? ? get_seed(expanded_val, expanded_val_size, cnt) : get_seed_with_option(expanded_val, expanded_val_size, option_conf, cnt)
134
- cnt += 1
135
-
136
- seed
137
- end
138
-
139
- update_maked_data(maked, sym_block, sym_model, key, seeds )
140
- update_maked_col(maked_col, sym_model, key, config_data[:col][key])
141
- config_data[:col][key] = seeds
142
- rescue => e
143
- print "\e[31m"
144
- puts "[Pokotarou ERROR]"
145
- puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
146
- puts "Failed Generating seed"
147
- puts "BLOCK: #{sym_block}"
148
- puts "MODEL: #{sym_model}"
149
- puts "COLUMN: #{key}"
150
- puts "MESSAGE: #{e.message}"
151
- puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
152
- print "\e[0m"
153
- puts ""
154
-
155
- raise SeedError.new
156
- end
157
- end
158
- end
159
-
160
- def update_let_key let, maked, maked_col
161
- let.each do |key, val|
162
- # TODO: run only maked_col or make key
163
- AdditionalVariables.let[key] = ExpressionParser.parse(val, maked, maked_col)
164
- end
165
- end
166
-
167
- def apply_autoincrement? autoincrement_flg
168
- # default true
169
- return true if autoincrement_flg.nil?
170
- autoincrement_flg
171
- end
172
-
173
- def set_autoincrement config_data, model, loop_size, prev_id_arr
174
- last_record = model.last
175
- current_id =
176
- if prev_id_arr.present?
177
- prev_id_arr.last
178
- elsif last_record.nil?
179
- 0
180
- else
181
- last_record.id
182
- end
183
-
184
- additions = current_id + loop_size
185
- next_id = current_id + 1
186
-
187
- config_data[:col][:id] = [*next_id..additions]
188
- end
189
-
190
- def set_expand_expression config_data, key, val, maked, maked_col
191
- # if it exists type, there is no need for doing 'expand expression'
192
- return if config_data[:type][key].present?
193
- config_data[:col][key] = SeedExpressionParser.parse(val, maked, maked_col)
194
- end
195
-
196
- def set_loop_expand_expression config_data, maked, maked_col
197
- config_data[:loop] =
198
- LoopExpressionParser.parse(config_data[:loop], maked, maked_col)
199
- end
200
-
201
- def get_seed arr, size, cnt
202
- ArrayOperation.get_rotated_val(arr, size, cnt)
203
- end
204
-
205
- def get_seed_with_option arr, size, option, cnt
206
- Option.apply(arr, size, option, cnt)
207
- end
208
-
209
- def update_maked_data maked, sym_block, sym_model, col, seed
210
- # maked: { key: Model, value: {key: col1, val: [col1_element, col1_element]} }
211
- maked[sym_block][sym_model][col] = seed
212
- end
213
-
214
- def update_maked_col maked_col, sym_model, column, vals
215
- maked_col[sym_model][column] ||= []
216
- maked_col[sym_model][column].concat(vals)
217
- end
218
-
219
- def output_log log
220
- return if log.nil?
221
- puts log
222
- end
223
-
224
- DUSH_OPTION = /^.*\'$/
225
- def is_dush? val
226
- return false unless val.kind_of?(String)
227
- DUSH_OPTION =~ val
228
- end
229
- end
230
- end
@@ -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