Class: SOULs::Update

Inherits:
Thor
  • Object
show all
Defined in:
lib/souls/cli/update/type.rb,
lib/souls/cli/update/index.rb,
lib/souls/cli/update/mutation.rb,
lib/souls/cli/update/resolver.rb,
lib/souls/cli/update/type_rbs.rb,
lib/souls/cli/update/mutation_rbs.rb,
lib/souls/cli/update/rspec_factory.rb,
lib/souls/cli/update/rspec_mutation.rb,
lib/souls/cli/update/rspec_resolver.rb

Instance Method Summary collapse

Instance Method Details

#create_mutation(class_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/souls/cli/update/mutation.rb', line 6

def create_mutation(class_name)
  singularized_class_name = class_name.singularize.underscore
  new_cols = SOULs.get_columns_num(class_name: singularized_class_name)
  dir_name = "./app/graphql/mutations/base/#{singularized_class_name}"
  file_path = "#{dir_name}/create_#{singularized_class_name}.rb"
  unless File.exist?(file_path)
    SOULs::Painter.error("File #{file_path} is missing. Please recreate it and then run this command again.")
    return
  end

  mutation_argument = check_mutation_argument(class_name: "user", action: "create")
  overwrite_class_file(mutation_argument: mutation_argument, file_path: file_path, new_cols: new_cols)
  SOULs::Painter.update_file(file_path.to_s)
end

#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

#resolver(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
# File 'lib/souls/cli/update/resolver.rb', line 4

def resolver(class_name)
  singularized_class_name = class_name.singularize.underscore
  new_cols = SOULs.get_columns_num(class_name: singularized_class_name)
  dir_name = "./app/graphql/resolvers"
  new_file_path = "tmp/update_resolver.rb"
  file_path = "#{dir_name}/#{singularized_class_name}_search.rb"
  args = check_resolver_argument(class_name: class_name)
  scope_args = check_resolver_argument(class_name: class_name, action: "scope")
  argument = false
  scope = false
  File.open(file_path) do |f|
    File.open(new_file_path, "w") do |new_line|
      f.each_line do |line|
        new_line.write(line)
        if line.include?("argument") && !argument
          new_cols.each do |col|
            type = SOULs.type_check(col[:type])
            type = "[#{type}]" if col[:array]
            add_line = "      argument :#{col[:column_name]}, #{type}, required: false\n"
            new_line.write(add_line) unless args.include?(col[:column_name])
          end
          argument = true
        elsif line.include?("scope = ::") && !scope
          new_cols.each do |col|
            type = SOULs.type_check(col[:type])
            type = "[#{type}]" if col[:array]

            add_line =
              if type.include?("[")
                "      scope = scope.where('#{col[:column_name]} @> ARRAY[?]::#{col[:type]}[]', " \
                "value[:#{col[:column_name]}]) if value[:#{col[:column_name]}]\n"
              else
                "      scope = scope.where(#{col[:column_name]}: value[:#{col[:column_name]}]) if " \
                "value[:#{col[:column_name]}]\n"
              end
            new_line.write(add_line) unless scope_args.include?(col[:column_name])
          end
          scope = true
        end
      end
    end
  end
  FileUtils.rm(file_path)
  FileUtils.mv(new_file_path, file_path)
  SOULs::Painter.update_file(file_path.to_s)
end

#rspec_factory(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
# File 'lib/souls/cli/update/rspec_factory.rb', line 4

def rspec_factory(class_name)
  singularized_class_name = class_name.singularize.underscore
  pluralized_class_name = class_name.pluralize.underscore
  new_cols = SOULs.get_columns_num(class_name: singularized_class_name)
  dir_name = "./spec/factories"
  new_file_path = "tmp/create_factory.rb"
  file_path = "#{dir_name}/#{pluralized_class_name}.rb"
  argument = false
  File.open(file_path) do |f|
    File.open(new_file_path, "w") do |new_line|
      f.each_line do |line|
        new_line.write(line)
        next unless line.include?("{") && !argument

        new_cols.each do |col|
          next if col[:column_name] == "created_at" || col[:column_name] == "updated_at"

          type = SOULs.get_test_type(col[:type])
          type = "[#{type}]" if col[:array]
          args = check_factory_argument(class_name: class_name)

          new_line.write("    #{col[:column_name]} { #{type} }\n") unless args.include?(col[:column_name])
        end
        argument = true
      end
    end
  end
  FileUtils.rm(file_path)
  FileUtils.mv(new_file_path, file_path)
  SOULs::Painter.update_file(file_path.to_s)
end

#rspec_mutation(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
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/souls/cli/update/rspec_mutation.rb', line 4

