pattern_generator 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/base_generator.rb +59 -0
  3. data/lib/generators/form/form_generator.rb +9 -0
  4. data/lib/generators/form/templates/form.rb +5 -0
  5. data/lib/generators/form/templates/form_spec.rb +5 -0
  6. data/lib/generators/form/templates/form_test.rb +7 -0
  7. data/lib/generators/policy/policy_generator.rb +3 -18
  8. data/lib/generators/policy/templates/policy.rb +1 -5
  9. data/lib/generators/policy/templates/policy_spec.rb +2 -6
  10. data/lib/generators/policy/templates/policy_test.rb +1 -1
  11. data/lib/generators/poro/poro_generator.rb +9 -16
  12. data/lib/generators/service/service_generator.rb +3 -18
  13. data/lib/generators/service/templates/service.rb +1 -5
  14. data/lib/generators/service/templates/service_spec.rb +2 -6
  15. data/lib/generators/service/templates/service_test.rb +1 -1
  16. data/lib/pattern_generator.rb +6 -0
  17. data/lib/pattern_generator/version.rb +1 -1
  18. data/test/dummy/config/application.rb +0 -1
  19. data/test/generators/form_generator_test.rb +53 -0
  20. data/test/generators/policy_generator_test.rb +15 -2
  21. data/test/generators/poro_generator_test.rb +1 -2
  22. data/test/generators/service_generator_test.rb +17 -4
  23. data/test/test_helper.rb +6 -2
  24. metadata +10 -29
  25. data/test/dummy/README.rdoc +0 -28
  26. data/test/dummy/app/assets/javascripts/application.js +0 -13
  27. data/test/dummy/app/assets/stylesheets/application.css +0 -15
  28. data/test/dummy/app/controllers/application_controller.rb +0 -5
  29. data/test/dummy/app/helpers/application_helper.rb +0 -2
  30. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  31. data/test/dummy/db/test.sqlite3 +0 -0
  32. data/test/dummy/log/development.log +0 -0
  33. data/test/dummy/log/test.log +0 -2107
  34. data/test/dummy/public/404.html +0 -67
  35. data/test/dummy/public/422.html +0 -67
  36. data/test/dummy/public/500.html +0 -66
  37. data/test/dummy/public/favicon.ico +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c6688fe0b90f8cf237c4e6c2a18268e1f77bede
4
- data.tar.gz: 74710dcaf305ebad7f61c53634b2fc0c0432fc0c
3
+ metadata.gz: 7ea1fb8d728c10d64b9cb86e82ce3b1a983148ed
4
+ data.tar.gz: 8ee5f1ebe85f20c7db9314c2904eb26eb8d23e79
5
5
  SHA512:
