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
@@ -0,0 +1,53 @@
1
+ require "pokotarou/registration_config_updater/array_utils.rb"
2
+ module Pokotarou
3
+ module RegistrationConfigUpdater
4
+ class OptionConfig
5
+ class << self
6
+
7
+ include ArrayUtils
8
+
9
+ def fetch option_hash, key
10
+ # shape option data
11
+ # [option1, option2] => { select: [option2], add: [option1] }
12
+ # [] => { select: [], add: [] }
13
+ return nil if option_hash.nil?
14
+ option = option_hash[key]
15
+ return option.nil? ? { select: [], add: [] } : separate(option)
16
+ end
17
+
18
+ def execute arr, size, option_conf, cnt = 0
19
+ selected_val = select(option_conf[:select], arr, size, cnt)
20
+
21
+ add(option_conf[:add], selected_val, cnt)
22
+ end
23
+
24
+ private
25
+
26
+ def separate option
27
+ # separate option to 'select' and 'add'
28
+ # { select = >[], add => [] }
29
+ {
30
+ select: option.find{|f| ["rotate", "random"].include?(f)},
31
+ add: option.find{|f| ["add_id", "sequence"].include?(f) }
32
+ }
33
+ end
34
+
35
+ def select option, arr, size, cnt
36
+ return arr.sample if option == "random"
37
+
38
+ # default return rotate
39
+ get_rotated_val(arr, size, cnt)
40
+ end
41
+
42
+ def add option, val, cnt
43
+ # if val is nil, return nil
44
+ return nil if val.nil?
45
+ # not use '<<' to avoid destructive effect
46
+ return "#{val}_#{cnt}" if option == "add_id" || option == "sequence"
47
+
48
+ val
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,81 @@
1
+ require "pokotarou/additional_variables/main.rb"
2
+ require "pokotarou/registration_config_updater/main.rb"
3
+
4
+ module Pokotarou
5
+ module SeedDataRegister
6
+ class Main
7
+ class RegisterError < StandardError; end
8
+ class SeedError < StandardError; end
9
+
10
+ class << self
11
+ def register data
12
+ # init maked to accumulate maded data
13
+ maked = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
14
+ maked_col = Hash.new { |h,k| h[k] = {} }
15
+ # init model_data to cache data of model
16
+ model_cache = {}
17
+ ActiveRecord::Base.transaction do
18
+ begin
19
+ data.each do |sym_block, model_data|
20
+ next if is_dush?(sym_block.to_s)
21
+ register_val_by_bulk(sym_block, model_data, maked, model_cache, maked_col)
22
+ end
23
+ rescue => e
24
+ raise StandardError.new("#{e.message}")
25
+ end
26
+ end
27
+
28
+ ReturnExpressionParser.parse(data[:"return'"], maked, maked_col)
29
+ end
30
+
31
+ private
32
+
33
+ def register_val_by_bulk block_name_sym, model_data, maked, model_cache, maked_col
34
+ model_data.each do |e|
35
+ begin
36
+ ::Pokotarou::RegistrationConfigUpdater::Main.update(e, block_name_sym, model_cache, maked, maked_col)
37
+ model_name = e.first.to_s
38
+ model_config = e.second
39
+ output_log(model_config[:log])
40
+ insert_record(block_name_sym, model_name, model_config ,model_cache)
41
+ rescue => e
42
+ print "\e[31m"
43
+ puts "[Pokotarou ERROR]"
44
+ puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
45
+ puts "Failed Register record"
46
+ puts "BLOCK: #{block_name_sym}"
47
+ puts "MODEL: #{model_name}"
48
+ puts "MESSAGE: #{e.message}"
49
+ puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
50
+ print "\e[0m"
51
+ puts ""
52
+
53
+ raise RegisterError.new
54
+ end
55
+ end
56
+ end
57
+
58
+ def insert_record sym_block, str_model, config_data, model_cache
59
+ # col_arr: [:col1, :col2, :col3]
60
+ col_arr = config_data[:col].keys
61
+ # seed_arr: [[elem1, elem2, elem3...]]
62
+ seed_arr = config_data[:col].map{|_, val| val }.transpose
63
+ validate = config_data[:validate].nil? ? false : config_data[:validate]
64
+
65
+ model_cache[str_model][:model].import(col_arr, seed_arr, validate: validate, timestamps: false)
66
+ end
67
+
68
+ def output_log log
69
+ return if log.nil?
70
+ puts log
71
+ end
72
+
73
+ DUSH_OPTION = /^.*\'$/
74
+ def is_dush? val
75
+ return false unless val.kind_of?(String)
76
+ DUSH_OPTION =~ val
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -1,3 +1,3 @@
1
1
  module Pokotarou
