Class: SOULs::Generate

Inherits:
Thor
  • Object
show all
Defined in:
lib/souls/cli/generate/job.rb,
lib/souls/cli/generate/edge.rb,
lib/souls/cli/generate/type.rb,
lib/souls/cli/generate/index.rb,
lib/souls/cli/generate/query.rb,
lib/souls/cli/generate/job_rbs.rb,
lib/souls/cli/generate/manager.rb,
lib/souls/cli/generate/edge_rbs.rb,
lib/souls/cli/generate/mutation.rb,
lib/souls/cli/generate/resolver.rb,
lib/souls/cli/generate/type_rbs.rb,
lib/souls/cli/generate/query_rbs.rb,
lib/souls/cli/generate/rspec_job.rb,
lib/souls/cli/generate/connection.rb,
lib/souls/cli/generate/application.rb,
lib/souls/cli/generate/manager_rbs.rb,
lib/souls/cli/generate/rspec_query.rb,
lib/souls/cli/generate/mutation_rbs.rb,
lib/souls/cli/generate/resolver_rbs.rb,
lib/souls/cli/generate/rspec_factory.rb,
lib/souls/cli/generate/rspec_manager.rb,
lib/souls/cli/generate/connection_rbs.rb,
lib/souls/cli/generate/rspec_mutation.rb,
lib/souls/cli/generate/rspec_resolver.rb

Instance Method Summary collapse

Instance Method Details

#connection(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/generate/connection.rb', line 4

def connection(class_name)
  file_dir = "./app/graphql/types/connections/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  singularized_class_name = class_name.underscore.singularize
  file_path = "./app/graphql/types/connections/#{singularized_class_name}_connection.rb"
  File.open(file_path, "w") do |f|
    f.write("      class Types::\#{singularized_class_name.camelize}Connection < Types::BaseConnection\n        edge_type(Types::\#{singularized_class_name.camelize}Edge)\n      end\n    TEXT\n  end\n  SOULs::Painter.create_file(file_path.to_s)\n  file_path\nend\n")

#connection_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/souls/cli/generate/connection_rbs.rb', line 4

