mj 0.3.0 → 0.4.2

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: 45d956a04733940240dd116cbd23f2bce4f7489d98954b6eebf4bef7ad650265
4
- data.tar.gz: bfd4982a65d4794f54c662fb62aed6952f35af192708984462aa286522a273df
3
+ metadata.gz: 79b064c1e86b2c881657f35989e852b5000e4677a1abf4636b2473974a5e115f
4
+ data.tar.gz: 165f134cfe2c427d6d96314f0bda0c73dfce38d93fd777cdd60740c65314438f
5
5
  SHA512:
6
- metadata.gz: 3f7d3cbda48c13aa55f08da78e2d072c7c70322c2d53fd6048e68885acda47b969bf36634c4e41d8fcd98f809821fcd0ab6c3f8a0e8b5d2be35d1bbca4c3e141
7
- data.tar.gz: 3e51c76e04d0dafed9c49ec6a548dbc6647727cde8fe930e74cff81eca818a67d8bf9b657717be1b55f9979eb29f814dfdd533c8b919492ffe188d9fcc9cd036
6
+ metadata.gz: e8337127e6a696efca4c24b673e3e1f5fea79886a9946ef37173683315701c7bb2c6644a1ae77d3cd55efc7717eb36efb80a1312c5db274fdfe8abe66aba289e
7
+ data.tar.gz: 538a81a2d58fe8bdfa85ec60409868526479cb6b6016b965f264a105b467ec9120d87c58efdf0260e1de9f6ae3ac3102c01eb432e0c098cc8ef7ee9bdf2982ff
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mj (0.3.0)
4
+ mj (0.4.2)
5
5
  thor (~> 1.2.1)
6
6
 
7
7
  GEM
@@ -41,6 +41,10 @@ module Mj
41
41
  next_file(@items.reverse, reference_file)
42
42
  end
43
43
 
44
+ def sorted_by_path
45
+ new(@items.sort_by(&:path))
46
+ end
47
+
44
48
  private
45
49
 
46
50
  def new(*args)
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Commands
6
+ class ListCommand
7
+ attr_reader :file
8
+
9
+ def initialize(file, options)
10
+ @file = CurrentFile.new(file.to_s)
11
+ @options = options
12
+ end
13
+
14
+ def exists?
15
+ @options[:exists]
16
+ end
17
+
18
+ def types
19
+ @options[:types].to_s.split(",")
20
+ end
21
+
22
+ def debug?
23
+ @options[:debug]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mj
4
+ module AlternativeFile
5
+ module Commands
6
+ class ListCommandHandler
7
+ def initialize(resolvers:)
8
+ @resolvers = resolvers
9
+ end
10
+
11
+ # rubocop:disable Metrics/MethodLength
12
+ def handle(command)
13
+ candidates = @resolvers.resolve(command.file)
14
+
15
+ if command.types.any?
16
+ candidates = candidates.of_types(command.types)
17
+ end
18
+
19
+ if command.exists?
20
+ candidates = candidates.existing
21
+ end
22
+
23
+ unless command.debug?
24
+ candidates = candidates.unique
25
+ end
26
+
27
+ candidates.sorted_by_path
28
+ end
29
+ # rubocop:enable Metrics/MethodLength
30
+ end
31
+ end
32
+ end
33
+ 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,19 +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, metadata: {
19
- resolved_by: self.class.name
20
- })
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)
21
25
  end
22
26
 
23
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,14 +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
19
 
20
20
  # lib files
21
- alternatives.push(create_candidate("lib/#{ruby_file.class_path}.rb", "lib"))
21
+ add_candidate("lib/#{ruby_file.class_path}.rb", "lib", to: candidates)
22
22
  end
23
23
  end
24
24
  end
@@ -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,7 +8,10 @@ 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"
13
+ require_relative "commands/list_command_handler"
14
+ require_relative "commands/list_command"
12
15
 
13
16
  module Mj
14
17
  module AlternativeFile
@@ -18,8 +21,10 @@ module Mj
18
21
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
19
22
  option :debug, type: :boolean, banner: "print debug information", aliases: :d
20
23
  def list(reference_file)
