nested-generators 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 473261670f239df50312ba210ded00cb31d0744d59a373293c00d4fd9423ce80
4
+ data.tar.gz: a43a7325760333d9af3d4e642b2e1b03084ab6973e38f633481450cb7d3816d2
5
+ SHA512:
6
+ metadata.gz: cdb84c48b0c89bae2d1fa863d1cdaa123d9f4b4a628ab70dd9f9e7ec97ba909f0805ef007dcd40218142fa6161bbe2bb086376935d9709abccceecf6cf5a35e4
7
+ data.tar.gz: d748483ce4b8abf771cdcc001be5c364b89fcb13dbad7b50ec755be3690d3f1ba4b370fafc68fb21b4a659314c6c8632f03e37c269a6224e219011dc2b146b80
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ class NestedGeneratorsBaseGenerator < Rails::Generators::NamedBase
6
+ SCOPES = %w[public protected private].freeze
7
+ RETURN = '
8
+ '
9
+ argument :methods, type: :array,
10
+ default: [],
11
+ banner: 'public:method "protected:method?" "private:method!"'
12
+
13
+ private
14
+
15
+ def create_base
16
+ return if base?
17
+
18
+ template 'base_class_template.erb', base_file_path
19
+ end
20
+
21
+ def create_class_file
22
+ template 'class_template.erb', class_file_path
23
+ end
24
+
25
+ def create_class_spec_file
26
+ template 'class_spec_template.erb', class_spec_file_path
27
+ end
28
+
29
+ def base?
30
+ File.exist?(base_file_path)
31
+ end
32
+
33
+ def base_file_path
34
+ @base_file_path ||= "app/#{@type.pluralize}/application_#{@type}.rb"
35
+ end
36
+
37
+ def class_file_path
38
+ "#{(%w[app] + modules.map(&:underscore)).join('/')}/#{class_file_name}.rb"
39
+ end
40
+
41
+ def class_spec_file_path
42
+ "#{(%w[spec] + modules.map(&:underscore)).join('/')}/#{class_file_name}_spec.rb"
43
+ end
44
+
45
+ def class_file_name
46
+ @class_file_name ||= "#{last_part.underscore}_#{@type}"
47
+ end
48
+
49
+ def last_part
50
+ @last_part ||= class_name.split('::')[-1]
51
+ end
52
+
53
+ def modules
54
+ @modules ||= [@type.pluralize.capitalize] + class_name.split('::')[0..-2]
55
+ end
56
+
57
+ def open_modules_nesting
58
+ modules.each.with_index.map { |namespace, index| "#{indent * index}module #{namespace}" }.join(RETURN)
59
+ end
60
+
61
+ def close_modules_nesting
62
+ modules.each.with_index.map { |_namespace, index| "#{indent * index}end" }.reverse.join(RETURN)
63
+ end
64
+
65
+ def open_class
66
+ "#{RETURN}#{class_indent}class #{last_part}#{@type.capitalize}" + (base? ? " < Application#{@type.capitalize}" : '')
67
+ end
68
+
69
+ def end_class
70
+ "#{class_indent}end#{RETURN}"
71
+ end
72
+
73
+ def class_indent
74
+ @class_indent ||= indent * modules.length
75
+ end
76
+
77
+ def code_indent
78
+ @code_indent ||= indent * (modules.length + 1)
79
+ end
80
+
81
+ def indent
82
+ @indent ||= ' '
83
+ end
84
+
85
+ def in_scope?(method, name)
86
+ " #{method.split(':')&.[](0)} " == " #{name} "
87
+ end
88
+
89
+ def scope?(name)
90
+ methods&.any? { |method| in_scope?(method, name) }
91
+ end
92
+
93
+ def scope_methods(name)
94
+ methods&.select { |method| in_scope?(method, name) }&.map(&:strip)
95
+ end
96
+
97
+ def strip_scope(method, name)
98
+ method.gsub("#{name}:", '')
99
+ end
100
+
101
+ def rspec_empty_message(method = nil)
102
+ "pending 'add some examples to (or delete) #{class_spec_file_path}#{method.nil? ? '' : ('#' + method)}'"
103
+ end
104
+ end
@@ -0,0 +1,29 @@
1
+ Description:
2
+ Generates QueryObject and its corresponding RSpec test file. Accepts
3
+ scoped methods as arguments.
4
+
5
+ Usage:
6
+ Pass the name of the QueryObject, either CamelCased or under_scored, as
7
+ the first argument along with an optional list of its scoped method names.
8
+
9
+ For further instructions refer to the documentation at
10
+ https://github.com/wscourge/nested-generators#queryobject-generator
11
+
12
+ Examples:
13
+ rails generate query tomatoes
14
+
15
+ In the app/queries directory it creates a QueryObject called
16
+ "TomatoesQuery" along with corresponding RSpec test file in the
17
+ spec/queries directory.
18
+
19
+ rails generate query vegetables/potatoes
20
+
21
+ In the app/queries/vegetables directory it creates a QueryObject
22
+ called "Vegetables::PotatoesQuery" along with corresponding RSpec test
23
+ file in the spec/queries/vegetables.
24
+
25
+ rails generate query fruits public:fresh "protected:skip!" "private:fresh?"
26
+
27
+ Creates QueryObject called "FruitsQuery" with three methods: public
28
+ "fresh", protected "skip!" and private "fresh?". Quote method names with
29
+ special characters.
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'generators/nested_generators_base_generator'
4
+
5
+ class QueryGenerator < NestedGeneratorsBaseGenerator
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def initialize(*args, &block)
9
+ super
10
+ @type = 'query'
11
+ end
12
+
13
+ def create_query_file
14
+ create_class_file
15
+ create_class_spec_file
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe <%= @type.capitalize.pluralize %>::<%= class_name %><%= @type.capitalize %> do
4
+ <%- if scope?('public') -%>
5
+ <%- scope_methods('public').each do |method| -%>
6
+ describe '#<%= strip_scope(method, 'public') %>' do
7
+ <%= rspec_empty_message(strip_scope(method, 'public')) %>
8
+ end
9
+
10
+ <%- end -%>
11
+ <%- else -%>
12
+ <%= rspec_empty_message %>
13
+ <%- end -%>
14
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ <%= open_modules_nesting %><%= open_class %>
4
+ <%= code_indent %>def initialize(relation)
5
+ <%= code_indent %> @relation = relation
6
+ <%= code_indent %>end
7
+ <%- SCOPES.each.with_index do |name, index| -%>
8
+ <%- if index.zero? -%>
9
+ <%- if scope?(name) -%>
10
+ <%- scope_methods(name).each do |method| %>
11
+ <%= code_indent %>def <%= method.gsub("#{name}:", '') %>
12
+ <%= code_indent %>end
13
+ <%- end -%>
14
+ <%- end -%>
15
+ <%- else -%>
16
+ <%- if scope?(name) -%><%- %>
17
+ <%- %><%= code_indent %><%= name %>
18
+ <%- scope_methods(name).each do |method| %>
19
+ <%= code_indent %>def <%= method.gsub("#{name}:", '') %>
20
+ <%= code_indent %>end
21
+ <%- end -%>
22
+ <%- end -%>
23
+ <%- end -%>
24
+ <%- end -%>
25
+ <%= end_class %><%= close_modules_nesting %>
@@ -0,0 +1,34 @@
1
+ Description:
2
+ Generates ServiceObject and its corresponding RSpec test file. Accepts
3
+ scoped methods as arguments.
4
+
5
+ IMPORTANT: On the first run, it also generates ApplicationService class
6
+ file from which all further generated ServiceObjects inherit.
7
+
8
+ Usage:
9
+ Pass the name of the ServiceObject, either CamelCased or under_scored, as
10
+ the first argument along with an optional list of its scoped method names.
11
+
12
+ For further instructions refer to the documentation at
13
+ https://github.com/wscourge/nested-generators#serviceobject-generator
14
+
15
+ Examples:
16
+ rails generate service cucumber
17
+
18
+ In the app/services directory it creates a ServiceObject called
19
+ "CucumberService" containing single public "call" method and the
20
+ "ApplicationService" along with corresponding RSpec test file in the
21
+ spec/services directory.
22
+
23
+ rails generate service vegetables/tomato
24
+
25
+ In the app/services/vegetables directory it creates a ServiceObject
26
+ called "Vegetables::TomatoService" containing single public "call"
27
+ method along with corresponding RSpec test file in the
28
+ spec/services/vegetables.
29
+
30
+ rails generate service swat public:nuke "protected:arm!" "private:ready?"
31
+
32
+ Creates ServiceObject called "SwatService" with four methods: public
33
+ "call", public "nuke", protected "arm!" and private "ready?". Quote
34
+ method names with special characters.
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'generators/nested_generators_base_generator'
4
+
5
+ class ServiceGenerator < NestedGeneratorsBaseGenerator
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def initialize(*args, &block)
9
+ super
10
+ @type = 'service'
11
+ end
12
+
13
+ def create_service_file
14
+ create_base
15
+ create_class_file
16
+ create_class_spec_file
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= @type.pluralize.capitalize%>
4
+ class Application<%= @type.capitalize%>
5
+ def self.call(*args)
6
+ new(*args).call
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe <%= @type.capitalize.pluralize %>::<%= class_name %><%= @type.capitalize %> do
4
+ describe '#call' do
5
+ <%= rspec_empty_message('call') %>
6
+ end
7
+ <%- if scope?('public') -%>
8
+ <%- scope_methods('public').each do |method| %>
9
+ describe '#<%= strip_scope(method, 'public') %>' do
10
+ <%= rspec_empty_message(strip_scope(method, 'public')) %>
11
+ end
12
+ <%- end -%>
13
+ <%- end -%>
14
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ <%= open_modules_nesting %><%= open_class %>
4
+ <%= code_indent %>def initialize
5
+ <%= code_indent %>end
6
+
7
+ <%= code_indent %>def call
8
+ <%= code_indent %>end
9
+ <%- SCOPES.each.with_index do |name, index| -%>
10
+ <%- if index.zero? -%>
11
+ <%- if scope?(name) -%>
12
+ <%- scope_methods(name).each do |method| %>
13
+ <%= code_indent %>def <%= method.gsub("#{name}:", '') %>
14
+ <%= code_indent %>end
15
+ <%- end -%>
16
+ <%- end -%>
17
+ <%- else -%>
18
+ <%- if scope?(name) -%><%- %>
19
+ <%- %><%= code_indent %><%= name %>
20
+ <%- scope_methods(name).each do |method| %>
21
+ <%= code_indent %>def <%= method.gsub("#{name}:", '') %>
22
+ <%= code_indent %>end
23
+ <%- end -%>
24
+ <%- end -%>
25
+ <%- end -%>
26
+ <%- end -%>
27
+ <%= end_class %><%= close_modules_nesting %>
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nested-generators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - wscourge
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.16.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.16.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.23
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.23
55
+ description: A handful of rails generate scripts
56
+ email:
57
+ - wscourge@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/generators/nested_generators_base_generator.rb
63
+ - lib/generators/query/USAGE
64
+ - lib/generators/query/query_generator.rb
65
+ - lib/generators/query/templates/class_spec_template.erb
66
+ - lib/generators/query/templates/class_template.erb
67
+ - lib/generators/service/USAGE
68
+ - lib/generators/service/service_generator.rb
69
+ - lib/generators/service/templates/base_class_template.erb
70
+ - lib/generators/service/templates/class_spec_template.erb
71
+ - lib/generators/service/templates/class_template.erb
72
+ homepage: https://github.com/wscourge/nested-generators
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message: Thanks for installing! See rails generate service --help for
77
+ usage
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.0.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Nested generators
96
+ test_files: []