6
- metadata.gz: ab019869856924be0d44a22a38f9a3bd5aef2e4897e11d584603f7c0e46e7c51e9bb20f211d34733840f80532e678ce2ca49e7b5c596b615c7ee3c7a6dade1ef
7
- data.tar.gz: f9fad96993b444f6e7fad67d63a2b27c4cd67b9325f93b28a7c605aa9742334dc6647da10a1a1f1dfa72ec24e48e79e7f55ed18da8b656734ef902af6b77e901
6
+ metadata.gz: 54b325aa4025cb188bbf13b2d4824e2ff404c2551cb5c08c17f0b4b8d1e68d28a3c057bbdad0b01b6aad459e718d03d70811a3f63f5580b2a58ed05b53d63d02
7
+ data.tar.gz: 500ad4871f906f37a3ca41bf7ed0b9e35ac5cb87779a32dd871ca6c145e53a914fcc60e2356a653d029af873ccdbea26f9b416922ac5b8f0d33ab4bc6bde1a24
@@ -0,0 +1,59 @@
1
+ require "active_support/inflector"
2
+
3
+ # Base class for all generators to come. If you want to override some behaviour, all you
4
+ # got to do is override a method in the child class.
5
+ class BaseGenerator < Rails::Generators::NamedBase
6
+ class_option :rspec, type: :boolean, desc: 'DEFAULT: Define rspec as your test framework.'
7
+ class_option :minitest, type: :boolean, desc: 'Test framework to generate test. (rspec or minitest)'
8
+ class_option :no_suffix, type: :boolean, desc: "If you don't want any pattern suffix. Ex: AuthenticationService. Service is the suffix."
9
+
10
+ # Creates the file for the requested pattern based on a template file.
11
+ def copy_pattern_file
12
+ template "#{pattern_name}.rb", generated_file_path
13
+ end
14
+
15
+ # Creates the test file for the request pattern based on a template file.
16
+ def copy_pattern_test_file
17
+ template "#{pattern_name}_#{test_suite_identifier}.rb", generated_test_file_path
18
+ end
19
+
20
+ private
21
+
22
+ # Determines the class name based on the file name given by the user.
23
+ def class_name
24
+ file_name.classify + suffix.classify
25
+ end
26
+
27
+ # This method must be overrided in the child classes.
28
+ def pattern_name
29
+ raise NotImplementedError
30
+ end
31
+
32
+ def folder_name
33
+ pattern_name.pluralize
34
+ end
35
+
36
+ # Generates the pattern suffix.
37
+ # Ex: _service
38
+ def suffix
39
+ options.no_suffix? ? "" : "_#{pattern_name}"
40
+ end
41
+
42
+ # Generates the file path.
43
+ # Ex: app/services/authentication.rb
44
+ def generated_file_path
45
+ "app/#{folder_name}/#{file_name}#{suffix}.rb"
46
+ end
47
+
48
+ def test_suite_identifier
49
+ identifier = "spec"
50
+ identifier = "test" if options.minitest?
51
+ identifier
52
+ end
53
+
54
+ # Generates the test file path.
55
+ # Ex: spec/services/authentication_spec.rb
56
+ def generated_test_file_path
57
+ "#{test_suite_identifier}/#{folder_name}/#{file_name}#{suffix}_#{test_suite_identifier}.rb"
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ class FormGenerator < BaseGenerator
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ private
5
+
6
+ def pattern_name
7
+ "form"
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class <%= class_name %>
2
+ def initialize
3
+
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe <%= class_name %>, type: :form do
4
+ pending "Add some tests to #{__FILE__}"
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class <%= class_name %>Test < Minitest::Test
4
+ def test_call
5
+
6
+ end
7
+ end
@@ -1,24 +1,9 @@
1
- require 'active_support/inflector'
2
-
3
- class PolicyGenerator < Rails::Generators::NamedBase
1
+ class PolicyGenerator < BaseGenerator
4
2
  source_root File.expand_path('../templates', __FILE__)
5
- class_option :test_suite, type: :string, default: 'rspec', desc: 'Test framework to generate test. (rspec or minitest)'
6
-
7
- def generate_template
8
- template 'policy.rb', "app/policies/#{file_name}_policy.rb"
9
- end
10
-
11
- def generate_test
12
- if options.test_suite == 'rspec'
13
- template 'policy_spec.rb', "spec/policies/#{file_name}_policy_spec.rb"
14
- elsif options.test_suite == 'minitest'
15
- template 'policy_test.rb', "test/policies/#{file_name}_policy_test.rb"
16
- end
17
- end
18
3
 
19
4
  private
20
5
 
21
- def class_name
22
- file_name.classify
6
+ def pattern_name
7
+ "policy"
23
8
  end
24
9
  end
@@ -1,9 +1,5 @@
1
- class <%= class_name %>Policy
1
+ class <%= class_name %>
2
2
  def initialize
3
3
 
4
4
  end
5
-
6
- def policy_method
7
- # Customize to fit your need
8
- end
9
5
  end
@@ -1,9 +1,5 @@
1
1
  require 'rails_helper'
2
2
 
3
- RSpec.describe <%= class_name %>Policy, type: :policy do
4
- describe '#policy_method' do
5
- it 'does something' do
6
-
7
- end
8
- end
3
+ RSpec.describe <%= class_name %>, type: :policy do
4
+ pending "Add some tests to #{__FILE__}"
9
5
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class <%= class_name %>PolicyTest < Minitest::Test
3
+ class <%= class_name %>Test < Minitest::Test
4
4
  def test_something
5
5
 
6
6
  end
@@ -1,24 +1,17 @@
1
- require 'active_support/inflector'
2
-
3
- class PoroGenerator < Rails::Generators::NamedBase
1
+ class PoroGenerator < BaseGenerator
4
2
  source_root File.expand_path('../templates', __FILE__)
5
- class_option :test_suite, type: :string, default: 'rspec', desc: 'Test framework to generate test. (rspec or minitest)'
6
3
 
7
- def generate_template
8
- template 'poro.rb', "app/models/#{file_name}.rb"
9
- end
4
+ private
10
5
 
11
- def generate_test
12
- if options.test_suite == 'rspec'
13
- template 'poro_spec.rb', "spec/models/#{file_name}_spec.rb"
14
- elsif options.test_suite == 'minitest'
15
- template 'poro_test.rb', "test/models/#{file_name}_test.rb"
16
- end
6
+ def folder_name
7
+ "models"
17
8
  end
