mj 0.1.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2e748dee2e36cb0160773697c160a224fd1f835852c4d9654e5be3fa6eae4a7
4
- data.tar.gz: 3cdd9fcbe46b922e7d5fa59c4cc728a3f93a16206259ec81fa66af9ce53728b8
3
+ metadata.gz: 69dc7451d088acc2d1a698e89db77361de5cdf2bb07bb137b8e63ddc224f6fc2
4
+ data.tar.gz: 7f1ac63a2d0b9e44845242bbf77f1154a0f14da3ba2a2a361b9dc90015a67904
5
5
  SHA512:
6
- metadata.gz: '0854440ae12e23d51031874cb521e3bf265b11cd36fd1bce4ab7badfa86f46f1b5471d19937479ff33eb301d0407e92040a1417d8b146337ae5dd20d5b8b090b'
7
- data.tar.gz: 21352d2b67831e6c8969e5c0668ac6f23f2586631a2962d3ac03c1b8a20f8a9296171a010a10f5f257abe9262c8a2f9e0814d47c24b8fc0b8c6919db73ea12d5
6
+ metadata.gz: 34c4108ff9d1bb29b9efa6051c5bf7399ac93fa7bb543cb3fcddfbbeabbd7be27fc19c8b66e088a56be329fd1291893c5a768699e74aaaa4d0c16ccdf4a9ac66
7
+ data.tar.gz: cc60732174983f0616a47a7735ab34229234676fea0a6e472adc639bfad107823c95b0f3498e2246ec7dccb2a3e40d23a290fd430ec417db5e98f3e7e0c6482f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mj (0.1.0)
4
+ mj (0.4.0)
5
5
  thor (~> 1.2.1)
6
6
 
7
7
  GEM
@@ -6,9 +6,10 @@ module Mj
6
6
  attr_reader :path
7
7
  attr_reader :type
8
8
 
9
- def initialize(path:, type:)
9
+ def initialize(path:, type:, metadata: {})
10
10
  @path = path
11
11
  @type = type
12
+ @metadata = metadata || {}
12
13
  end
13
14
 
14
15
  def exist?
@@ -22,6 +23,31 @@ module Mj
22
23
 
23
24
  other.path == path && other.type == type
24
25
  end
26
+
27
+ def to_s(debug: false)
28
+ parts = [path]
29
+
30
+ if debug
31
+ parts.push("(#{metadata})")
32
+ end
33
+
34
+ parts.join
35
+ end
36
+
37
+ private
38
+
39
+ def metadata
40
+ data = {
41
+ type: type,
42
+ exists: exist?
43
+ }
44
+
45
+ @metadata.keys.sort.each do |key|
46
+ data[key] = @metadata[key]
47
+ end
48
+
49
+ data.map { |k, v| "#{k}:#{v}" }.join(",")
50
+ end
25
51
  end
26
52
  end
27
53
  end
@@ -41,6 +41,10 @@ module Mj
41
41
  to_s.start_with?(*args)
42
42
  end
43
43
 
44
+ def end_with?(*args)
45
+ to_s.end_with?(*args)
46
+ end
47
+
44
48
  def without_prefix(prefix)
45
49
  sub(/^#{prefix}/, "")
46
50
  end
@@ -5,17 +5,23 @@ module Mj
5
5
  module Resolvers
6
6
  class Base
7
7
  def resolve(file)
8
- [].tap do |alternatives|
8
+ [].tap do |candidates|
9
9
  if apply_to?(file)
10
- create_alternatives(file, alternatives)
10
+ add_candidates(file, candidates)
11
11
  end
12
12
  end
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- def create_candidate(path, type)
18
- AlternativeFile::Candidate.new(path: path, type: type)
17
+ def add_candidate(path, type, to:)
18
+ candidate = AlternativeFile::Candidate.new(
19
+ path: path.to_s,
20
+ type: type.to_s,
21
+ metadata: { resolved_by: self.class.name }
22
+ )
23
+
24
+ to.push(candidate)
19
25
  end
20
26
 
21
27
  def apply_to?(_file)
@@ -17,31 +17,29 @@ module Mj
17
17
  )
18
18
  end
19
19
 
20
- def create_alternatives(file, alternatives)
20
+ def add_candidates(file, candidates)
21
21
  if file.start_with?("app/controllers")
