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
@@ -0,0 +1,15 @@
1
+ module Pokotarou
2
+ class NothingDataError < StandardError; end
3
+ module RegistrationConfigUpdater
4
+ module ArrayUtils
5
+ # return rotated val in passed array
6
+ def get_rotated_val array, size, cnt
7
+ raise NothingDataError.new("Nothing seed data") if array.nil?
8
+ raise NothingDataError.new("Seed data is empty") if size.zero?
9
+ x = (size + cnt) / size
10
+
11
+ array[ size + cnt - size * x ]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module Pokotarou
2
+ module RegistrationConfigUpdater
3
+ class ConvertConfig
4
+ class << self
5
+ PARENTHESES = /\(.*\)/
6
+ def execute arr, config
7
+ return arr if config.nil?
8
+ config.each do |e|
9
+ range = eval(e.slice(PARENTHESES).delete("()"))
10
+ convert_name = e.gsub(PARENTHESES, "")
11
+ arr[range] = get_val(convert_name, range.size)
12
+ end
13
+
14
+ arr
15
+ end
16
+
17
+ private
18
+ def get_val convert_name, size
19
+ return [""] * size if convert_name == "empty"
20
+ return [nil] * size if convert_name == "nil"
21
+ return ["text\n" * 5] * size if convert_name == "br_text"
22
+ return ["text" * 50] * size if convert_name == "big_text"
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ module Pokotarou
2
+ module RegistrationConfigUpdater
3
+ class DefaultValueMaker
4
+ class << self
5
+ def make config_data, key
6
+ n = config_data[:loop]
7
+ type = config_data[:type][key]
8
+ enum = config_data[:enum][key]
9
+
10
+ return enum if enum.present?
11
+ return make_array(n, ->(){ rand(100) }) if type == "integer"
12
+ return make_array(n, ->(){ rand(0.0..100.0) }) if type == "float"
13
+ return make_array(n, ->(){ rand(0.0..1_000_000_000.0) }) if type == "decimal"
14
+ return make_array(n, ->(){ SecureRandom.hex(20) }) if type == "string"
15
+ return make_array(n, ->(){ SecureRandom.hex(200) }) if ["text", "binary"].include?(type)
16
+ return make_array(n, ->(){ [true, false].sample }) if type == "boolean"
17
+ return make_datetime_array() if ["datetime", "date", "time"].include?(type)
18
+ end
19
+
20
+ private
21
+
22
+ def make_array n, proc
23
+ Array.new(n).map{ proc.call() }
24
+ end
25
+
26
+ def make_datetime_array
27
+ [Time.now.to_s(:db)]
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,170 @@
1
+ require "pokotarou/registration_config_updater/default_value_maker.rb"
2
+ require "pokotarou/registration_config_updater/option_config.rb"
3
+ require "pokotarou/registration_config_updater/convert_config.rb"
4
+ require "pokotarou/registration_config_updater/array_utils.rb"
5
+ require "pokotarou/parser/seed_data_expression_parser.rb"
6
+ require "pokotarou/parser/loop_expression_parser.rb"
7
+ require "pokotarou/parser/return_expression_parser..rb"
8
+
9
+ module Pokotarou
10
+ module RegistrationConfigUpdater
11
+ class Main
12
+ class << self
13
+ include ArrayUtils
14
+
15
+ def update model_content, block_name_sym, model_cache, maked, maked_col
16
+ model_name = model_content.first.to_s
17
+ model_config = model_content.second
18
+ update_model_cache(model_cache, model_name)
19
+
20
+
21
+ model = model_cache[model_name][:model]
22
+ model_name_sym = model_cache[model_name][:sym_model]
23
+ set_loop_expand_expression(model_config, maked, maked_col)
24
+ set_default_seed(model_config)
25
+ set_seed_arr(model, block_name_sym, model_name_sym, model_config, maked, maked_col)
26
+ end
27
+
28
+ private
29
+ def set_loop_expand_expression model_config, maked, maked_col
30
+ model_config[:loop] =
31
+ LoopExpressionParser.parse(model_config[:loop], maked, maked_col)
32
+ end
33
+
34
+ def set_default_seed model_config
35
+ # each column type, key is symbolize column name
36
+ model_config[:type].each do |key, _|
37
+ # if it is id, skip
38
+ next if :id == key
39
+ # if there is data already, skip
40
+ next if model_config[:col][key].present?
41
+
42
+ model_config[:col][key] = DefaultValueMaker.make(model_config, key)
43
+ end
44
+ end
45
+
46
+ def update_model_cache model_cache, model_name
47
+ return if model_cache[model_name].present?
48
+ model = eval(model_name)
49
+ model_cache[model_name] = {
50
+ model: model,
51
+ sym_model: model_name.to_sym,
52
+ table_name: model.table_name
53
+ }
54
+ end
55
+
56
+ def set_seed_arr model, sym_block, sym_model, config_data, maked, maked_col
57
+ options = config_data[:option]
58
+ convert_conf = config_data[:convert]
59
+ loop_size = config_data[:loop]
60
+
61
+ if has_autoincrement_config?(config_data[:autoincrement])
62
+ prev_id_arr =
63
+ if maked_col[sym_model].present? && maked_col[sym_model].has_key?(:id)
64
+ maked_col[sym_model][:id]
65
+ else
66
+ []
67
+ end
68
+ set_autoincrement(config_data, model, loop_size, prev_id_arr)
69
+ end
70
+
71
+ config_data[:col].each do |key, val|
72
+ begin
73
+ # set expand expression '<>' and ':' and so on...
74
+ set_expand_expression(config_data, key, val, maked, maked_col)
75
+ expanded_val = config_data[:col][key]
76
+ expanded_val_size = expanded_val.size
77
+
78
+ # apply converter
79
+ if convert_conf.present?
80
+ expanded_val =
81
+ ConvertConfig.execute(expanded_val, convert_conf[key])
82
+ end
83
+
84
+ # get option configration
85
+ option_conf = OptionConfig.fetch(options, key)
86
+
87
+ # Take count yourself, because .with_index is slow
88
+ cnt = 0
89
+ seeds =
90
+ loop_size.times.map do
91
+ seed =
92
+ option_conf.nil? ? get_rotated_val(expanded_val, expanded_val_size, cnt) : get_seed_with_option(expanded_val, expanded_val_size, option_conf, cnt)
93
+ cnt += 1
94
+
95
+ seed
96
+ end
97
+
98
+ update_maked_data(maked, sym_block, sym_model, key, seeds )
99
+ update_maked_col(maked_col, sym_model, key, config_data[:col][key])
100
+ config_data[:col][key] = seeds
101
+
102
+ rescue => e
103
+ print "\e[31m"
104
+ puts "[Pokotarou ERROR]"
105
+ puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
106
+ puts "Failed Generating seed"
107
+ puts "BLOCK: #{sym_block}"
108
+ puts "MODEL: #{sym_model}"
109
+ puts "COLUMN: #{key}"
110
+ puts "MESSAGE: #{e.message}"
111
+ puts "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
112
+ print "\e[0m"
113
+ puts ""
114
+
115
+ raise SeedError.new
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ def has_autoincrement_config? autoincrement_flg
122
+ # default true
123
+ return true if autoincrement_flg.nil?
124
+ autoincrement_flg
125
+ end
126
+
127
+ def set_autoincrement config_data, model, loop_size, prev_id_arr
128
+
129
+ current_id =
130
+ if prev_id_arr.present?
131
+ prev_id_arr.last
132
+ else
133
+ max_id = model.maximum("id")
134
+ max_id.nil? ? 0 : max_id
135
+ end
136
+
137
+ additions = current_id + loop_size
138
+ next_id = current_id + 1
139
+
140
+ ids = [*next_id..additions]
141
+
142
+ ids.shuffle! if config_data[:randomincrement]
143
+ config_data[:col][:id] = ids
144
+ end
145
+
146
+ def set_expand_expression config_data, key, val, maked, maked_col
147
+ # if it exists type, there is no need for doing 'expand expression'
148
+ return if config_data[:type][key].present?
149
+ config_data[:col][key] = SeedExpressionParser.parse(val, maked, maked_col)
150
+ end
151
+
152
+
153
+ def get_seed_with_option arr, size, option, cnt
154
+ OptionConfig.execute(arr, size, option, cnt)
155
+ end
156
+
157
+ def update_maked_data maked, sym_block, sym_model, col, seed
158
+ # maked: { key: Model, value: {key: col1, val: [col1_element, col1_element]} }
159
+ maked[sym_block][sym_model][col] = seed
160
+ end
161
+
162
+ def update_maked_col maked_col, sym_model, column, vals
163
+ maked_col[sym_model][column] ||= []
164
+ maked_col[sym_model][column].concat(vals)
165
+ end
166
+
167
+ end
168
+ end
169
+ end
170
+ end
@@ -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.1.7'
2
+ VERSION = '1.2.2'
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.1.7
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kashiwara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-20 00:00:00.000000000 Z
11
+ date: 2021-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -49,24 +49,33 @@ 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/pokotarou_handler.rb
66
- - 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/preset_option_setter.rb
69
+ - lib/pokotarou/registration_config_maker/template_option_setter.rb
70
+ - lib/pokotarou/registration_config_updater/array_utils.rb
71
+ - lib/pokotarou/registration_config_updater/convert_config.rb
72
+ - lib/pokotarou/registration_config_updater/default_value_maker.rb
73
+ - lib/pokotarou/registration_config_updater/main.rb
74
+ - lib/pokotarou/registration_config_updater/option_config.rb
75
+ - lib/pokotarou/seed_data_register/main.rb
67
76
  - lib/pokotarou/version.rb
68
77
  - lib/tasks/pokotarou_tasks.rake
69
- homepage:
78
+ homepage: https://github.com/Kashiwara0205/Pokotarou
70
79
  licenses:
71
80
  - MIT
72
81
  metadata: {}