def connection_rbs(class_name)
  file_path = ""
  Dir.chdir(SOULs.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/types/connections/"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    singularized_class_name = class_name.underscore.singularize
    file_path = "#{file_dir}#{singularized_class_name}_connection.rbs"
    File.open(file_path, "w") do |f|
      f.write("        module Types\n          class \#{singularized_class_name.camelize}Connection < Types::BaseConnection\n            def self.edge_type: (*untyped) -> untyped\n          end\n        end\n      TEXT\n    end\n    SOULs::Painter.create_file(file_path.to_s)\n  end\n  file_path\nend\n")

#edge(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/generate/edge.rb', line 4

def edge(class_name)
  file_dir = "./app/graphql/types/edges"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  singularized_class_name = class_name.underscore.singularize
  file_path = "./app/graphql/types/edges/#{singularized_class_name}_edge.rb"
  File.open(file_path, "w") do |f|
    f.write("      class Types::\#{singularized_class_name.camelize}Edge < Types::BaseEdge\n        node_type(Types::\#{singularized_class_name.camelize}Type)\n      end\n    TEXT\n  end\n  SOULs::Painter.create_file(file_path.to_s)\n  file_path\nend\n")

#edge_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
# File 'lib/souls/cli/generate/edge_rbs.rb', line 4

def edge_rbs(class_name)
  file_path = ""
  Dir.chdir(SOULs.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/types/edges/"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    singularized_class_name = class_name.underscore.singularize
    file_path = "#{file_dir}#{singularized_class_name}_edge.rbs"
    File.open(file_path, "w") do |f|
      f.write("        module Types\n          class \#{singularized_class_name.camelize}Edge < Types::BaseEdge\n            def self.edge_type: (*untyped) -> untyped\n            def self.node_type: (*untyped) -> untyped\n            def self.global_id_field: (*untyped) -> untyped\n            def self.connection_type: ()-> untyped\n          end\n        end\n      TEXT\n    end\n    SOULs::Painter.create_file(file_path.to_s)\n  end\n  file_path\nend\n")

#job(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/job.rb', line 5

def job(class_name)
  if options[:mailer]
    create_job_mailer_type(class_name)
    mailgun_mailer(class_name)
  else
    create_job_type(class_name)
    create_job(class_name)
  end
  SOULs::Generate.new.invoke(:job_rbs, [class_name], {})
  SOULs::Generate.new.invoke(:rspec_job, [class_name], { mailer: options[:mailer] })
end

#job_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
# File 'lib/souls/cli/generate/job_rbs.rb', line 4

def job_rbs(class_name)
  file_path = ""
  worker_name = FileUtils.pwd.split("/").last
  Dir.chdir(SOULs.get_mother_path.to_s) do
    singularized_class_name = class_name.underscore.singularize
    file_dir = "./sig/#{worker_name}/app/graphql/queries/"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}#{singularized_class_name}.rbs"
    sig_type_path = "./sig/#{worker_name}/app/graphql/types"
    FileUtils.mkdir_p(sig_type_path) unless Dir.exist?(sig_type_path)
    type_file_path = "#{sig_type_path}/#{singularized_class_name}_type.rbs"
    File.open(file_path, "w") do |f|
      f.write("        module Queries\n          class \#{singularized_class_name.camelize} < BaseQuery\n            def self.description: (String) -> untyped\n            def self.field: (:response, String, null: false) -> untyped\n            def self.type: (untyped, null: false) -> untyped\n          end\n        end\n      TEXT\n    end\n    File.open(type_file_path, \"w\") do |f|\n      f.write(<<~TEXT)\n        module Types\n          class \#{singularized_class_name.camelize}Type < SOULs::Types::BaseObject\n            def self.field: (:response, String, null: true) -> untyped\n          end\n        end\n      TEXT\n    end\n    SOULs::Painter.create_file(file_path.to_s)\n  end\n  file_path\nend\n")

#manager(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/manager.rb', line 5

def manager(class_name)
  singularized_class_name = class_name.underscore.singularize
  current_path = FileUtils.pwd
  unless current_path.split("/").last == "api" || current_path.split("/").last == "souls"
    raise(StandardError, "You Are at Wrong Directory! Please Go to Api Directory!")
  end

  create_manager(class_name, options[:mutation])
  SOULs::Generate.new.invoke(:manager_rbs, [singularized_class_name], { mutation: options[:mutation] })
  SOULs::Generate.new.invoke(:rspec_manager, [singularized_class_name], { mutation: options[:mutation] })
end

#manager_rbs(class_name) ⇒ Object



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
# File 'lib/souls/cli/generate/manager_rbs.rb', line 5

def manager_rbs(class_name)
  file_path = ""
  singularized_class_name = class_name.underscore.singularize
  Dir.chdir(SOULs.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/mutations/managers/#{singularized_class_name}_manager"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}/#{options[:mutation]}.rbs"
    File.open(file_path, "w") do |f|
      f.write("        module Mutations\n          module Managers\n            module \#{singularized_class_name.camelize}Manager\n              class \#{options[:mutation].singularize.camelize} < BaseMutation\n                def self.description: (String)-> untyped\n                def self.argument: (untyped, untyped, untyped)-> untyped\n                def self.field: (untyped, untyped, untyped)-> untyped\n              end\n            end\n          end\n        end\n      TEXT\n      SOULs::Painter.create_file(file_path.to_s)\n    end\n  end\n  file_path\nend\n")

#mutation(class_name) ⇒ Object



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

def mutation(class_name)
  singularized_class_name = class_name.singularize

  Dir.chdir(SOULs.get_api_path.to_s) do
    file_dir = "./app/graphql/mutations/base"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "./app/graphql/mutations/base/#{singularized_class_name}/create_#{singularized_class_name}.rb"
    return "Mutation already exist! #{file_path}" if File.exist?(file_path)

    create_mutation(class_name: singularized_class_name)
    update_mutation(class_name: singularized_class_name)
    delete_mutation(class_name: singularized_class_name)
    destroy_delete_mutation(class_name: singularized_class_name)
  end
end

#mutation_rbs(class_name) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/souls/cli/generate/mutation_rbs.rb', line 4

def mutation_rbs(class_name)
  singularized_class_name = class_name.underscore.singularize
  create_rbs_mutation(class_name: singularized_class_name)
  update_rbs_mutation(class_name: singularized_class_name)
  delete_rbs_mutation(class_name: singularized_class_name)
  destroy_delete_rbs_mutation(class_name: singularized_class_name)
end

#query(class_name) ⇒ Object



4
5
6
7
8
9
# File 'lib/souls/cli/generate/query.rb', line 4

def query(class_name)
  file_dir = "./app/graphql/queries/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  singularized_class_name = class_name.singularize
  create_individual_query(class_name: singularized_class_name)
end

#query_rbs(class_name) ⇒ Object



4
5
6
# File 'lib/souls/cli/generate/query_rbs.rb', line 4

def query_rbs(class_name)
  single_query_rbs(class_name)
end

#resolver(class_name) ⇒ Object

Raises:

  • (StandardError)


4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/souls/cli/generate/resolver.rb', line 4

def resolver(class_name)
  singularized_class_name = class_name.singularize.underscore
  file_path = "./app/graphql/resolvers/#{singularized_class_name}_search.rb"
  raise(StandardError, "Resolver already exist! #{file_path}") if File.exist?(file_path)

  resolver_head(class_name: singularized_class_name)
  resolver_params(class_name: singularized_class_name)
  resolver_after_params(class_name: singularized_class_name)
  resolver_before_end(class_name: singularized_class_name)
  resolver_end(class_name: singularized_class_name)
  SOULs::Painter.create_file(file_path.to_s)
  file_path
end

#resolver_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
# File 'lib/souls/cli/generate/resolver_rbs.rb', line 4

def resolver_rbs(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_path = ""
  Dir.chdir(SOULs.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/resolvers"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}/#{singularized_class_name}_search.rbs"
    raise(Thor::Error, "Mutation RBS already exist! #{file_path}") if File.exist?(file_path)

    File.open(file_path, "w") do |f|
      f.write("        module Resolvers\n          class BaseResolver\n          end\n          class \#{singularized_class_name.camelize}Search < BaseResolver\n            include SearchObject\n            def self.scope: () ?{ () -> nil } -> [Hash[Symbol, untyped]]\n            def self.type: (*untyped) -> String\n            def self.option: (:filter, type: untyped, with: :apply_filter) -> String\n            def self.description: (String) -> String\n            def self.types: (*untyped) -> String\n            def decode_global_key: (String value) -> Integer\n            def apply_filter: (untyped scope, untyped value) -> untyped\n\n            class \#{singularized_class_name.camelize}Filter\n              String: String\n              Boolean: Boolean\n              Integer: Integer\n            end\n          end\n        end\n      TEXT\n    end\n  end\n  SOULs::Painter.create_file(file_path.to_s)\n  file_path\nend\n")

#rspec_factory(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/souls/cli/generate/rspec_factory.rb', line 4

def rspec_factory(class_name)
  file_path = "./spec/factories/#{class_name.pluralize}.rb"
  return "RspecFactory already exist! #{file_path}" if File.exist?(file_path)

  singularized_class_name = class_name.singularize
  rspec_factory_head(class_name: singularized_class_name)
  rspec_factory_params(class_name: singularized_class_name)
  rspec_factory_end(class_name: singularized_class_name)
  SOULs::Painter.create_file(file_path.to_s)
  file_path
end

#rspec_job(class_name) ⇒ Object



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/generate/rspec_job.rb', line 5

def rspec_job(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_dir = "./spec/queries/jobs/"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  file_path = "#{file_dir}/#{singularized_class_name}_spec.rb"
  return "RspecJob already exist! #{file_path}" if File.exist?(file_path)

  if options[:mailer]
    File.open(file_path, "w") do |f|
      f.write("        RSpec.describe(\"\#{singularized_class_name.camelize}\") do\n          describe \"Define \#{singularized_class_name.camelize}\" do\n\n            let(:query) do\n              %(query {\n                \#{singularized_class_name.camelize(:lower)} {\n                    response\n                  }\n                }\n              )\n            end\n\n            subject(:result) do\n              SOULsApiSchema.execute(query).as_json\n            end\n\n            it \"return \#{singularized_class_name.camelize} response\" do\n              allow_any_instance_of(::Mailgun::Client).to(receive(:send_message).and_return(true))\n              a1 = result.dig(\"data\", \"\#{singularized_class_name.camelize(:lower)}\")\n              expect(a1).not_to be_empty\n              expect(a1).to(include(\"response\" => be_a(String)))\n            end\n          end\n        end\n      TEXT\n    end\n  else\n    File.open(file_path, \"w\") do |f|\n      f.write(<<~TEXT)\n        RSpec.describe(\"\#{singularized_class_name.camelize}\") do\n          describe \"Define \#{singularized_class_name.camelize}\" do\n\n            let(:query) do\n              %(query {\n                \#{singularized_class_name.camelize(:lower)} {\n                    response\n                  }\n                }\n              )\n            end\n\n            subject(:result) do\n              SOULsApiSchema.execute(query).as_json\n            end\n\n            it \"return \#{singularized_class_name.camelize} response\" do\n              a1 = result.dig(\"data\", \"\#{singularized_class_name.camelize(:lower)}\")\n              expect(a1).not_to be_empty\n              expect(a1).to(include(\"response\" => be_a(String)))\n            end\n          end\n        end\n      TEXT\n    end\n  end\n  SOULs::Painter.create_file(file_path.to_s)\n  file_path\nend\n")

#rspec_manager(class_name) ⇒ Object



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
# File 'lib/souls/cli/generate/rspec_manager.rb', line 5

def rspec_manager(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_dir = "./spec/managers/#{singularized_class_name}_manager"
  FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
  file_path = "#{file_dir}/#{options[:mutation]}_spec.rb"
  return "RspecManager already exist! #{file_path}" if File.exist?(file_path)

  File.open(file_path, "w") do |f|
    f.write("      RSpec.describe(\"\#{options[:mutation].singularize.camelize}\") do\n        describe \"Define \#{options[:mutation].singularize.camelize}\" do\n\n          let(:mutation) do\n            %(mutation {\n              \#{options[:mutation].singularize.camelize(:lower)}(input: {\n                argument: \"argument test!\"\n              }) {\n                  response\n                }\n              }\n            )\n          end\n\n          subject(:result) do\n            SOULsApiSchema.execute(mutation).as_json\n          end\n\n          it \"return User response\" do\n            begin\n              a1 = result.dig(\"data\", \"\#{options[:mutation].singularize.camelize(:lower)}\", \"response\")\n              raise unless a1.present?\n            rescue StandardError\n              raise(StandardError, result)\n            end\n            expect(a1).to(eq(\"success!\"))\n          end\n        end\n      end\n    TEXT\n  end\n  SOULs::Painter.create_file(file_path.to_s)\n  file_path\nend\n")

#rspec_mutation(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/souls/cli/generate/rspec_mutation.rb', line 4

def rspec_mutation(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./spec/mutations/base/#{singularized_class_name}_spec.rb"
  return "RspecMutation already exist! #{file_path}" if File.exist?(file_path)

  rspec_mutation_head(class_name: singularized_class_name)
  rspec_mutation_after_head(class_name: singularized_class_name)
  rspec_mutation_params(class_name: singularized_class_name)
  rspec_mutation_params_response(class_name: singularized_class_name)
  rspec_mutation_end(class_name: singularized_class_name)
  SOULs::Painter.create_file(file_path.to_s)
  file_path
rescue StandardError => e
  raise(StandardError, e)
end

#rspec_query(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/rspec_query.rb', line 4

def rspec_query(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./spec/queries/#{singularized_class_name}_spec.rb"
  return "RspecQuery already exist! #{file_path}" if File.exist?(file_path)

  rspec_query_head(singularized_class_name)
  rspec_query_after_head(singularized_class_name)
  rspec_query_params(singularized_class_name)
  rspec_query_end(singularized_class_name)
  SOULs::Painter.create_file(file_path.to_s)
  file_path
end

#rspec_resolver(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/souls/cli/generate/rspec_resolver.rb', line 4

def rspec_resolver(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./spec/resolvers/#{singularized_class_name}_search_spec.rb"
  return "Resolver already exist! #{file_path}" if File.exist?(file_path)

  rspec_resolver_head(singularized_class_name)
  rspec_resolver_after_head(singularized_class_name)
  rspec_resolver_params(singularized_class_name)
  rspec_resolver_end(singularized_class_name)
  SOULs::Painter.create_file(file_path.to_s)
  file_path
end

#scaffold(class_name) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/souls/cli/generate/application.rb', line 5

def scaffold(class_name)
  singularized_class_name = class_name.singularize
  if options[:rbs]
    run_rbs_scaffold(singularized_class_name)
  else
    run_scaffold(singularized_class_name)
  end
  true
end

#scaffold_allObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/souls/cli/generate/application.rb', line 17

def scaffold_all
  puts(Paint["Let's Go SOULs AUTO CRUD Assist!\n", :cyan])
  SOULs.get_tables.each do |table|
    if options[:rbs]
      SOULs::Generate.new.invoke(:scaffold, [table.singularize], { rbs: options[:rbs] })
    else
      SOULs::Generate.new.invoke(:scaffold, [table.singularize], {})
    end
  end
  true
end

#type(class_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/souls/cli/generate/type.rb', line 4

def type(class_name)
  singularized_class_name = class_name.singularize
  file_path = "./app/graphql/types/#{singularized_class_name}_type.rb"
  return "Type already exist! #{file_path}" if File.exist?(file_path)

  create_type_head(class_name: singularized_class_name)
  create_type_params(class_name: singularized_class_name)
  create_type_end(class_name: singularized_class_name)
  SOULs::Painter.create_file(file_path.to_s)
  file_path
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
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/souls/cli/generate/type_rbs.rb', line 4

def type_rbs(class_name)
  singularized_class_name = class_name.underscore.singularize
  file_path = ""
  Dir.chdir(SOULs.get_mother_path.to_s) do
    file_dir = "./sig/api/app/graphql/types"
    FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
    file_path = "#{file_dir}/#{singularized_class_name}_type.rbs"
    raise(Thor::Error, "Type RBS already exist! #{file_path}") if File.exist?(file_path)

    params = SOULs.get_relation_params(class_name: singularized_class_name)
    File.open(file_path, "w") do |f|
      f.write("        module Types\n          class \#{singularized_class_name.camelize}Type < BaseObject\n            def self.implements: (*untyped) -> untyped\n            def self.global_id_field: (:id) -> String\n      TEXT\n    end\n    File.open(file_path, \"a\") do |f|\n      params[:params].each_with_index do |param, i|\n        type = SOULs.type_check(param[:type])\n        type = \"[\#{type}]\" if param[:array]\n        rbs_type = SOULs.rbs_type_check(param[:type])\n        if i.zero?\n          if param[:column_name].match?(/$*_id\\z/)\n            col_name = param[:column_name].gsub(\"_id\", \"\")\n            f.write(\"    def self.field: (:\#{col_name}, untyped, null: false) -> untyped\\n\")\n          else\n            f.write(\"    def self.field: (:\#{param[:column_name]}, \#{type}, null: true) -> \#{rbs_type}\\n\")\n          end\n        elsif param[:column_name].match?(/$*_id\\z/)\n          col_name = param[:column_name].gsub(\"_id\", \"\")\n          f.write(\"                  | (:\#{col_name}, untyped, null: false) -> untyped\\n\")\n        else\n          f.write(\"                  | (:\#{param[:column_name]}, \#{type}, null: true) -> \#{rbs_type}\\n\")\n        end\n      end\n    end\n\n    File.open(file_path, \"a\") do |f|\n      f.write(<<~TEXT)\n          def self.edge_type: () -> untyped\n            def self.connection_type: () -> untyped\n          end\n        end\n      TEXT\n    end\n  end\n  SOULs::Painter.create_file(file_path.to_s)\n  file_path\nend\n")