Module: SOULs::Utils
- Included in:
- SOULs
- Defined in:
- lib/souls/utils/index.rb
Instance Method Summary collapse
- #check_schema(class_name: "user") ⇒ Object
- #get_api_path ⇒ Object
- #get_col_name_and_type(class_name: "user", file_path: "db/migrate/20210816094410_add_column_to_users.rb", action: "add") ⇒ Object
- #get_columns_num(class_name: "user") ⇒ Object
- #get_columns_num_no_timestamp(class_name: "user") ⇒ Object
- #get_create_migration_type(class_name: "user") ⇒ Object
- #get_functions_path ⇒ Object
- #get_latest_version_txt(service_name: "api") ⇒ Object
- #get_migration_type(class_name: "user", action: "add") ⇒ Object
- #get_mother_path ⇒ Object
- #get_relation_params(class_name: "user", col: "") ⇒ Object
- #get_tables ⇒ Object
- #get_test_type(type) ⇒ Object
- #get_type(type) ⇒ Object
- #get_type_and_name(line) ⇒ Object
- #rbs_type_check(type) ⇒ Object
- #table_check(line: "", class_name: "") ⇒ Object
- #type_check(type) ⇒ Object
- #version_detector(current_ver: [0, 0, 1], update_kind: "patch") ⇒ Object
Instance Method Details
#check_schema(class_name: "user") ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/souls/utils/index.rb', line 239 def check_schema(class_name: "user") schema_data = get_columns_num(class_name:) create_migration_data = get_create_migration_type(class_name:) add_migration_data = get_migration_type(class_name:, action: "add") remove_migration_data = get_migration_type(class_name:, action: "remove") migration_data = create_migration_data + add_migration_data - remove_migration_data return "Already Up to date!" if schema_data.size == migration_data.size schema_data - migration_data end |
#get_api_path ⇒ Object
14 15 16 |
# File 'lib/souls/utils/index.rb', line 14 def get_api_path get_mother_path + "/apps/api" end |
#get_col_name_and_type(class_name: "user", file_path: "db/migrate/20210816094410_add_column_to_users.rb", action: "add") ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/souls/utils/index.rb', line 216 def get_col_name_and_type( class_name: "user", file_path: "db/migrate/20210816094410_add_column_to_users.rb", action: "add" ) pluralized_class_name = class_name.pluralize response = [] File.open(file_path) do |line| line.each_line do |file_line| next unless file_line.include?("#{action}_column") array = file_line.include?("array: true") types = file_line.split(",").map(&:strip) types.map { |n| n.gsub!(":", "") } types[0].gsub!("#{action}_column ", "") unless types[0].to_s == pluralized_class_name raise(StandardError, "Wrong class_name!Please Check your migration file!") end response << { column_name: types[1], type: types[2], array: } end end response end |
#get_columns_num(class_name: "user") ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/souls/utils/index.rb', line 139 def get_columns_num(class_name: "user") pluralized_class_name = class_name.pluralize file_path = "./db/schema.rb" class_check_flag = false cols = [] File.open(file_path, "r") do |f| f.each_line.with_index do |line, _i| class_check_flag = true if line.include?("create_table") && line.include?(pluralized_class_name) if class_check_flag == true && !line.include?("create_table") return cols if line.include?("t.index") || line.strip == "end" types = SOULs.get_type_and_name(line) array = line.include?("array: true") cols << { column_name: types[1], type: types[0], array: } end end end cols end |
#get_columns_num_no_timestamp(class_name: "user") ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/souls/utils/index.rb', line 159 def (class_name: "user") pluralized_class_name = class_name.pluralize file_path = "./db/schema.rb" class_check_flag = false cols = [] File.open(file_path, "r") do |f| f.each_line.with_index do |line, _i| class_check_flag = true if line.include?("create_table") && line.include?(pluralized_class_name) if class_check_flag == true && !line.include?("create_table") return cols if line.include?("t.index") || line.strip == "end" types = SOULs.get_type_and_name(line) array = line.include?("array: true") cols << { column_name: types[1], type: types[0], array: } unless %w[ created_at updated_at ].include?(types[1]) end end end cols end |
#get_create_migration_type(class_name: "user") ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/souls/utils/index.rb', line 182 def get_create_migration_type(class_name: "user") pluralized_class_name = class_name.pluralize file_path = Dir["db/migrate/*_create_#{pluralized_class_name}.rb"][0] class_check_flag = false response = [ { column_name: "created_at", type: "datetime", array: false }, { column_name: "updated_at", type: "datetime", array: false } ] File.open(file_path) do |f| f.each_line do |line| class_check_flag = true if line.include?("create_table") next unless class_check_flag == true && !line.include?("create_table") return response if line.include?("t.timestamps") || line.strip == "end" types = SOULs.get_type_and_name(line) types.map { |n| n.gsub!(":", "") } array = line.include?("array: true") response << { column_name: types[1], type: types[0], array: } end end end |
#get_functions_path ⇒ Object
10 11 12 |
# File 'lib/souls/utils/index.rb', line 10 def get_functions_path get_mother_path + "/apps/functions" end |
#get_latest_version_txt(service_name: "api") ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/souls/utils/index.rb', line 107 def get_latest_version_txt(service_name: "api") case service_name when "gem" return SOULs::VERSION.split(".").map(&:to_i) when "api", "worker", "console", "admin", "media" file_path = "#{Gem.dir}/gems/souls-#{SOULs::VERSION}/lib/souls/versions/.souls_#{service_name}_version" else raise(StandardError, "You are at wrong directory!") end File.open(file_path, "r") do |f| f.readlines[0].strip.split(".").map(&:to_i) end end |
#get_migration_type(class_name: "user", action: "add") ⇒ Object
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/souls/utils/index.rb', line 205 def get_migration_type(class_name: "user", action: "add") pluralized_class_name = class_name.pluralize file_paths = Dir["db/migrate/*_#{action}_column_to_#{pluralized_class_name}.rb"] new_columns = file_paths.map do |file_path| get_col_name_and_type(class_name:, file_path:, action:) end new_columns.flatten end |
#get_mother_path ⇒ Object
3 4 5 6 7 8 |
# File 'lib/souls/utils/index.rb', line 3 def get_mother_path current_dir = Dir.pwd file_array = current_dir.split("/") mother_dir_num = file_array.rindex("apps") mother_dir_num ? file_array.each_slice(mother_dir_num).to_a[0].join("/") : current_dir end |
#get_relation_params(class_name: "user", col: "") ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/souls/utils/index.rb', line 121 def get_relation_params(class_name: "user", col: "") Dir.chdir(SOULs.get_api_path.to_s) do cols = if col == "mutation" (class_name:) else get_columns_num(class_name:) end relation_params = cols.select { |n| n[:column_name].match?(/_id$/) } user_check = relation_params.map do |param| param[:column_name] == "user_id" end user_exist = user_check.include?(true) return { user_exist:, params: cols, relation_params: } end end |
#get_tables ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/souls/utils/index.rb', line 74 def get_tables path = "./db/schema.rb" tables = [] File.open(path, "r") do |f| f.each_line.with_index do |line, _i| tables << line.split("\"")[1] if line.include?("create_table") end end tables end |
#get_test_type(type) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/souls/utils/index.rb', line 61 def get_test_type(type) { bigint: "rand(1..10)", float: 4.2, string: '"MyString"', text: '"MyString"', datetime: "Time.now", date: "Time.now.strftime('%F')", boolean: false, integer: "rand(1..10)" }[type.to_sym] end |
#get_type(type) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/souls/utils/index.rb', line 48 def get_type(type) { bigint: "Integer", string: "String", float: "Float", text: "String", datetime: "GraphQL::Types::ISO8601DateTime", date: "GraphQL::Types::ISO8601DateTime", boolean: "Boolean", integer: "Integer" }[type.to_sym] end |
#get_type_and_name(line) ⇒ Object
44 45 46 |
# File 'lib/souls/utils/index.rb', line 44 def get_type_and_name(line) line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0] end |
#rbs_type_check(type) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/souls/utils/index.rb', line 31 def rbs_type_check(type) { bigint: "Integer", string: "String", float: "Float", text: "String", datetime: "String", date: "String", boolean: "bool", integer: "Integer" }[type.to_sym] end |
#table_check(line: "", class_name: "") ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/souls/utils/index.rb', line 85 def table_check(line: "", class_name: "") if line.include?("create_table") && (line.split[1].gsub("\"", "").gsub(",", "") == class_name.pluralize.to_s) return true end false end |
#type_check(type) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/souls/utils/index.rb', line 18 def type_check(type) { bigint: "Integer", string: "String", float: "Float", text: "String", datetime: "String", date: "String", boolean: "Boolean", integer: "Integer" }[type.to_sym] end |
#version_detector(current_ver: [0, 0, 1], update_kind: "patch") ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/souls/utils/index.rb', line 94 def version_detector(current_ver: [0, 0, 1], update_kind: "patch") case update_kind when "patch" "#{current_ver[0]}.#{current_ver[1]}.#{current_ver[2] + 1}" when "minor" "#{current_ver[0]}.#{current_ver[1] + 1}.0" when "major" "#{current_ver[0] + 1}.0.0" else raise(StandardError, "Wrong version!") end end |