2
- VERSION = '1.2.0'
2
+ VERSION = '1.2.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pokotarou
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kashiwara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-13 00:00:00.000000000 Z
11
+ date: 2020-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -49,20 +49,29 @@ files:
49
49
  - README.md
50
50
  - Rakefile
51
51
  - lib/pokotarou.rb
52
- - lib/pokotarou/additional_methods.rb
53
- - lib/pokotarou/additional_variables/additional_variables.rb
54
- - lib/pokotarou/additional_variables/def_variable.rb
55
- - lib/pokotarou/arguments/arguments.rb
56
- - lib/pokotarou/arguments/def_args.rb
57
- - lib/pokotarou/array_operation.rb
58
- - lib/pokotarou/converter.rb
59
- - lib/pokotarou/data_register.rb
60
- - lib/pokotarou/data_structure.rb
61
- - lib/pokotarou/expression_parser.rb
62
- - lib/pokotarou/file_loader.rb
63
- - lib/pokotarou/option.rb
52
+ - lib/pokotarou/additional_arguments/main.rb
53
+ - lib/pokotarou/additional_methods/main.rb
54
+ - lib/pokotarou/additional_variables/main.rb
64
55
  - lib/pokotarou/p_tool.rb
65
- - lib/pokotarou/seeder.rb
56
+ - lib/pokotarou/parser/const_parser.rb
57
+ - lib/pokotarou/parser/loop_expression_parser.rb
58
+ - lib/pokotarou/parser/parser.rb
59
+ - lib/pokotarou/parser/parser_domain.rb
60
+ - lib/pokotarou/parser/return_expression_parser..rb
61
+ - lib/pokotarou/parser/seed_data_expression_parser.rb
62
+ - lib/pokotarou/registration_config_maker/column_domain.rb
63
+ - lib/pokotarou/registration_config_maker/config_domain.rb
64
+ - lib/pokotarou/registration_config_maker/grouping_option_setter.rb
65
+ - lib/pokotarou/registration_config_maker/import_option_setter.rb
66
+ - lib/pokotarou/registration_config_maker/main.rb
67
+ - lib/pokotarou/registration_config_maker/model_option_setter.rb
68
+ - lib/pokotarou/registration_config_maker/template_option_setter.rb
69
+ - lib/pokotarou/registration_config_updater/array_utils.rb
70
+ - lib/pokotarou/registration_config_updater/convert_config.rb
71
+ - lib/pokotarou/registration_config_updater/default_value_maker.rb
72
+ - lib/pokotarou/registration_config_updater/main.rb
73
+ - lib/pokotarou/registration_config_updater/option_config.rb
74
+ - lib/pokotarou/seed_data_register/main.rb
66
75
  - lib/pokotarou/version.rb
67
76
  - lib/tasks/pokotarou_tasks.rake
68
77
  homepage: https://github.com/Kashiwara0205/Pokotarou
@@ -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,233 +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
- max_id = model.maximum(:id)
175
-
176
- current_id =
177
- if prev_id_arr.present?
178
- prev_id_arr.last
179
- elsif max_id.nil?
180
- 0
181
- else
182
- max_id
183
- end
184
-
185
- additions = current_id + loop_size
186
- next_id = current_id + 1
187
-
188
- ids = [*next_id..additions]
189
- ids.shuffle! if config_data[:randomincrement]
190
- config_data[:col][:id] = ids
191
- end
192
-
193
- def set_expand_expression config_data, key, val, maked, maked_col
194
- # if it exists type, there is no need for doing 'expand expression'
195
- return if config_data[:type][key].present?
196
- config_data[:col][key] = SeedExpressionParser.parse(val, maked, maked_col)
197
- end
198
-
199
- def set_loop_expand_expression config_data, maked, maked_col
200
- config_data[:loop] =
201
- LoopExpressionParser.parse(config_data[:loop], maked, maked_col)
202
- end
203
-
204
- def get_seed arr, size, cnt
205
- ArrayOperation.get_rotated_val(arr, size, cnt)
206
- end
207
-
208
- def get_seed_with_option arr, size, option, cnt
209
- Option.apply(arr, size, option, cnt)
210
- end
211
-
212
- def update_maked_data maked, sym_block, sym_model, col, seed
213
- # maked: { key: Model, value: {key: col1, val: [col1_element, col1_element]} }
214
- maked[sym_block][sym_model][col] = seed
215
- end
216
-
217
- def update_maked_col maked_col, sym_model, column, vals
218
- maked_col[sym_model][column] ||= []
219
- maked_col[sym_model][column].concat(vals)
220
- end
221
-
222
- def output_log log
223
- return if log.nil?
224
- puts log
225
- end
226
-
227
- DUSH_OPTION = /^.*\'$/
228
- def is_dush? val
229
- return false unless val.kind_of?(String)
230
- DUSH_OPTION =~ val
231
- end
232
- end
233
- end