18
9
 
19
- private
10
+ def pattern_name
11
+ "poro"
12
+ end
20
13
 
21
- def class_name
22
- file_name.classify
14
+ def suffix
15
+ ""
23
16
  end
24
17
  end
@@ -1,24 +1,9 @@
1
- require "active_support/inflector"
2
-
3
- class ServiceGenerator < Rails::Generators::NamedBase
1
+ class ServiceGenerator < BaseGenerator
4
2
  source_root File.expand_path('../templates', __FILE__)
5
- class_option :test_suite, type: :string, default: 'rspec', desc: 'Test framework to generate test. (rspec or minitest)'
6
-
7
- def copy_service_file
8
- template 'service.rb', "app/services/#{file_name}_service.rb"
9
- end
10
-
11
- def copy_service_test_file
12
- if options.test_suite == 'rspec'
13
- template 'service_spec.rb', "spec/services/#{file_name}_service_spec.rb"
14
- elsif options.test_suite == 'minitest'
15
- template 'service_test.rb', "test/services/#{file_name}_service_test.rb"
16
- end
17
- end
18
3
 
19
4
  private
20
5
 
21
- def class_name
22
- file_name.classify
6
+ def pattern_name
7
+ "service"
23
8
  end
24
9
  end
@@ -1,9 +1,5 @@
1
- class <%= class_name %>Service
1
+ class <%= class_name %>
2
2
  def initialize
3
3
 
4
4
  end
5
-
6
- def call
7
-
8
- end
9
5
  end
@@ -1,9 +1,5 @@
1
1
  require 'rails_helper'
2
2
 
3
- RSpec.describe <%= class_name %>Service, type: :service do
4
- describe '#call' do
5
- it 'performs something' do
6
-
7
- end
8
- end
3
+ RSpec.describe <%= class_name %>, type: :service do
4
+ pending "Add some tests to #{__FILE__}"
9
5
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class <%= class_name %>ServiceTest < Minitest::Test
3
+ class <%= class_name %>Test < Minitest::Test
4
4
  def test_call
5
5
 
6
6
  end
@@ -1,4 +1,10 @@
1
1
  require "rails/generators"
2
2
 
3
+ require_relative "./generators/base_generator"
4
+
5
+ %w(form policy poro service).each do |pattern|
6
+ require_relative "./generators/#{pattern}/#{pattern}_generator"
7
+ end
8
+
3
9
  module PatternGenerator
4
10
  end
@@ -1,3 +1,3 @@
1
1
  module PatternGenerator
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -20,7 +20,6 @@ module Dummy
20
20
  # config.i18n.default_locale = :de
21
21
 
22
22
  # Do not swallow errors in after_commit/after_rollback callbacks.
23
- config.active_record.raise_in_transactional_callbacks = true
24
23
  end
25
24
  end
26
25
 
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ class FormGeneratorTest < Rails::Generators::TestCase
4
+ tests FormGenerator
5
+
6
+ destination File.join(Rails.root)
7
+ teardown :cleanup
8
+
9
+ def cleanup
10
+ FileUtils.rm_rf(File.join(Rails.root, 'app/forms'))
11
+ FileUtils.rm_rf(File.join(Rails.root, 'spec/forms'))
12
+ FileUtils.rm_rf(File.join(Rails.root, 'test/forms'))
13
+ end
14
+
15
+ test 'correct file is generated' do
16
+ run_generator %w(registration)
17
+
18
+ assert_file 'app/forms/registration_form.rb' do |content|
19
+ assert_match /RegistrationForm/, content
20
+ assert_match /def initialize/, content
21
+ end
22
+ end
23
+
24
+ test 'correct spec file is generated' do
25
+ run_generator %w(registration)
26
+
27
+ assert_file 'spec/forms/registration_form_spec.rb' do |content|
28
+ assert_match /RSpec.describe RegistrationForm, type: :form/, content
29
+ assert_match /pending/, content
30
+ end
31
+ end
32
+
33
+ test 'generates minitest file if --minitest options is passed' do
34
+ run_generator %w(registration --minitest)
35
+
36
+ assert_file 'test/forms/registration_form_test.rb' do |content|
37
+ assert_match /class RegistrationFormTest < Minitest::Test/, content
38
+ end
39
+ end
40
+
41
+ test 'generates files with no suffixes if --no-sufix option is passed' do
42
+ run_generator %w(registration --no-suffix)
43
+
44
+ assert_file 'app/forms/registration.rb' do |content|
45
+ assert_match /Registration/, content
46
+ assert_match /def initialize/, content
47
+ end
48
+
49
+ assert_file 'spec/forms/registration_spec.rb' do |content|
50
+ assert_match /RSpec.describe Registration, type: :form/, content
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
- require 'generators/policy/policy_generator'
3
2
 
