leftovers 0.5.5 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -2
  3. data/README.md +22 -1
  4. data/docs/Configuration.md +173 -42
  5. data/docs/Custom-Precompilers.md +38 -0
  6. data/leftovers.gemspec +8 -5
  7. data/lib/config/actionmailer.yml +11 -11
  8. data/lib/config/actionpack.yml +5 -3
  9. data/lib/config/activesupport.yml +1 -1
  10. data/lib/config/haml.yml +4 -2
  11. data/lib/config/rails.yml +1 -1
  12. data/lib/config/railties.yml +11 -0
  13. data/lib/config/ruby.yml +76 -8
  14. data/lib/config/slim.yml +4 -2
  15. data/lib/leftovers/ast/node.rb +16 -11
  16. data/lib/leftovers/cli.rb +5 -0
  17. data/lib/leftovers/collector.rb +5 -2
  18. data/lib/leftovers/config.rb +3 -38
  19. data/lib/leftovers/config_loader/argument_position_schema.rb +11 -0
  20. data/lib/leftovers/config_loader/argumentless_transform_schema.rb +21 -0
  21. data/lib/leftovers/config_loader/attribute.rb +56 -0
  22. data/lib/leftovers/config_loader/built_in_precompiler_schema.rb +13 -0
  23. data/lib/leftovers/config_loader/document_schema.rb +48 -0
  24. data/lib/leftovers/config_loader/dynamic_schema.rb +17 -0
  25. data/lib/leftovers/config_loader/has_argument_schema.rb +13 -0
  26. data/lib/leftovers/config_loader/has_value_schema.rb +18 -0
  27. data/lib/leftovers/config_loader/inherit_schema_attributes.rb +22 -0
  28. data/lib/leftovers/config_loader/keep_test_only_schema.rb +13 -0
  29. data/lib/leftovers/config_loader/node.rb +106 -0
  30. data/lib/leftovers/config_loader/object_schema.rb +117 -0
  31. data/lib/leftovers/config_loader/precompile_schema.rb +12 -0
  32. data/lib/leftovers/config_loader/precompiler_schema.rb +11 -0
  33. data/lib/leftovers/config_loader/privacy_processor_schema.rb +10 -0
  34. data/lib/leftovers/config_loader/privacy_schema.rb +15 -0
  35. data/lib/leftovers/config_loader/require_schema.rb +11 -0
  36. data/lib/leftovers/config_loader/rule_pattern_schema.rb +18 -0
  37. data/lib/leftovers/config_loader/scalar_argument_schema.rb +14 -0
  38. data/lib/leftovers/config_loader/scalar_value_schema.rb +14 -0
  39. data/lib/leftovers/config_loader/schema.rb +23 -0
  40. data/lib/leftovers/config_loader/string_enum_schema.rb +62 -0
  41. data/lib/leftovers/config_loader/string_pattern_schema.rb +14 -0
  42. data/lib/leftovers/config_loader/string_schema.rb +14 -0
  43. data/lib/leftovers/config_loader/string_value_processor_schema.rb +11 -0
  44. data/lib/leftovers/config_loader/suggester.rb +22 -0
  45. data/lib/leftovers/config_loader/transform_schema.rb +26 -0
  46. data/lib/leftovers/config_loader/true_schema.rb +18 -0
  47. data/lib/leftovers/config_loader/value_matcher_schema.rb +18 -0
  48. data/lib/leftovers/config_loader/value_or_array_schema.rb +66 -0
  49. data/lib/leftovers/config_loader/value_or_object_schema.rb +40 -0
  50. data/lib/leftovers/config_loader/value_processor_schema.rb +14 -0
  51. data/lib/leftovers/config_loader/value_type_schema.rb +17 -0
  52. data/lib/leftovers/config_loader.rb +86 -0
  53. data/lib/leftovers/definition.rb +1 -1
  54. data/lib/leftovers/definition_node.rb +6 -17
  55. data/lib/leftovers/definition_node_set.rb +11 -0
  56. data/lib/leftovers/definition_to_add.rb +31 -0
  57. data/lib/leftovers/dynamic_processors/call.rb +4 -7
  58. data/lib/leftovers/dynamic_processors/call_definition.rb +14 -11
  59. data/lib/leftovers/dynamic_processors/definition.rb +8 -7
  60. data/lib/leftovers/dynamic_processors/set_default_privacy.rb +18 -0
  61. data/lib/leftovers/dynamic_processors/set_privacy.rb +23 -0
  62. data/lib/leftovers/dynamic_processors.rb +2 -0
  63. data/lib/leftovers/file.rb +12 -12
  64. data/lib/leftovers/file_collector/comments_processor.rb +57 -0
  65. data/lib/leftovers/file_collector/node_processor.rb +131 -0
  66. data/lib/leftovers/file_collector.rb +66 -169
  67. data/lib/leftovers/matcher_builders/and_not.rb +7 -5
  68. data/lib/leftovers/matcher_builders/document.rb +13 -0
  69. data/lib/leftovers/matcher_builders/name.rb +18 -17
  70. data/lib/leftovers/matcher_builders/node.rb +48 -28
  71. data/lib/leftovers/matcher_builders/node_has_argument.rb +48 -53
  72. data/lib/leftovers/matcher_builders/node_has_keyword_argument.rb +13 -10
  73. data/lib/leftovers/matcher_builders/node_has_positional_argument.rb +29 -15
  74. data/lib/leftovers/matcher_builders/node_privacy.rb +13 -0
  75. data/lib/leftovers/matcher_builders/node_type.rb +4 -4
  76. data/lib/leftovers/matcher_builders/node_value.rb +28 -23
  77. data/lib/leftovers/matcher_builders/or.rb +50 -50
  78. data/lib/leftovers/matcher_builders/string_pattern.rb +14 -5
  79. data/lib/leftovers/matcher_builders.rb +2 -0
  80. data/lib/leftovers/matchers/all.rb +0 -4
  81. data/lib/leftovers/matchers/and.rb +0 -4
  82. data/lib/leftovers/matchers/any.rb +0 -4
  83. data/lib/leftovers/matchers/node_has_any_keyword_argument.rb +1 -7
  84. data/lib/leftovers/matchers/node_has_any_positional_argument_with_value.rb +1 -7
  85. data/lib/leftovers/matchers/node_has_positional_argument_with_value.rb +0 -4
  86. data/lib/leftovers/matchers/node_name.rb +0 -4
  87. data/lib/leftovers/matchers/node_pair_value.rb +0 -4
  88. data/lib/leftovers/matchers/node_path.rb +0 -4
  89. data/lib/leftovers/matchers/node_privacy.rb +19 -0
  90. data/lib/leftovers/matchers/node_scalar_value.rb +0 -4
  91. data/lib/leftovers/matchers/node_type.rb +0 -4
  92. data/lib/leftovers/matchers/not.rb +0 -4
  93. data/lib/leftovers/matchers/or.rb +0 -4
  94. data/lib/leftovers/matchers.rb +1 -0
  95. data/lib/leftovers/merged_config.rb +15 -33
  96. data/lib/leftovers/precompilers/erb.rb +22 -0
  97. data/lib/leftovers/precompilers/haml.rb +15 -0
  98. data/lib/leftovers/precompilers/json.rb +27 -0
  99. data/lib/leftovers/precompilers/precompiler.rb +28 -0
  100. data/lib/leftovers/precompilers/slim.rb +15 -0
  101. data/lib/leftovers/precompilers/yaml.rb +75 -0
  102. data/lib/leftovers/precompilers.rb +50 -0
  103. data/lib/leftovers/processor_builders/action.rb +48 -39
  104. data/lib/leftovers/processor_builders/add_prefix.rb +2 -2
  105. data/lib/leftovers/processor_builders/add_suffix.rb +2 -2
  106. data/lib/leftovers/processor_builders/argument.rb +8 -11
  107. data/lib/leftovers/processor_builders/dynamic.rb +51 -16
  108. data/lib/leftovers/processor_builders/each.rb +2 -2
  109. data/lib/leftovers/processor_builders/each_action.rb +33 -33
  110. data/lib/leftovers/processor_builders/each_dynamic.rb +4 -8
  111. data/lib/leftovers/processor_builders/each_for_definition_set.rb +25 -21
  112. data/lib/leftovers/processor_builders/keyword.rb +3 -4
  113. data/lib/leftovers/processor_builders/transform.rb +4 -4
  114. data/lib/leftovers/processor_builders/transform_chain.rb +16 -8
  115. data/lib/leftovers/processor_builders/transform_set.rb +32 -28
  116. data/lib/leftovers/rake_task.rb +1 -1
  117. data/lib/leftovers/todo_reporter.rb +10 -35
  118. data/lib/leftovers/value_processors/add_dynamic_prefix.rb +3 -10
  119. data/lib/leftovers/value_processors/add_dynamic_suffix.rb +3 -10
  120. data/lib/leftovers/value_processors/delete_prefix.rb +0 -6
  121. data/lib/leftovers/value_processors/delete_suffix.rb +0 -6
  122. data/lib/leftovers/value_processors/each.rb +1 -1
  123. data/lib/leftovers/value_processors/each_for_definition_set.rb +4 -10
  124. data/lib/leftovers/value_processors/each_keyword.rb +1 -1
  125. data/lib/leftovers/value_processors/each_keyword_argument.rb +1 -1
  126. data/lib/leftovers/value_processors/each_positional_argument.rb +3 -2
  127. data/lib/leftovers/value_processors/keyword.rb +3 -11
  128. data/lib/leftovers/value_processors/keyword_argument.rb +2 -10
  129. data/lib/leftovers/value_processors/return_definition_node.rb +14 -0
  130. data/lib/leftovers/value_processors/{return_string.rb → return_sym.rb} +1 -1
  131. data/lib/leftovers/value_processors/split.rb +2 -2
  132. data/lib/leftovers/value_processors.rb +2 -2
  133. data/lib/leftovers/version.rb +1 -1
  134. data/lib/leftovers.rb +66 -23
  135. metadata +98 -39
  136. data/lib/config/actioncable.yml +0 -4
  137. data/lib/leftovers/backports.rb +0 -40
  138. data/lib/leftovers/config_validator/error_processor.rb +0 -196
  139. data/lib/leftovers/config_validator/schema_hash.rb +0 -530
  140. data/lib/leftovers/config_validator.rb +0 -60
  141. data/lib/leftovers/erb.rb +0 -20
  142. data/lib/leftovers/haml.rb +0 -21
  143. data/lib/leftovers/slim.rb +0 -21
  144. data/lib/leftovers/value_processors/return_definition.rb +0 -26
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module Leftovers
6
+ module Precompilers
7
+ module YAML
8
+ class Builder < ::Psych::TreeBuilder
9
+ def initialize
10
+ @constants = []
11
+
12
+ super
13
+ end
14
+
15
+ def add_constant_for_tag(tag, value = nil)
16
+ match = %r{\A!ruby/[^:]*(?::(.*))?\z}.match(tag)
17
+ return unless match
18
+
19
+ @constants << (match[1] || value)
20
+ end
21
+
22
+ def start_mapping(_anchor, tag, *rest) # leftovers:keep
23
+ add_constant_for_tag(tag)
24
+ tag = nil
25
+
26
+ super
27
+ end
28
+
29
+ def start_sequence(_anchor, tag, *rest) # leftovers:keep
30
+ add_constant_for_tag(tag)
31
+ tag = nil
32
+
33
+ super
34
+ end
35
+
36
+ def scalar(value, _anchor, tag, *rest) # leftovers:keep
37
+ add_constant_for_tag(tag, value)
38
+ tag = nil
39
+
40
+ super
41
+ end
42
+
43
+ def to_ruby_file
44
+ <<~FILE
45
+ __leftovers_document(#{to_ruby_argument(root.to_ruby.first)})
46
+ #{@constants.join("\n")}
47
+ FILE
48
+ end
49
+
50
+ private
51
+
52
+ def to_ruby_argument(value)
53
+ ruby = value.inspect
54
+ return ruby unless value.is_a?(Array)
55
+
56
+ ruby.delete_prefix!('[')
57
+ ruby.delete_suffix!(']')
58
+
59
+ ruby
60
+ end
61
+ end
62
+
63
+ def self.precompile(yaml)
64
+ builder = ::Leftovers::Precompilers::YAML::Builder.new
65
+ parser = ::Psych::Parser.new(builder)
66
+ parser.parse(yaml)
67
+
68
+ builder.to_ruby_file
69
+ rescue ::Psych::SyntaxError => e
70
+ message = [e.problem, e.context].compact.join(' ')
71
+ raise ::Leftovers::PrecompileError.new(message, line: e.line, column: e.column)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -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
@@ -6,12 +6,12 @@ module Leftovers
6
6
  def self.build(argument, then_processor)
7
7
  case argument
8
8
  when ::Hash
9
- dynamic_prefix = ::Leftovers::ProcessorBuilders::Action.build(argument, :call)
9
+ dynamic_prefix = ::Leftovers::ProcessorBuilders::Action.build(argument, :sym)
10
10
  ::Leftovers::ValueProcessors::AddDynamicPrefix.new(dynamic_prefix, then_processor)
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
@@ -6,12 +6,12 @@ module Leftovers
6
6
  def self.build(argument, then_processor)
7
7
  case argument
8
8
  when ::Hash
9
- dynamic_suffix = ::Leftovers::ProcessorBuilders::Action.build(argument, :call)
9
+ dynamic_suffix = ::Leftovers::ProcessorBuilders::Action.build(argument, :sym)
10
10
  ::Leftovers::ValueProcessors::AddDynamicSuffix.new(dynamic_suffix, then_processor)
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
@@ -3,22 +3,57 @@
3
3
  module Leftovers
4
4
  module ProcessorBuilders
5
5
  module Dynamic
6
- def self.build(dynamic_rules) # rubocop:disable Metrics/MethodLength
7
- ::Leftovers::ProcessorBuilders::EachDynamic.each_or_self(dynamic_rules) do |dynamic|
8
- call = ::Leftovers::ProcessorBuilders::Action.build(dynamic.delete(:call), :call)
9
- define = ::Leftovers::ProcessorBuilders::Action.build(dynamic.delete(:define), :define)
10
- matcher = ::Leftovers::MatcherBuilders::Node.build(**dynamic)
11
-
12
- # this nonsense saves a method call and array instantiation per method
13
- if call && define
14
- ::Leftovers::DynamicProcessors::CallDefinition.new(matcher, call, define)
15
- elsif define
16
- ::Leftovers::DynamicProcessors::Definition.new(matcher, define)
17
- elsif call
18
- ::Leftovers::DynamicProcessors::Call.new(matcher, call)
19
- # :nocov:
20
- else raise
21
- # :nocov:
6
+ class << self
7
+ def build(dynamic_rules)
8
+ ::Leftovers::ProcessorBuilders::EachDynamic.each_or_self(dynamic_rules) do |dynamic|
9
+ build_processors(**dynamic)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def build_processors(
16
+ call: nil, define: nil, set_privacy: nil, set_default_privacy: nil, **matcher_rules
17
+ )
18
+ matcher = ::Leftovers::MatcherBuilders::Node.build(**matcher_rules)
19
+
20
+ call_action = build_action(call, return_type: :sym)
21
+ define_action = build_action(define, return_type: :definition_node)
22
+
23
+ ::Leftovers::ProcessorBuilders::EachDynamic.build([
24
+ build_call_define_processor(matcher, call_action, define_action),
25
+ build_set_privacy_processor(matcher, set_privacy),
26
+ build_set_default_privacy_processor(matcher, set_default_privacy)
27
+ ])
28
+ end
29
+
30
+ def build_action(processor_rules, return_type:)
31
+ ::Leftovers::ProcessorBuilders::Action.build(processor_rules, return_type)
32
+ end
33
+
34
+ def build_set_privacy_processor(matcher, set_privacy)
35
+ ::Leftovers::ProcessorBuilders::EachDynamic.each_or_self(set_privacy) do |action_values|
36
+ to = action_values.delete(:to)
37
+ action = build_action(action_values, return_type: :sym)
38
+
39
+ ::Leftovers::DynamicProcessors::SetPrivacy.new(matcher, action, to)
40
+ end
41
+ end
42
+
43
+ def build_set_default_privacy_processor(matcher, set_default_privacy)
44
+ ::Leftovers::DynamicProcessors::SetDefaultPrivacy.new(matcher, set_default_privacy)
45
+ end
46
+
47
+ def build_call_define_processor(matcher, call_action, define_action)
48
+ if call_action && define_action
49
+ # this nonsense saves a method call and array instantiation per method
50
+ ::Leftovers::DynamicProcessors::CallDefinition.new(matcher, call_action, define_action)
51
+ elsif define_action
52
+ ::Leftovers::DynamicProcessors::Definition.new(matcher, define_action)
53
+ elsif call_action
54
+ ::Leftovers::DynamicProcessors::Call.new(matcher, call_action)
55
+ else
56
+ ::Leftovers::DynamicProcessors::Null
22
57
  end
23
58
  end
24
59
  end
@@ -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
@@ -19,9 +19,9 @@ module Leftovers
19
19
  ::Leftovers::ValueProcessors::Pluralize.new(then_processor)
20
20
  when 'singularize'
21
21
  ::Leftovers::ValueProcessors::Singularize.new(then_processor)
22
- when 'camelize', 'camelcase'
22
+ when 'camelize'
23
23
  ::Leftovers::ValueProcessors::Camelize.new(then_processor)
24
- when 'titleize', 'titlecase'
24
+ when 'titleize'
25
25
  ::Leftovers::ValueProcessors::Titleize.new(then_processor)
26
26
  when 'demodulize'
27
27
  ::Leftovers::ValueProcessors::Demodulize.new(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