21
- file = CurrentFile.new(reference_file)
22
- print_candidates(resolve(file))
24
+ handler = Commands::ListCommandHandler.new(resolvers: self.class.resolvers)
25
+ command = Commands::ListCommand.new(reference_file, options)
26
+ candidates = handler.handle(command)
27
+ print_candidates(candidates)
23
28
  end
24
29
 
25
30
  desc "next <reference-file>", "Next alternative file"
@@ -27,8 +32,10 @@ module Mj
27
32
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
28
33
  option :debug, type: :boolean, banner: "print debug information", aliases: :d
29
34
  def next(reference_file)
30
- file = CurrentFile.new(reference_file)
31
- candidate = resolve(file).after(file)
35
+ handler = Commands::ListCommandHandler.new(resolvers: self.class.resolvers)
36
+ command = Commands::ListCommand.new(reference_file, options)
37
+ candidates = handler.handle(command)
38
+ candidate = candidates.after(command.file)
32
39
  print_candidates([candidate].compact)
33
40
  end
34
41
 
@@ -37,8 +44,10 @@ module Mj
37
44
  option :exists, type: :boolean, banner: "files that exist", aliases: :e
38
45
  option :debug, type: :boolean, banner: "print debug information", aliases: :d
39
46
  def prev(reference_file)
40
- file = CurrentFile.new(reference_file)
41
- candidate = resolve(file).before(file)
47
+ handler = Commands::ListCommandHandler.new(resolvers: self.class.resolvers)
48
+ command = Commands::ListCommand.new(reference_file, options)
49
+ candidates = handler.handle(command)
50
+ candidate = candidates.before(command.file)
42
51
  print_candidates([candidate].compact)
43
52
  end
44
53
 
@@ -46,6 +55,7 @@ module Mj
46
55
  @resolvers ||= AlternativeFile::Resolver.new.tap do |resolvers|
47
56
  resolvers.add(Resolvers::Ruby::RailsResolver.new)
48
57
  resolvers.add(Resolvers::Ruby::RailsControllerResolver.new)
58
+ resolvers.add(Resolvers::Ruby::ViewComponentResolver.new)
49
59
  end
50
60
  end
51
61
 
@@ -54,26 +64,6 @@ module Mj
54
64
  def print_candidates(candidates)
55
65
  $stdout.puts candidates.map { |i| i.to_s(debug: options[:debug]) }.join(" ")
56
66
  end
57
-
58
- # rubocop:disable Metrics/MethodLength
59
- def resolve(file)
60
- candidates = self.class.resolvers.resolve(file)
61
-
62
- if options[:types]
63
- candidates = candidates.of_types(options[:types].to_s.split(","))
64
- end
65
-
66
- if options[:exists]
67
- candidates = candidates.existing
68
- end
69
-
70
- unless options[:debug]
71
- candidates = candidates.unique
72
- end
73
-
74
- candidates
75
- end
76
- # rubocop:enable Metrics/MethodLength
77
67
  end
78
68
  end
79
69
  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.3.0"
4
+ VERSION = "0.4.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mj
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
@@ -47,12 +47,15 @@ files:
47
47
  - lib/mj.rb
48
48
  - lib/mj/alternative_file/candidate.rb
49
49
  - lib/mj/alternative_file/candidates.rb
50
+ - lib/mj/alternative_file/commands/list_command.rb
51
+ - lib/mj/alternative_file/commands/list_command_handler.rb
50
52
  - lib/mj/alternative_file/current_file.rb
51
53
  - lib/mj/alternative_file/resolver.rb
52
54
  - lib/mj/alternative_file/resolvers/base.rb
53
55
  - lib/mj/alternative_file/resolvers/ruby/rails_controller_resolver.rb
54
56
  - lib/mj/alternative_file/resolvers/ruby/rails_resolver.rb
55
57
  - lib/mj/alternative_file/resolvers/ruby/ruby_file.rb
58
+ - lib/mj/alternative_file/resolvers/ruby/view_component_resolver.rb
56
59
  - lib/mj/alternative_file/thor_command.rb
57
60
  - lib/mj/cli.rb
58
61
  - lib/mj/version.rb