def rspec_mutation(class_name)
  singularized_class_name = class_name.singularize.underscore
  new_cols = SOULs.get_columns_num(class_name: singularized_class_name)
  dir_name = "./spec/mutations/base"
  new_file_path = "tmp/rspec_mutation.rb"
  file_path = "#{dir_name}/#{singularized_class_name}_spec.rb"
  argument = false
  node_res = false
  test_res = false
  File.open(file_path) do |f|
    File.open(new_file_path, "w") do |new_line|
      f.each_line do |line|
        new_line.write(line)
        node_res = true if line.include?("node {")
        test_res = true if line.include?("include(")
        node_res = false if node_res && line.include?("}")
        test_res = false if test_res && line.strip == ")"

        if line.include?('#{') && !argument
          new_cols.each do |col|
            type = SOULs.type_check(col[:type])
            next if col[:column_name] == "created_at" || col[:column_name] == "updated_at"

            type_line =
              if type == "String" && !col[:array]
                "          #{col[:column_name].camelize(:lower)}" \
                ": \"\#{#{class_name}[:#{col[:column_name].underscore}]}\"\n"
              else
                "          #{col[:column_name].camelize(:lower)}" \
                ": \#{#{class_name}[:#{col[:column_name].underscore}]}\n"
              end
            args = check_rspec_mutation_argument(class_name: class_name)
            new_line.write(type_line) unless args.include?(col[:column_name].underscore)
          end
          argument = true
        elsif node_res && !line.include?("{")
          node_args = check_rspec_mutation_argument(class_name: class_name, action: "node_args")
          new_cols.each do |col|
            unless node_args.include?(col[:column_name])
              new_line.write("              #{col[:column_name].camelize(:lower)}\n")
            end
          end
          node_res = false
        elsif test_res && line.include?("=> be_")
          test_args = check_rspec_mutation_argument(class_name: class_name, action: "test_args")
          new_cols.each do |col|
            type = SOULs.type_check(col[:type])
            text =
              case type
              when "Integer", "Float"
                col[:array] ? "be_all(Integer)" : "be_a(Integer)"
              when "Boolean"
                col[:array] ? "be_all([true, false])" : "be_in([true, false])"
              else
                col[:array] ? "be_all(String)" : "be_a(String)"
              end
            unless test_args.include?(col[:column_name])
              new_line.write("        \"#{col[:column_name].camelize(:lower)}\" => #{text},\n")
            end
          end
          test_res = false
        end
      end
    end
  end
  FileUtils.rm(file_path)
  FileUtils.mv(new_file_path, file_path)
  SOULs::Painter.update_file(file_path.to_s)
end

#rspec_resolver(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
# File 'lib/souls/cli/update/rspec_resolver.rb', line 4

def rspec_resolver(class_name)
  singularized_class_name = class_name.singularize.underscore
  new_cols = SOULs.get_columns_num(class_name: singularized_class_name)
  dir_name = "./spec/resolvers"
  new_file_path = "tmp/rspec_resolver.rb"
  file_path = "#{dir_name}/#{singularized_class_name}_search_spec.rb"
  node_res = false
  test_res = false
  File.open(file_path) do |f|
    File.open(new_file_path, "w") do |new_line|
      f.each_line do |line|
        new_line.write(line)
        node_res = true if line.include?("node {")
        test_res = true if line.include?("include(")
        node_res = false if node_res && line.include?("}")
        test_res = false if test_res && line.strip == ")"

        if node_res && !line.include?("{")
          node_args = check_rspec_resolver_argument(class_name: class_name, action: "node_args")
          new_cols.each do |col|
            unless node_args.include?(col[:column_name])
              new_line.write("              #{col[:column_name].camelize(:lower)}\n")
            end
          end
          node_res = false
        elsif test_res && line.include?("=> be_")
          test_args = check_rspec_resolver_argument(class_name: class_name, action: "test_args")
          new_cols.each do |col|
            type = SOULs.type_check(col[:type])
            text =
              case type
              when "Integer", "Float"
                col[:array] ? "be_all(Integer)" : "be_a(Integer)"
              when "Boolean"
                col[:array] ? "be_all([true, false])" : "be_in([true, false])"
              else
                col[:array] ? "be_all(String)" : "be_a(String)"
              end
            unless test_args.include?(col[:column_name])
              new_line.write("          \"#{col[:column_name].camelize(:lower)}\" => #{text},\n")
            end
          end
          test_res = false
        end
      end
    end
  end
  FileUtils.rm(file_path)
  FileUtils.mv(new_file_path, file_path)
  SOULs::Painter.update_file(file_path.to_s)
end

#scaffold(class_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/souls/cli/update/index.rb', line 13

