Method: Souls::Update#create_mutation_rbs

Defined in:
lib/souls/cli/update/mutation_rbs.rb

#create_mutation_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/souls/cli/update/mutation_rbs.rb', line 4

def create_mutation_rbs(class_name)
  singularized_class_name = class_name.singularize.underscore
  new_cols = ""
  Dir.chdir(Souls.get_api_path.to_s) do
    new_cols = Souls.get_columns_num(class_name: singularized_class_name)
  end
  dir_name = "./sig/api/app/graphql/mutations/base/#{singularized_class_name}"
  file_path = "#{dir_name}/create_#{singularized_class_name}.rbs"
  argument = false
  resolve = false
  write_txt = ""
  File.open(file_path, "r") do |f|
    f.each_line do |line|
      next if line.include?("| (:") && argument

      if line.include?("{ :node => String } } | ::GraphQL::ExecutionError )")
        write_txt += line
        resolve = false
      elsif resolve
        next
      elsif line.include?("def resolve:") && !resolve
        new_cols.each_with_index do |col, i|
          type = Souls.type_check(col[:type])
          type = "[#{type}]" if col[:array]
          next if col[:column_name] == "created_at" || col[:column_name] == "updated_at"

          write_txt +=
            if i.zero?
              line
            else
              "                          #{col[:column_name]}: #{type}?,\n"
            end
        end
        resolve = true
      elsif line.include?("def self.argument:") && !argument
        new_cols.each_with_index do |col, i|
          type = Souls.type_check(col[:type])
          type = "[#{type}]" if col[:array]
          next if col[:column_name] == "created_at" || col[:column_name] == "updated_at"

          if i.zero?
            write_txt +=
              "        def self.argument: (:#{col[:column_name]}, #{type}, required: false ) -> #{type}\n"
          else
            write_txt +=
              "                         | (:#{col[:column_name]}, #{type}, required: false ) -> #{type}\n"
          end
        end
        argument = true
      else
        write_txt += line
      end
    end
  end
  File.open(file_path, "w") { |f| f.write(write_txt) }
  Souls::Painter.update_file(file_path.to_s)
end