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,18 @@
1
+ require "pokotarou/parser/parser.rb"
2
+
3
+ # for seed data
4
+ module Pokotarou
5
+ class SeedExpressionParser < ExpressionParser
6
+ class << self
7
+ private
8
+ def nothing_apply_process val
9
+ # for escape \\
10
+ val.instance_of?(String) ? [val.tr("\\","")] : [val]
11
+ end
12
+
13
+ def output_error e
14
+ raise ParseError.new("Failed Seed Expression parse:#{e.message}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Pokotarou
2
+ module RegistrationConfigMaker
3
+ class ColumnDomain
4
+ class << self
5
+ def is_foreign_key? col_name_sym, foreign_key_dict
6
+ return foreign_key_dict[col_name_sym].present?
7
+ end
8
+
9
+ ENUM_REGEX = /^enum(\s*.)*$/
10
+ def is_enum? sql_type
11
+ return false unless sql_type.kind_of?(String)
12
+ return ENUM_REGEX =~ sql_type
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ module Pokotarou
2
+ module RegistrationConfigMaker
3
+ class ConfigDomain
4
+ class << self
5
+ def has_dush_import_syntax? all_content
6
+ return false if all_content.blank?
7
+ return all_content.has_key?(:"import'")
8
+ end
9
+
10
+ def has_dush_template_syntax? all_content
11
+ return false if all_content.blank?
12
+ return all_content.has_key?(:"template'")
13
+ end
14
+
15
+ def has_dush_template_path_syntax? all_content
16
+ return false if all_content.blank?
17
+ return all_content.has_key?(:"template_path'")
18
+ end
19
+
20
+ def has_grouping_syntax? model_content
21
+ return false if model_content.blank?
22
+ return model_content.has_key?(:grouping)
23
+ end
24
+
25
+ def has_template_syntax? model_content
26
+ return false if model_content.blank?
27
+ return model_content.has_key?(:template)
28
+ end
29
+
30
+ def has_seed_data_syntax? col_config, col_name_sym
31
+ return false if col_config.blank?
32
+ return col_config.has_key?(col_name_sym)
33
+ end
34
+
35
+ DUSH_OPTION_REGEX = /^.*\'$/
36
+ def is_dush_syntax? syntax
37
+ return false unless syntax.kind_of?(String)
38
+ return DUSH_OPTION_REGEX =~ syntax
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,28 @@
1
+ module Pokotarou
2
+ module RegistrationConfigMaker
3
+ class GroupingOptionSetter
4
+ class << self
5
+ def set seed_config
6
+ seed_config[:grouping].each do |grouping_key, cols|
7
+ set_grouping_data(:col, seed_config, grouping_key, cols)
8
+ set_grouping_data(:option, seed_config, grouping_key, cols)
9
+ set_grouping_data(:convert, seed_config, grouping_key, cols)
10
+ end
11
+
12
+ seed_config.delete(:grouping)
13
+ end
14
+
15
+ private
16
+ def set_grouping_data config_name, seed_config, grouping_key, cols
17
+ return if seed_config[config_name].blank?
18
+ return unless seed_config[config_name].has_key?(grouping_key)
19
+ cols.each do |e|
20
+ seed_config[config_name][e.to_sym] = seed_config[config_name][grouping_key]
21
+ end
22
+
23
+ seed_config[config_name].delete(grouping_key)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ require "pokotarou/additional_methods/main"
2
+ module Pokotarou
3
+ module RegistrationConfigMaker
4
+ class ImportOptionSetter
5
+ class << self
6
+ def set all_content
7
+ ::Pokotarou::AdditionalMethods::Main.import_from_yml(all_content[:"import'"])
8
+ all_content.delete(:"import'")
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,58 @@
1
+ require "pokotarou/registration_config_maker/column_domain.rb"
2
+ require "pokotarou/registration_config_maker/config_domain.rb"
3
+ require "pokotarou/registration_config_maker/import_option_setter.rb"
4
+ require "pokotarou/registration_config_maker/template_option_setter.rb"
5
+ require "pokotarou/registration_config_maker/grouping_option_setter.rb"
6
+ require "pokotarou/registration_config_maker/model_option_setter.rb"
7
+
8
+ module Pokotarou
9
+ module RegistrationConfigMaker
10
+ class Main
11
+ class << self
12
+ def gen all_content
13
+ set_header_config(all_content)
14
+ all_content.reduce({}) do |acc, block_content|
15
+ block_name = block_content.first
16
+ block_config = block_content.second
17
+
18
+ if ConfigDomain.is_dush_syntax?(block_name.to_s)
19
+ acc[block_name] = block_config
20
+ else
21
+ set_model_config_to_acc(acc, block_content)
22
+ end
23
+
24
+ acc
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def set_header_config all_content
31
+ set_template_option(all_content)
32
+ set_import_option(all_content)
33
+ end
34
+
35
+ def set_import_option all_content
36
+ return unless ConfigDomain.has_dush_import_syntax?(all_content)
37
+ ImportOptionSetter.set(all_content)
38
+ end
39
+
40
+ def set_template_option all_content
41
+ return if !ConfigDomain.has_dush_template_syntax?(all_content) && !ConfigDomain.has_dush_template_path_syntax?(all_content)
42
+ TemplateOptionSetter.set(all_content)
43
+ end
44
+
45
+ def set_model_config_to_acc acc, block_content
46
+ set_grouping_option(block_content.second)
47
+ ModelOptionSetter.set(acc, block_content)
48
+ end
49
+
50
+ def set_grouping_option block_content
51
+ block_content.each do |_, model_content|
52
+ GroupingOptionSetter.set(model_content) if ConfigDomain.has_grouping_syntax?(model_content)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,65 @@
1
+ module Pokotarou
2
+ module RegistrationConfigMaker
3
+ class ModelOptionSetter
4
+ class << self
5
+ def set acc, block_content
6
+ block_name = block_content.first
7
+ block_config = block_content.second
8
+ acc[block_name] = {}
9
+
10
+ block_config.each do |model_content|
11
+ model_name = model_content.first
12
+ model_config = model_content.second
13
+ set_column_type(model_config, model_name)
14
+ acc[block_name][model_name] = model_config
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def set_column_type model_config, model_name
21
+ model = eval(model_name.to_s)
22
+ foreign_key_data = get_foreign_key_data(model)
23
+
24
+ model_config[:col] ||= {}
25
+ model.columns.each do |e|
26
+ symbol_col_name = e.name.to_sym
27
+
28
+ next if ConfigDomain.has_seed_data_syntax?(model_config[:col], symbol_col_name)
29
+
30
+ # set foreign_key info
31
+ if ColumnDomain.is_foreign_key?(symbol_col_name, foreign_key_data)
32
+ # don't set :type val for don't run default seeder
33
+ # use F function for set foreign key
34
+ model_config[:col][symbol_col_name] = "F|#{foreign_key_data[symbol_col_name].to_s}"
35
+ else
36
+ model_config[:type] ||= {}
37
+ model_config[:type][symbol_col_name] = e.type.to_s
38
+ end
39
+
40
+ # set enum info
41
+ model_config[:enum] ||= {}
42
+ if ColumnDomain.is_enum?(e.sql_type.to_s)
43
+ model_config[:enum][symbol_col_name] = e.sql_type.to_s[5..-2].tr("'", "").split(",")
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ def get_foreign_key_data model
50
+ relations = model.reflect_on_all_associations(:belongs_to)
51
+ return {} if relations.empty?
52
+ relations.reduce({})do |acc, r|
53
+
54
+ relation_model = r.name.to_s.camelize
55
+ if Object.const_defined?(relation_model.to_sym)
56
+ acc[r.foreign_key.to_sym] = eval(relation_model)
57
+ end
58
+
59
+ acc
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,63 @@
1
+ require "pokotarou/registration_config_maker/config_domain.rb"
2
+ module Pokotarou
3
+ module RegistrationConfigMaker
4
+ class TemplateOptionSetter
5
+ class << self
6
+ def set all_content
7
+ templates = fetch_template_option(all_content)
8
+
9
+ all_content.each do |key, block_content|
10
+ next if ConfigDomain.is_dush_syntax?(key.to_s)
11
+ set_template_option(block_content, templates)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def fetch_template_option all_content
18
+ templates = ConfigDomain.has_dush_template_syntax?(all_content) ? all_content.delete(:"template'") : {}
19
+
20
+ if ConfigDomain.has_dush_template_path_syntax?(all_content)
21
+ merge_template_from_template_path(all_content, templates)
22
+ end
23
+
24
+ return templates
25
+ end
26
+
27
+ def merge_template_from_template_path all_content, templates
28
+ template_path = all_content.delete(:"template_path'")
29
+ template_path.each do |e|
30
+ YAML.load_file(e).deep_symbolize_keys!.each do |loaded_template|
31
+ templates[loaded_template.first] = loaded_template.second
32
+ end
33
+ end
34
+ end
35
+
36
+ def set_template_option block_content, templates
37
+ block_content.each do |key, model_content|
38
+ next unless ConfigDomain.has_template_syntax?(model_content)
39
+ template_copy = get_template_copy(model_content[:template], templates)
40
+ deep_overwrite(model_content, template_copy)
41
+ # update config all_config
42
+ block_content[key] = template_copy
43
+ end
44
+ end
45
+
46
+ def get_template_copy template_name, templates
47
+ return templates[template_name.to_sym].deep_dup
48
+ end
49
+
50
+ def deep_overwrite source, destination
51
+ source.each do |key, val|
52
+ if val.kind_of?(Hash)
53
+ destination[key] ||= {}
54
+ deep_overwrite(val, destination[key])
55
+ else
56
+ destination[key] = val
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -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