File: worker_generator.rb

package info (click to toggle)
ruby-sidekiq 6.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 860 kB
  • sloc: ruby: 4,076; makefile: 20; sh: 6
file content (57 lines) | stat: -rw-r--r-- 1,388 bytes parent folder | download
1
2
3
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
require "rails/generators/named_base"

module Sidekiq
  module Generators # :nodoc:
    class WorkerGenerator < ::Rails::Generators::NamedBase # :nodoc:
      desc "This generator creates a Sidekiq Worker in app/workers and a corresponding test"

      check_class_collision suffix: "Worker"

      def self.default_generator_root
        File.dirname(__FILE__)
      end

      def create_worker_file
        template "worker.rb.erb", File.join("app/workers", class_path, "#{file_name}_worker.rb")
      end

      def create_test_file
        return unless test_framework

        if test_framework == :rspec
          create_worker_spec
        else
          create_worker_test
        end
      end

      private

      def create_worker_spec
        template_file = File.join(
          "spec/workers",
          class_path,
          "#{file_name}_worker_spec.rb"
        )
        template "worker_spec.rb.erb", template_file
      end

      def create_worker_test
        template_file = File.join(
          "test/workers",
          class_path,
          "#{file_name}_worker_test.rb"
        )
        template "worker_test.rb.erb", template_file
      end

      def file_name
        @_file_name ||= super.sub(/_?worker\z/i, "")
      end

      def test_framework
        ::Rails.application.config.generators.options[:rails][:test_framework]
      end
    end
  end
end