def scaffold(class_name)
  create_mutation(class_name)
  update_mutation(class_name)
  resolver(class_name)
  type(class_name)
  rspec_factory(class_name)
  rspec_mutation(class_name)
  rspec_resolver(class_name)
  Dir.chdir(SOULs.get_mother_path.to_s) do
    create_mutation_rbs(class_name)
    update_mutation_rbs(class_name)
    type_rbs(class_name)
  end
end

#type(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
# File 'lib/souls/cli/update/type.rb', line 4

def type(class_name)
  singularized_class_name = class_name.singularize.underscore
  new_cols = SOULs.get_columns_num(class_name: singularized_class_name)
  dir_name = "./app/graphql/types"
  new_file_path = "tmp/create_type.rb"
  file_path = "#{dir_name}/#{singularized_class_name}_type.rb"
  argument = false
  File.open(file_path) do |f|
    File.open(new_file_path, "w") do |new_line|
      f.each_line do |line|
        new_line.write(line)
        next unless line.include?("field") && !argument

        new_cols.each do |col|
          type = SOULs.get_type(col[:type])
          type = "[#{type}]" if col[:array]
          args = check_type_argument(class_name: class_name)
          unless args.include?(col[:column_name])
            new_line.write("    field :#{col[:column_name]}, #{type}, null: true\n")
          end
        end
        argument = true
      end
    end
  end
  FileUtils.rm(file_path)
  FileUtils.mv(new_file_path, file_path)
  SOULs::Painter.update_file(file_path.to_s)
end

#type_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
# File 'lib/souls/cli/update/type_rbs.rb', line 4

def type_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/types"
  new_file_path = "config/create_type.rbs"
  file_path = "#{dir_name}/#{singularized_class_name}_type.rbs"
  argument = false
  File.open(file_path) do |f|
    File.open(new_file_path, "w") do |new_line|
      f.each_line do |line|
        next if line.include?("| (:") && argument

        if line.include?("    def self.edge_type:")
          new_line.write(line)
          argument = false
        elsif line.include?("def self.field:") && !argument
          new_cols.each_with_index do |col, i|
            type = SOULs.get_type(col[:type])
            type = "[#{type}]" if col[:array]
            if i.zero?
              new_line.write("    def self.field: (:#{col[:column_name]}, #{type}, null: true) -> #{type}\n")
            else
              new_line.write("                  | (:#{col[:column_name]}, #{type}, null: true) -> #{type}\n")
            end
          end
          argument = true
        else
          new_line.write(line)
        end
      end
    end
  end
  FileUtils.rm(file_path)
  FileUtils.mv(new_file_path, file_path)
  SOULs::Painter.update_file(file_path.to_s)
end

#update_mutation(class_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/souls/cli/update/mutation.rb', line 22

def update_mutation(class_name)
  singularized_class_name = class_name.singularize.underscore
  new_cols = SOULs.get_columns_num(class_name: singularized_class_name)
  dir_name = "./app/graphql/mutations/base/#{singularized_class_name}"
  file_path = "#{dir_name}/update_#{singularized_class_name}.rb"
  unless File.exist?(file_path)
    SOULs::Painter.error("File #{file_path} is missing. Please recreate it and then run this command again.")
    return
  end

  mutation_argument = check_mutation_argument(class_name: class_name, action: "update")
  overwrite_class_file(mutation_argument: mutation_argument, file_path: file_path, new_cols: new_cols)

  SOULs::Painter.update_file(file_path.to_s)
end

#update_mutation_rbs(class_name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/souls/cli/update/mutation_rbs.rb', line 63

def update_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}"
  new_file_path = "config/update_mutation.rbs"
  file_path = "#{dir_name}/update_#{singularized_class_name}.rbs"
  argument = false
  resolve = false
  File.open(file_path) do |f|
    File.open(new_file_path, "w") do |new_line|
      f.each_line do |line|
        next if line.include?("| (:") && argument

        if line.include?("{ :node => String } } | ::GraphQL::ExecutionError )")
          new_line.write(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"

            if i.zero?
              new_line.write(line)
              new_line.write("                          id: String,\n")
            else
              new_line.write("                          #{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?
              new_line.write(
                "        def self.argument: (:#{col[:column_name]}, #{type}, required: false ) -> #{type}\n"
              )
            else
              new_line.write(
                "                         | (:#{col[:column_name]}, #{type}, required: false ) -> #{type}\n"
              )
            end
          end
          argument = true
        else
          new_line.write(line)
        end
      end
    end
  end
  FileUtils.rm(file_path)
  FileUtils.mv(new_file_path, file_path)
  SOULs::Painter.update_file(file_path.to_s)
end