Module: SOULs::Utils

Included in:
SOULs
Defined in:
lib/souls/utils/index.rb

Instance Method Summary collapse

Instance Method Details

#check_schema(class_name: "user") ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/souls/utils/index.rb', line 226

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_pathObject



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



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/souls/utils/index.rb', line 203

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/souls/utils/index.rb', line 126

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/souls/utils/index.rb', line 146

def get_columns_num_no_timestamp(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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/souls/utils/index.rb', line 169

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_pathObject



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



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/souls/utils/index.rb', line 94

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



192
193
194
195
196
197
198
199
200
201
# File 'lib/souls/utils/index.rb', line 192

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_pathObject



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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/souls/utils/index.rb', line 108

def get_relation_params(class_name: "user", col: "")
  Dir.chdir(SOULs.get_api_path.to_s) do
    cols =
      if col == "mutation"
        get_columns_num_no_timestamp(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_tablesObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/souls/utils/index.rb', line 61

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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/souls/utils/index.rb', line 48

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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/souls/utils/index.rb', line 35

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



31
32
33
# File 'lib/souls/utils/index.rb', line 31

def get_type_and_name(line)
  line.split(",")[0].gsub("\"", "").scan(/((?<=t\.).+(?=\s)) (.+)/)[0]
end

#table_check(line: "", class_name: "") ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/souls/utils/index.rb', line 72

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



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/souls/utils/index.rb', line 81

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