22
- add_controller_test(file, alternatives, "spec")
23
- add_controller_test(file, alternatives, "test")
24
- add_integration_test(file, alternatives, "spec")
25
- add_integration_test(file, alternatives, "test")
22
+ add_controller_test(file, candidates, "spec")
23
+ add_controller_test(file, candidates, "test")
24
+ add_integration_test(file, candidates, "spec")
25
+ add_integration_test(file, candidates, "test")
26
26
  return
27
27
  end
28
28
 
29
- resolve_controller(file, alternatives)
29
+ resolve_controller(file, candidates)
30
30
  end
31
31
 
32
- def add_integration_test(file, alternatives, type)
32
+ def add_integration_test(file, candidates, type)
33
33
  path = file.without_prefix("app/controllers").without_suffix(".rb").trim_slashes
34
- alternative = create_candidate("#{type}/integration/#{path}_#{type}.rb", "integration_#{type}")
35
- alternatives.push(alternative)
34
+ add_candidate("#{type}/integration/#{path}_#{type}.rb", "integration_#{type}", to: candidates)
36
35
  end
37
36
 
38
- def add_controller_test(file, alternatives, type)
37
+ def add_controller_test(file, candidates, type)
39
38
  path = file.without_prefix("app/controllers").without_suffix(".rb").trim_slashes
40
- alternative = create_candidate("#{type}/controllers/#{path}_#{type}.rb", "controller_#{type}")
41
- alternatives.push(alternative)
39
+ add_candidate("#{type}/controllers/#{path}_#{type}.rb", "controller_#{type}", to: candidates)
42
40
  end
43
41
 
44
- def resolve_controller(file, alternatives)
42
+ def resolve_controller(file, candidates)
45
43
  controller_path = file.sub("test/integration", "app/controllers")
46
44
  .sub("spec/integration", "app/controllers")
47
45
  .sub("spec/controllers", "app/controllers")
@@ -50,7 +48,7 @@ module Mj
50
48
  .sub("_test.rb", ".rb")
51
49
  .to_s
52
50
 
53
- alternatives.push(create_candidate(controller_path, "controller"))
51
+ add_candidate(controller_path, "controller", to: candidates)
54
52
  end
55
53
  end
56
54
  end
@@ -11,11 +11,14 @@ module Mj
11
11
  file.extension == "rb"
12
12
  end
13
13
 
14
- def create_alternatives(file, alternatives)
14
+ def add_candidates(file, candidates)
15
15
  ruby_file = Ruby::RubyFile.new(file)
16
- alternatives.push(create_candidate("app/#{ruby_file.class_path}.rb", "model"))
17
- alternatives.push(create_candidate("spec/#{ruby_file.class_path}_spec.rb", "rspec"))
18
- alternatives.push(create_candidate("test/#{ruby_file.class_path}_test.rb", "minitest"))
16
+ add_candidate("app/#{ruby_file.class_path}.rb", :model, to: candidates)
17
+ add_candidate("spec/#{ruby_file.class_path}_spec.rb", :spec, to: candidates)
18
+ add_candidate("test/#{ruby_file.class_path}_test.rb", :minitest, to: candidates)
19
+
20
+ # lib files
21
+ add_candidate("lib/#{ruby_file.class_path}.rb", "lib", to: candidates)
19
22
  end
20
23
  end
21
24
  end
@@ -8,6 +8,7 @@ module Mj
8
8
  def class_path
9
9
  path_without_extension
10
10
  .without_prefix("app")
11
+ .without_prefix("lib")
11
12
  .without_prefix("spec")
12
13
  .without_suffix("_spec")
13
14
  .without_suffix("_test")
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Resolvers
6
+ module Ruby
7
+ class ViewComponentResolver < Resolvers::Base
8
+ private
9
+
10
+ def apply_to?(file)
11
+ file.end_with?("component.rb", "component.html.erb")
12
+ end
13
+
14
+ def add_candidates(file, candidates)
15
+ if file.end_with?("component.rb")
16
+ return resolve_template(file, candidates)
17
+ end
18
+
19
+ resolve_component_class(file, candidates)
20
+ end
21
+
22
+ def resolve_template(file, candidates)
23
+ file_name = file.sub(/_component.rb$/, "_component.html.erb")
24
+ add_candidate(file_name, "component_template", to: candidates)
25
+ end
26
+
27
+ def resolve_component_class(file, candidates)
28
+ file_name = file.sub(/_component.html.erb$/, "_component.rb")
29
+ add_candidate(file_name, "component_class", to: candidates)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -8,6 +8,7 @@ require_relative "resolver"
8
8
  require_relative "resolvers/base"
