leftovers 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +1 -0
  4. data/docs/Configuration.md +20 -57
  5. data/docs/Custom-Precompilers.md +38 -0
  6. data/lib/config/actionpack.yml +5 -3
  7. data/lib/config/haml.yml +4 -2
  8. data/lib/config/ruby.yml +4 -15
  9. data/lib/config/slim.yml +4 -2
  10. data/lib/leftovers/cli.rb +5 -0
  11. data/lib/leftovers/collector.rb +2 -1
  12. data/lib/leftovers/config.rb +2 -18
  13. data/lib/leftovers/config_loader/argument_position_schema.rb +2 -2
  14. data/lib/leftovers/config_loader/attribute.rb +36 -10
  15. data/lib/leftovers/config_loader/built_in_precompiler_schema.rb +13 -0
  16. data/lib/leftovers/config_loader/document_schema.rb +32 -5
  17. data/lib/leftovers/config_loader/has_argument_schema.rb +2 -2
  18. data/lib/leftovers/config_loader/has_value_schema.rb +2 -2
  19. data/lib/leftovers/config_loader/inherit_schema_attributes.rb +22 -0
  20. data/lib/leftovers/config_loader/keep_test_only_schema.rb +2 -2
  21. data/lib/leftovers/config_loader/object_schema.rb +33 -105
  22. data/lib/leftovers/config_loader/precompile_schema.rb +12 -0
  23. data/lib/leftovers/config_loader/precompiler_schema.rb +11 -0
  24. data/lib/leftovers/config_loader/privacy_processor_schema.rb +0 -2
  25. data/lib/leftovers/config_loader/require_schema.rb +3 -3
  26. data/lib/leftovers/config_loader/schema.rb +2 -0
  27. data/lib/leftovers/config_loader/string_pattern_schema.rb +2 -2
  28. data/lib/leftovers/config_loader/string_value_processor_schema.rb +2 -2
  29. data/lib/leftovers/config_loader/transform_schema.rb +4 -6
  30. data/lib/leftovers/config_loader/value_matcher_schema.rb +2 -2
  31. data/lib/leftovers/config_loader/value_or_array_schema.rb +5 -3
  32. data/lib/leftovers/config_loader/value_or_object_schema.rb +40 -0
  33. data/lib/leftovers/config_loader/value_processor_schema.rb +2 -2
  34. data/lib/leftovers/config_loader.rb +5 -1
  35. data/lib/leftovers/file.rb +7 -53
  36. data/lib/leftovers/file_collector/comments_processor.rb +57 -0
  37. data/lib/leftovers/file_collector/node_processor.rb +131 -0
  38. data/lib/leftovers/file_collector.rb +42 -203
  39. data/lib/leftovers/matcher_builders/and_not.rb +7 -5
  40. data/lib/leftovers/matcher_builders/name.rb +18 -17
  41. data/lib/leftovers/matcher_builders/node.rb +48 -34
  42. data/lib/leftovers/matcher_builders/node_has_argument.rb +48 -52
  43. data/lib/leftovers/matcher_builders/node_has_keyword_argument.rb +13 -10
  44. data/lib/leftovers/matcher_builders/node_has_positional_argument.rb +29 -15
  45. data/lib/leftovers/matcher_builders/node_type.rb +1 -1
  46. data/lib/leftovers/matcher_builders/node_value.rb +27 -22
  47. data/lib/leftovers/matcher_builders/or.rb +50 -50
  48. data/lib/leftovers/matcher_builders/string_pattern.rb +1 -1
  49. data/lib/leftovers/merged_config.rb +3 -23
  50. data/lib/leftovers/precompilers/erb.rb +22 -0
  51. data/lib/leftovers/precompilers/haml.rb +15 -0
  52. data/lib/leftovers/precompilers/json.rb +27 -0
  53. data/lib/leftovers/precompilers/precompiler.rb +28 -0
  54. data/lib/leftovers/precompilers/slim.rb +15 -0
  55. data/lib/leftovers/precompilers/yaml.rb +75 -0
  56. data/lib/leftovers/precompilers.rb +50 -0
  57. data/lib/leftovers/processor_builders/action.rb +48 -39
  58. data/lib/leftovers/processor_builders/add_prefix.rb +1 -1
  59. data/lib/leftovers/processor_builders/add_suffix.rb +1 -1
  60. data/lib/leftovers/processor_builders/argument.rb +8 -11
  61. data/lib/leftovers/processor_builders/dynamic.rb +5 -5
  62. data/lib/leftovers/processor_builders/each.rb +2 -2
  63. data/lib/leftovers/processor_builders/each_action.rb +33 -33
  64. data/lib/leftovers/processor_builders/each_dynamic.rb +4 -8
  65. data/lib/leftovers/processor_builders/each_for_definition_set.rb +25 -21
  66. data/lib/leftovers/processor_builders/keyword.rb +3 -4
  67. data/lib/leftovers/processor_builders/transform.rb +2 -2
  68. data/lib/leftovers/processor_builders/transform_chain.rb +16 -8
  69. data/lib/leftovers/processor_builders/transform_set.rb +32 -28
  70. data/lib/leftovers/rake_task.rb +1 -1
  71. data/lib/leftovers/value_processors/add_dynamic_prefix.rb +3 -10
  72. data/lib/leftovers/value_processors/add_dynamic_suffix.rb +3 -10
  73. data/lib/leftovers/value_processors/each.rb +1 -1
  74. data/lib/leftovers/value_processors/each_for_definition_set.rb +2 -5
  75. data/lib/leftovers/value_processors/each_keyword.rb +1 -1
  76. data/lib/leftovers/value_processors/each_keyword_argument.rb +1 -1
  77. data/lib/leftovers/value_processors/each_positional_argument.rb +2 -1
  78. data/lib/leftovers/value_processors/keyword.rb +3 -7
  79. data/lib/leftovers/value_processors/keyword_argument.rb +2 -6
  80. data/lib/leftovers/value_processors/split.rb +2 -2
  81. data/lib/leftovers/version.rb +1 -1
  82. data/lib/leftovers.rb +41 -6
  83. metadata +17 -7
  84. data/lib/leftovers/erb.rb +0 -20
  85. data/lib/leftovers/haml.rb +0 -21
  86. data/lib/leftovers/json.rb +0 -28
  87. data/lib/leftovers/slim.rb +0 -21
  88. data/lib/leftovers/yaml.rb +0 -73
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Leftovers
4
+ module Precompilers
5
+ autoload(:ERB, "#{__dir__}/precompilers/erb")
6
+ autoload(:Haml, "#{__dir__}/precompilers/haml")
7
+ autoload(:JSON, "#{__dir__}/precompilers/json")
8
+ autoload(:Precompiler, "#{__dir__}/precompilers/precompiler")
9
+ autoload(:Slim, "#{__dir__}/precompilers/slim")
10
+ autoload(:YAML, "#{__dir__}/precompilers/yaml")
11
+
12
+ class << self
13
+ def build(precompilers)
14
+ precompilers.group_by { |p| build_precompiler(p[:format]) }.map do |format, precompiler|
15
+ Precompiler.new(
16
+ format,
17
+ Leftovers::MatcherBuilders::Path.build(precompiler.flat_map { |p| p[:paths] })
18
+ )
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def build_precompiler(format)
25
+ case format
26
+ when 'erb' then ::Leftovers::Precompilers::ERB
27
+ when 'haml' then ::Leftovers::Precompilers::Haml
28
+ when 'json' then ::Leftovers::Precompilers::JSON
29
+ when 'slim' then ::Leftovers::Precompilers::Slim
30
+ when 'yaml' then ::Leftovers::Precompilers::YAML
31
+ when Hash then constantize_precompiler(format[:custom])
32
+ # :nocov:
33
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{format}"
34
+ # :nocov:
35
+ end
36
+ end
37
+
38
+ def constantize_precompiler(precompiler)
39
+ precompiler = "::#{precompiler}" unless precompiler.start_with?('::')
40
+
41
+ Object.const_get(precompiler, false)
42
+ rescue ::NameError
43
+ Leftovers.error <<~MESSAGE
44
+ Tried using #{precompiler}, but it wasn't available.
45
+ add its path to `requires:` in your .leftovers.yml
46
+ MESSAGE
47
+ end
48
+ end
49
+ end
50
+ end
@@ -3,60 +3,69 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module Action
6
- def self.build(patterns, action)
7
- ::Leftovers::ProcessorBuilders::EachAction.each_or_self(patterns) do |pattern|
8
- case pattern
9
- when ::String, ::Integer
10
- ::Leftovers::ProcessorBuilders::Argument.build(pattern, final_transformer(action))
11
- when ::Hash
12
- build_from_hash_value(action, **pattern)
13
- # :nocov:
14
- else raise
6
+ class << self
7
+ def build(patterns, action)
8
+ ::Leftovers::ProcessorBuilders::EachAction.each_or_self(patterns) do |pattern|
9
+ case pattern
10
+ when ::String, ::Integer
11
+ ::Leftovers::ProcessorBuilders::Argument.build(pattern, final_transformer(action))
12
+ when ::Hash then build_from_hash_value(action, **pattern)
15
13
  # :nocov:
