mj 0.2.0 → 0.4.1

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: 1315dc125dadf7c6af6dfdaba6b81c52afd1aea322676a050d01ff6b3af6cd12
4
- data.tar.gz: 975b749f8c67d2e9380c6b4df17b457e9e88f04ebbb3e74539b27b9816cb17c2
3
+ metadata.gz: ff905fc5978244944098033bf6ecfb671f1130c336e2f77c19beefab3c927dfe
4
+ data.tar.gz: 0560a8a156cb45bc2e8c459b021e721118e304abf55e92d25039cd9c9277ee24
5
5
  SHA512:
6
- metadata.gz: 7dc77ebcfe8f3b8b02972b027cba9a820f3efade191fd925ed53d25ad3500eba4ce17054028fe828575a23b12696205cb8933dd2a26bb2d81648d2dd7355302c
7
- data.tar.gz: 63b69114fa66a7ccfdbe81f3d4e10a3d8bf873adef015a76e7f7e460e2df1ab630f949f14b6d5e7ff0bd031f43192f2e813fa06878cc547e12f8b44705554af6
6
+ metadata.gz: b20134878fb83b50a0d9dda518dfc81500af893f522616605835e1ee2f6d78bbe824a9302d07131926d799bf308ee463efbfa9a0b4d37dce793fd2ab614c91d8
7
+ data.tar.gz: 30f12048aa54fa5070895859a99f0ffdd66bf35962d82e98952fc889b8a23afd9f4b650a89bea6645f28434210b173d90460196ab20a72f93fe68368cc50d87d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mj (0.2.0)
4
+ mj (0.4.1)
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", "spec"))
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,41 @@
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?(
12
+ "component.rb",
13
+ "component.html.erb",
14
+ "component_test.rb",
15
+ "component_spec.rb"
16
+ )
17
+ end
18
+
19
+ def add_candidates(file, candidates)
20
+ if file.extension == "rb"
21
+ return resolve_template(file, candidates)
22
+ end
23
+
24
+ resolve_component_class(file, candidates)
25
+ end
26
+
27
+ def resolve_template(file, candidates)
28
+ file = file.sub(/_component(_test|_spec)?.rb$/, "_component.html.erb")
29
+ file = file.sub(/^(spec|test)/, "app")
30
+ add_candidate(file, "component_template", to: candidates)
31
+ end
32
+
33
+ def resolve_component_class(file, candidates)
34
+ file_name = file.sub(/_component.html.erb$/, "_component.rb")
35
+ add_candidate(file_name, "component_class", to: candidates)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ 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.2.0"
4
+ VERSION = "0.4.1"
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.2.0
4
+ version: 0.4.1
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-13 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