9
9
  require_relative "resolvers/ruby/rails_resolver"
10
10
  require_relative "resolvers/ruby/rails_controller_resolver"
11
+ require_relative "resolvers/ruby/view_component_resolver"
11
12
  require_relative "resolvers/ruby/ruby_file"
12
13
 
13
14
  module Mj
@@ -16,6 +17,7 @@ module Mj
16
17
  desc "list <reference-file>", "List all alternative files"
17
18
  option :types, type: :string, banner: "<comma-separated-types>", aliases: :t
18
19
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
20
+ option :debug, type: :boolean, banner: "print debug information", aliases: :d
19
21
  def list(reference_file)
20
22
  file = CurrentFile.new(reference_file)
21
23
  print_candidates(resolve(file))
@@ -24,6 +26,7 @@ module Mj
24
26
  desc "next <reference-file>", "Next alternative file"
25
27
  option :types, type: :string, banner: "<comma-separated-types>", aliases: :t
26
28
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
29
+ option :debug, type: :boolean, banner: "print debug information", aliases: :d
27
30
  def next(reference_file)
28
31
  file = CurrentFile.new(reference_file)
29
32
  candidate = resolve(file).after(file)
@@ -33,6 +36,7 @@ module Mj
33
36
  desc "prev <reference-file>", "Previous alternative file"
34
37
  option :types, type: :string, banner: "<comma-separated-types>", aliases: :t
35
38
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
39
+ option :debug, type: :boolean, banner: "print debug information", aliases: :d
36
40
  def prev(reference_file)
37
41
  file = CurrentFile.new(reference_file)
38
42
  candidate = resolve(file).before(file)
@@ -43,15 +47,17 @@ module Mj
43
47
  @resolvers ||= AlternativeFile::Resolver.new.tap do |resolvers|
44
48
  resolvers.add(Resolvers::Ruby::RailsResolver.new)
45
49
  resolvers.add(Resolvers::Ruby::RailsControllerResolver.new)
50
+ resolvers.add(Resolvers::Ruby::ViewComponentResolver.new)
46
51
  end
47
52
  end
48
53
 
49
54
  private
50
55
 
51
56
  def print_candidates(candidates)
52
- $stdout.puts candidates.map(&:path).join(" ")
57
+ $stdout.puts candidates.map { |i| i.to_s(debug: options[:debug]) }.join(" ")
53
58
  end
54
59
 
60
+ # rubocop:disable Metrics/MethodLength
55
61
  def resolve(file)
56
62
  candidates = self.class.resolvers.resolve(file)
57
63
 
@@ -63,8 +69,13 @@ module Mj
63
69
  candidates = candidates.existing
64
70
  end
65
71
 
66
- candidates.unique
72
+ unless options[:debug]
73
+ candidates = candidates.unique
74
+ end
75
+
76
+ candidates
67
77
  end
78
+ # rubocop:enable Metrics/MethodLength
68
79
  end
69
80
  end
70
81
  end
data/lib/mj/cli.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "thor"
4
+ require_relative "version"
4
5
  require_relative "alternative_file/thor_command"
5
6
 
6
7
  module Mj
@@ -9,7 +10,12 @@ module Mj
9
10
  true
10
11
  end
11
12
 
12
- desc "alternative_file", "lists alternative files"
13
+ desc "version", "Prints the version"
14
+ def version
15
+ puts Mj::VERSION
16
+ end
17
+
18
+ desc "alternative_file", "Lists alternative files"
13
19
  subcommand "alternative_file", AlternativeFile::ThorCommand
14
20
  end
15
21
  end
data/lib/mj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mj
4
- VERSION = "0.1.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-11 00:00:00.000000000 Z
11
+ date: 2022-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -53,6 +53,7 @@ files:
53
53
  - lib/mj/alternative_file/resolvers/ruby/rails_controller_resolver.rb
54
54
  - lib/mj/alternative_file/resolvers/ruby/rails_resolver.rb
55
55
  - lib/mj/alternative_file/resolvers/ruby/ruby_file.rb
56
+ - lib/mj/alternative_file/resolvers/ruby/view_component_resolver.rb
56
57
  - lib/mj/alternative_file/thor_command.rb
57
58
  - lib/mj/cli.rb
58
59
  - lib/mj/version.rb