14
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{pattern.inspect}"
15
+ # :nocov:
16
+ end
16
17
  end
17
18
  end
18
- end
19
19
 
20
- def self.final_transformer(action)
21
- ::Leftovers::ProcessorBuilders::TransformSet.build_final(action)
22
- end
20
+ private
23
21
 
24
- def self.build_from_hash_value( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
25
- action,
26
- arguments: nil,
27
- keywords: nil,
28
- itself: nil,
29
- value: nil,
30
- nested: nil,
31
- recursive: nil,
32
- **transform_args
33
- )
34
- transformer = ::Leftovers::ProcessorBuilders::TransformSet.build(transform_args, action)
35
- if nested
36
- transformer = ::Leftovers::ProcessorBuilders::Each.build([
22
+ def final_transformer(action)
23
+ ::Leftovers::ProcessorBuilders::TransformSet.build_final(action)
24
+ end
25
+
26
+ def build_nested(nested, transformer, action)
27
+ ::Leftovers::ProcessorBuilders::Each.build([
37
28
  ::Leftovers::ProcessorBuilders::Action.build(nested, action),
38
29
  transformer
39
30
  ])
40
31
  end
41
32
 
42
- if recursive
33
+ def build_processor(arguments, keywords, itself, value, transformer)
34
+ ::Leftovers::ProcessorBuilders::EachAction.build([
35
+ ::Leftovers::ProcessorBuilders::Argument.build(arguments, transformer),
36
+ ::Leftovers::ProcessorBuilders::Keyword.build(keywords, transformer),
37
+ ::Leftovers::ProcessorBuilders::Itself.build(itself, transformer),
38
+ ::Leftovers::ProcessorBuilders::Value.build(value, transformer)
39
+ ])
40
+ end
41
+
42
+ def build_recursive(transformer)
43
43
  placeholder = ::Leftovers::ValueProcessors::Placeholder.new
44
- transformer = ::Leftovers::ProcessorBuilders::Each.build(
45
- [placeholder, transformer]
46
- )
44
+ transformer = ::Leftovers::ProcessorBuilders::Each.build([placeholder, transformer])
45
+
46
+ [placeholder, transformer]
47
47
  end
48
48
 
49
- processor = ::Leftovers::ProcessorBuilders::EachAction.build([
50
- ::Leftovers::ProcessorBuilders::Argument.build(arguments, transformer),
51
- ::Leftovers::ProcessorBuilders::Keyword.build(keywords, transformer),
52
- ::Leftovers::ProcessorBuilders::Itself.build(itself, transformer),
53
- ::Leftovers::ProcessorBuilders::Value.build(value, transformer)
54
- ])
49
+ def build_from_hash_value( # rubocop:disable Metrics/ParameterLists
50
+ action,
51
+ arguments: nil,
52
+ keywords: nil,
53
+ itself: nil,
54
+ value: nil,
55
+ nested: nil,
56
+ recursive: nil,
57
+ **transform_args
58
+ )
59
+ transformer = ::Leftovers::ProcessorBuilders::TransformSet.build(transform_args, action)
60
+ transformer = build_nested(nested, transformer, action) if nested
61
+ placeholder, transformer = build_recursive(transformer) if recursive
62
+ processor = build_processor(arguments, keywords, itself, value, transformer)
55
63
 
56
- return processor unless recursive
64
+ return processor unless recursive
57
65
 
58
- placeholder.processor = processor
59
- placeholder
66
+ placeholder.processor = processor
67
+ placeholder
68
+ end
60
69
  end
61
70
  end
62
71
  end
@@ -11,7 +11,7 @@ module Leftovers
11
11
  when ::String
12
12
  ::Leftovers::ValueProcessors::AddPrefix.new(argument, then_processor)
13
13
  # :nocov:
14
- else raise
14
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{argument.inspect}"
15
15
  # :nocov:
16
16
  end
17
17
  end
@@ -11,7 +11,7 @@ module Leftovers
11
11
  when ::String
12
12
  ::Leftovers::ValueProcessors::AddSuffix.new(argument, then_processor)
13
13
  # :nocov:
14
- else raise
14
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{argument.inspect}"
15
15
  # :nocov:
16
16
  end
17
17
  end
@@ -3,19 +3,16 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module Argument
6
- def self.build(patterns, then_processor) # rubocop:disable Metrics/MethodLength
7
- ::Leftovers::ProcessorBuilders::EachAction.each_or_self(patterns) do |pattern|
8
- case pattern
9
- when ::Integer
10
- ::Leftovers::ValueProcessors::PositionalArgument.new(pattern, then_processor)
11
- when '*'
12
- ::Leftovers::ValueProcessors::EachPositionalArgument.new(then_processor)
13
- when '**'
14
- ::Leftovers::ValueProcessors::EachKeywordArgument.new(then_processor)
6
+ def self.build(patterns, processor)
7
+ ::Leftovers::ProcessorBuilders::EachAction.each_or_self(patterns) do |pat|
8
+ case pat
9
+ when ::Integer then ::Leftovers::ValueProcessors::PositionalArgument.new(pat, processor)
10
+ when '*' then ::Leftovers::ValueProcessors::EachPositionalArgument.new(processor)
11
+ when '**' then ::Leftovers::ValueProcessors::EachKeywordArgument.new(processor)
15
12
  when ::String, ::Hash
16
- ::Leftovers::ProcessorBuilders::KeywordArgument.build(pattern, then_processor)
13
+ ::Leftovers::ProcessorBuilders::KeywordArgument.build(pat, processor)
17
14
  # :nocov:
18
- else raise
15
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{pat.inspect}"
19
16
  # :nocov:
20
17
  end
21
18
  end
@@ -17,8 +17,11 @@ module Leftovers
17
17
  )
18
18
  matcher = ::Leftovers::MatcherBuilders::Node.build(**matcher_rules)
19
19
 
20
+ call_action = build_action(call, return_type: :sym)
21
+ define_action = build_action(define, return_type: :definition_node)
22
+
20
23
  ::Leftovers::ProcessorBuilders::EachDynamic.build([
21
- build_call_define_processor(matcher, call, define),
24
+ build_call_define_processor(matcher, call_action, define_action),
22
25
  build_set_privacy_processor(matcher, set_privacy),
23
26
  build_set_default_privacy_processor(matcher, set_default_privacy)
24
27
  ])
@@ -41,10 +44,7 @@ module Leftovers
41
44
  ::Leftovers::DynamicProcessors::SetDefaultPrivacy.new(matcher, set_default_privacy)
42
45
  end
43
46
 
44
- def build_call_define_processor(matcher, call, define) # rubocop:disable Metrics/MethodLength
45
- call_action = build_action(call, return_type: :sym)
46
- define_action = build_action(define, return_type: :definition_node)
47
-
47
+ def build_call_define_processor(matcher, call_action, define_action)
48
48
  if call_action && define_action
49
49
  # this nonsense saves a method call and array instantiation per method
50
50
  ::Leftovers::DynamicProcessors::CallDefinition.new(matcher, call_action, define_action)
@@ -6,7 +6,7 @@ module Leftovers
6
6
  def self.each_or_self(value, &block)
7
7
  case value
8
8
  # :nocov:
9
- when nil then raise
9
+ when nil then raise Leftovers::UnexpectedCase, "Unhandled value #{value.inspect}"
10
10
  # :nocov:
11
11
  when Array then build(value.map(&block))
12
12
  else build([yield(value)])
@@ -18,7 +18,7 @@ module Leftovers
18
18
 
19
19
  case processors.length
20
20
  # :nocov:
21
- when 0 then raise
21
+ when 0 then raise Leftovers::UnexpectedCase, "Unhandled value #{processors.inspect}"
22
22
  # :nocov:
23
23
  when 1 then processors.first
24
24
  else ::Leftovers::ValueProcessors::Each.new(processors)
@@ -3,48 +3,48 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module EachAction
6
- def self.each_or_self(value, &block)
7
- case value
8
- when nil then nil
9
- when Array then build(value.map(&block))
10
- else build([yield(value)])
6
+ class << self
7
+ def each_or_self(value, &block)
8
+ case value
9
+ when nil then nil
10
+ when Array then build(value.map(&block))
11
+ else build([yield(value)])
12
+ end
11
13
  end
12
- end
13
14
 
14
- def self.build(processors)
15
- processors = compact(processors)
15
+ def build(processors)
16
+ processors = compact(processors)
16
17
 
17
- case processors.length
18
- # :nocov:
19
- when 0 then raise
20
- # :nocov:
21
- when 1 then processors.first
22
- else ::Leftovers::ValueProcessors::Each.new(processors)
18
+ case processors.length
19
+ # :nocov:
20
+ when 0 then raise Leftovers::UnexpectedCase, "Unhandled value #{processors.inspect}"
21
+ # :nocov:
22
+ when 1 then processors.first
23
+ else ::Leftovers::ValueProcessors::Each.new(processors)
24
+ end
23
25
  end
24
- end
25
26
 
26
- def self.flatten(value) # rubocop:disable Metrics/MethodLength
27
- case value
28
- when ::Leftovers::ValueProcessors::Each
29
- ret = value.processors.map { |v| flatten(v) }
30
- ret.flatten!(1)
31
- ret
32
- when Array
33
- ret = value.map { |v| flatten(v) }
34
- ret.flatten!(1)
35
- ret
36
- else
37
- value
27
+ private
28
+
29
+ def flatten(value)
30
+ case value
31
+ when ::Leftovers::ValueProcessors::Each
32
+ value.processors.flat_map { |v| flatten(v) }
33
+ when Array
34
+ value.flat_map { |v| flatten(v) }
35
+ else
36
+ value
37
+ end
38
38
  end
39
- end
40
39
 
41
- def self.compact(processors)
42
- return processors if processors.length <= 1
40
+ def compact(processors)
41
+ return processors if processors.length <= 1
43
42
 
44
- processors = flatten(processors)
45
- processors.compact!
43
+ processors = flatten(processors)
44
+ processors.compact!
46
45
 
47
- processors
46
+ processors
47
+ end
48
48
  end
49
49
  end
50
50
  end
@@ -16,23 +16,19 @@ module Leftovers
16
16
 
17
17
  case processors.length
18
18
  # :nocov:
19
- when 0 then raise
19
+ when 0 then raise Leftovers::UnexpectedCase, "Unhandled value #{processors.inspect}"
20
20
  # :nocov:
21
21
  when 1 then processors.first
22
22
  else ::Leftovers::DynamicProcessors::Each.new(processors)
23
23
  end
24
24
  end
25
25
 
26
- def self.flatten(value) # rubocop:disable Metrics/MethodLength
26
+ def self.flatten(value)
27
27
  case value
28
28
  when ::Leftovers::DynamicProcessors::Each
29
- ret = value.processors.map { |v| flatten(v) }
30
- ret.flatten!(1)
31
- ret
29
+ value.processors.flat_map { |v| flatten(v) }
32
30
  when Array
33
- ret = value.map { |v| flatten(v) }
34
- ret.flatten!(1)
35
- ret
31
+ value.flat_map { |v| flatten(v) }
36
32
  else
37
33
  value
38
34
  end
@@ -3,33 +3,37 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module EachForDefinitionSet
6
- def self.each_or_self(value, &block)
7
- case value
8
- # :nocov:
9
- when nil then raise
10
- # :nocov:
11
- when Array then build(value.map(&block))
12
- else build([yield(value)])
6
+ class << self
7
+ def each_or_self(value, &block)
8
+ case value
9
+ # :nocov:
10
+ when nil then raise Leftovers::UnexpectedCase, "Unhandled value #{value.inspect}"
11
+ # :nocov:
12
+ when Array then build(value.map(&block))
13
+ else build([yield(value)])
14
+ end
13
15
  end
14
- end
15
16
 
16
- def self.build(processors)
17
- processors = compact(processors)
17
+ def build(processors)
18
+ processors = compact(processors)
18
19
 
19
- case processors.length
20
- # :nocov:
21
- when 0 then raise
22
- # :nocov:
23
- when 1 then processors.first
24
- else ::Leftovers::ValueProcessors::EachForDefinitionSet.new(processors)
20
+ case processors.length
21
+ # :nocov:
22
+ when 0 then raise Leftovers::UnexpectedCase, "Unhandled value #{processors.inspect}"
23
+ # :nocov:
24
+ when 1 then processors.first
25
+ else ::Leftovers::ValueProcessors::EachForDefinitionSet.new(processors)
26
+ end
25
27
  end
26
- end
27
28
 
28
- def self.compact(processors)
29
- processors.flatten!
30
- processors.compact!
29
+ private
31
30
 
32
- processors
31
+ def compact(processors)
32
+ processors.flatten!
33
+ processors.compact!
34
+
35
+ processors
36
+ end
33
37
  end
34
38
  end
35
39
  end
@@ -3,19 +3,18 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module Keyword
6
- def self.build(value, then_processor) # rubocop:disable Metrics/MethodLength
6
+ def self.build(value, then_processor)
7
7
  return unless value && then_processor
8
8
 
9
9
  case value
10
- when true, '**'
11
- ::Leftovers::ValueProcessors::EachKeyword.new(then_processor)
10
+ when true, '**' then ::Leftovers::ValueProcessors::EachKeyword.new(then_processor)
12
11
  when ::String, ::Hash, ::Array
13
12
  ::Leftovers::ValueProcessors::Keyword.new(
14
13
  ::Leftovers::MatcherBuilders::NodePairName.build(value),
15
14
  then_processor
16
15
  )
17
16
  # :nocov:
18
- else raise
17
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{value.inspect}"
19
18
  # :nocov:
20
19
  end
21
20
  end
@@ -3,7 +3,7 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module Transform
6
- def self.build(transform, argument, then_processor) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/AbcSize
6
+ def self.build(transform, argument, then_processor) # rubocop:disable Metrics
7
7
  case transform.to_s
8
8
  when 'original', nil
9
9
  then_processor
@@ -46,7 +46,7 @@ module Leftovers
46
46
  when 'delete_suffix'
47
47
  ::Leftovers::ValueProcessors::DeleteSuffix.new(argument, then_processor)
48
48
  # :nocov:
49
- else raise
49
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{transform.to_s.inspect}"
50
50
  # :nocov:
51
51
  end
52
52
  end
@@ -3,20 +3,28 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module TransformChain
6
- def self.build(transforms, next_transform) # rubocop:disable Metrics/MethodLength
7
- case transforms
8
- when ::Hash
6
+ class << self
7
+ def build(transforms, next_transform)
8
+ case transforms
9
+ when ::Hash then build_from_hash(transforms, next_transform)
10
+ when ::String
11
+ ::Leftovers::ProcessorBuilders::Transform.build(transforms, true, next_transform)
12
+ # :nocov:
13
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{transforms.inspect}"
14
+ # :nocov:
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def build_from_hash(transforms, next_transform)
9
21
  transforms.reverse_each do |(transform, transform_arg)|
10
22
  next_transform = ::Leftovers::ProcessorBuilders::Transform.build(
11
23
  transform, transform_arg, next_transform
12
24
  )
13
25
  end
26
+
14
27
  next_transform
15
- when ::String
16
- ::Leftovers::ProcessorBuilders::Transform.build(transforms, true, next_transform)
17
- # :nocov:
18
- else raise
19
- # :nocov:
20
28
  end
21
29
  end
22
30
  end
@@ -3,43 +3,47 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module TransformSet
6
- def self.build(transforms, action)
7
- each_builder(action).each_or_self(transforms) do |transform|
8
- case transform
9
- when ::Hash
10
- next build(transform[:transforms], action) if transform[:transforms]
6
+ class << self
7
+ def build(transforms, action)
8
+ each_builder(action).each_or_self(transforms) do |transform|
9
+ case transform
10
+ when ::Hash
11
+ next build(transform[:transforms], action) if transform[:transforms]
11
12
 
12
- ::Leftovers::ProcessorBuilders::TransformChain.build(transform, build_final(action))
13
- when ::String
14
- ::Leftovers::ProcessorBuilders::TransformChain.build(transform, build_final(action))
15
- # :nocov:
16
- else raise
13
+ ::Leftovers::ProcessorBuilders::TransformChain.build(transform, build_final(action))
14
+ when ::String
15
+ ::Leftovers::ProcessorBuilders::TransformChain.build(transform, build_final(action))
17
16
  # :nocov:
17
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{transform.inspect}"
18
+ # :nocov:
19
+ end
18
20
  end
19
21
  end
20
- end
21
22
 
22
- def self.each_builder(action)
23
- case action
24
- when :sym
25
- ::Leftovers::ProcessorBuilders::Each
26
- when :definition_node
27
- ::Leftovers::ProcessorBuilders::EachForDefinitionSet
28
- # :nocov:
29
- else raise "Unknown action #{action}"
23
+ def build_final(action)
24
+ case action
25
+ when :sym
26
+ ::Leftovers::ValueProcessors::ReturnSym
27
+ when :definition_node
28
+ ::Leftovers::ValueProcessors::ReturnDefinitionNode
30
29
  # :nocov:
30
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{action.inspect}"
31
+ # :nocov:
32
+ end
31
33
  end
32
- end
33
34
 
34
- def self.build_final(action)
35
- case action
36
- when :sym
37
- ::Leftovers::ValueProcessors::ReturnSym
38
- when :definition_node
39
- ::Leftovers::ValueProcessors::ReturnDefinitionNode
40
- # :nocov:
41
- else raise "Unknown action #{action}"
35
+ private
36
+
37
+ def each_builder(action)
38
+ case action
39
+ when :sym
40
+ ::Leftovers::ProcessorBuilders::Each
41
+ when :definition_node
42
+ ::Leftovers::ProcessorBuilders::EachForDefinitionSet
42
43
  # :nocov:
44
+ else raise Leftovers::UnexpectedCase, "Unhandled value #{action.inspect}"
45
+ # :nocov:
46
+ end
43
47
  end
44
48
  end
45
49
  end
@@ -8,7 +8,7 @@ module Leftovers
8
8
  class RakeTask
9
9
  include ::Rake::DSL
10
10
 
11
- def self.generate_task(name = :leftovers, *default_argv) # rubocop:disable
11
+ def self.generate_task(name = :leftovers, *default_argv)
12
12
  new(name, default_argv)
13
13
 
14
14
  name
@@ -10,20 +10,13 @@ module Leftovers
10
10
  freeze
11
11
  end
12
12
 
13
- def process(str, node, method_node) # rubocop:disable Metrics/MethodLength
13
+ def process(str, node, method_node)
14
14
  return unless str
15
15
 
16
16
  prefixes = @prefix_processor.process(nil, method_node, method_node)
17
- if prefixes.is_a?(Array)
18
- prefixes.flatten!
19
- prefixes.compact!
20
- prefixes.uniq!
21
17
 
22
- prefixes.map do |prefix|
23
- @then_processor.process("#{prefix}#{str}", node, method_node)
24
- end
25
- else
26
- @then_processor.process("#{prefixes}#{str}", node, method_node)
18
+ Leftovers.map_or_self(prefixes) do |prefix|
19
+ @then_processor.process("#{prefix}#{str}", node, method_node)
27
20
  end
28
21
  end
29
22
  end
@@ -10,20 +10,13 @@ module Leftovers
10
10
  freeze
11
11
  end
12
12
 
13
- def process(str, node, method_node) # rubocop:disable Metrics/MethodLength
13
+ def process(str, node, method_node)
14
14
  return unless str
15
15
 
16
16
  suffixes = @suffix_processor.process(nil, method_node, method_node)
17
- if suffixes.is_a?(Array)
18
- suffixes.flatten!
19
- suffixes.compact!
20
- suffixes.uniq!
21
17
 
22
- suffixes.map do |suffix|
23
- @then_processor.process("#{str}#{suffix}", node, method_node)
24
- end
25
- else
26
- @then_processor.process("#{str}#{suffixes}", node, method_node)
18
+ Leftovers.map_or_self(suffixes) do |suffix|
19
+ @then_processor.process("#{str}#{suffix}", node, method_node)
27
20
  end
28
21
  end
29
22
  end
@@ -12,7 +12,7 @@ module Leftovers
12
12
  end
13
13
 
14
14
  def process(str, node, method_node)
15
- @processors.flat_map do |processor|
15
+ Leftovers.map_or_self(@processors) do |processor|
16
16
  processor.process(str, node, method_node)
17
17
  end
18
18
  end