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/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:, file_path:, new_cols:)
  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:)
  scope_args = check_resolver_argument(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:)

          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:)
            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:, 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:, 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:, 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:, 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



11
12
13
14
15
16
17
18
19
# File 'lib/souls/cli/update/index.rb', line 11

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)
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:)
          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

#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:, action: "update")
  overwrite_class_file(mutation_argument:, file_path:, new_cols:)

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