4
3
  class PolicyGeneratorTest < Rails::Generators::TestCase
5
4
  tests PolicyGenerator
@@ -19,6 +18,7 @@ class PolicyGeneratorTest < Rails::Generators::TestCase
19
18
 
20
19
  assert_file 'app/policies/active_user_policy.rb' do |content|
21
20
  assert_match /class ActiveUserPolicy/, content
21
+ assert_match /def initialize/, content
22
22
  end
23
23
  end
24
24
 
@@ -27,15 +27,28 @@ class PolicyGeneratorTest < Rails::Generators::TestCase
27
27
 
28
28
  assert_file 'spec/policies/active_user_policy_spec.rb' do |content|
29
29
  assert_match /RSpec.describe ActiveUserPolicy, type: :policy do/, content
30
+ assert_match /pending/, content
30
31
  end
31
32
  end
32
33
 
33
34
  test "if minitest is specified, minitest file is generated" do
34
- run_generator %w(active_user --test-suite=minitest)
35
+ run_generator %w(active_user --minitest)
35
36
 
36
37
  assert_file 'test/policies/active_user_policy_test.rb' do |content|
37
38
  assert_match /class ActiveUserPolicyTest < Minitest::Test/, content
38
39
  end
39
40
  end
40
41
 
42
+ test 'generates files with no suffixes if --no-sufix option is passed' do
43
+ run_generator %w(active_user --no-suffix)
44
+
45
+ assert_file 'app/policies/active_user.rb' do |content|
46
+ assert_match /ActiveUser/, content
47
+ assert_match /def initialize/, content
48
+ end
49
+
50
+ assert_file 'spec/policies/active_user_spec.rb' do |content|
51
+ assert_match /RSpec.describe ActiveUser, type: :policy/, content
52
+ end
53
+ end
41
54
  end
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
- require 'generators/poro/poro_generator'
3
2
 
4
3
  class PoroGeneratorTest < Rails::Generators::TestCase
5
4
  tests PoroGenerator
@@ -31,7 +30,7 @@ class PoroGeneratorTest < Rails::Generators::TestCase
31
30
  end
32
31
 
33
32
  test 'minitest file is generated when test-suite is minitest' do
34
- run_generator %w(grade --test-suite=minitest)
33
+ run_generator %w(grade --minitest)
35
34
 
36
35
  assert_file 'test/models/grade_test.rb' do |content|
37
36
  assert_match /class GradeTest < MiniTest::Test/, content
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
- require 'generators/service/service_generator'
3
2
 
4
3
  class ServiceGeneratorTest < Rails::Generators::TestCase
5
4
  tests ServiceGenerator
@@ -17,8 +16,8 @@ class ServiceGeneratorTest < Rails::Generators::TestCase
17
16
  run_generator %w(find_match)
18
17
 
19
18
  assert_file 'app/services/find_match_service.rb' do |content|
20
- assert_match /FindMatchService/, content
21
- assert_match /def call/, content
19
+ assert_match /class FindMatchService/, content
20
+ assert_match /def initialize/, content
22
21
  end
23
22
  end
24
23
 
@@ -27,14 +26,28 @@ class ServiceGeneratorTest < Rails::Generators::TestCase
27
26
 
28
27
  assert_file 'spec/services/find_match_service_spec.rb' do |content|
29
28
  assert_match /RSpec.describe FindMatchService, type: :service/, content
29
+ assert_match /pending/, content
30
30
  end
31
31
  end
32
32
 
33
33
  test 'generates minitest file if test-framework is minitest' do
34
- run_generator %w(find_match --test-suite=minitest)
34
+ run_generator %w(find_match --minitest)
35
35
 
36
36
  assert_file 'test/services/find_match_service_test.rb' do |content|
37
37
  assert_match /class FindMatchServiceTest < Minitest::Test/, content
38
38
  end
39
39
  end
40
+
41
+ test 'generates files with no suffixes if --no-sufix option is passed' do
42
+ run_generator %w(find_match --no-suffix)
43
+
44
+ assert_file 'app/services/find_match.rb' do |content|
45
+ assert_match /FindMatch/, content
46
+ assert_match /def initialize/, content
47
+ end
48
+
49
+ assert_file 'spec/services/find_match_spec.rb' do |content|
50
+ assert_match /RSpec.describe FindMatch, type: :service/, content
51
+ end
52
+ end
40
53
  end