yalphabetize 0.3.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: 702ef0ac265097aa95ae58399f9bd221336ebba2f29ee51b07ff03beb7c471bd
4
- data.tar.gz: d2da1541ac411f12ba6b3a70534df739cfca69620b07d067dfa75bd18c4d7da7
3
+ metadata.gz: 7233124faca7d704c5fc49708d03df7bf821863a7ef1470d2525b00dadc13c80
4
+ data.tar.gz: 904ee4fb5f329b2b9be7fb33a1e7d4bebb3e15dddfee3b6928756e47aab4cf61
5
5
  SHA512:
6
- metadata.gz: 43205417abde2761326cd34006fa49cbd574d1ec98e5d47a01f9a5769fad51883de9bc7822e206cbabf66c56f91ea54ce3c3d986bae3639550d5e3899a9759ff
7
- data.tar.gz: 543936dea7ac58dc5857419cd27330214759109fa3bdb8bdf50524964bb4b1f1d6e30ce9a60fce156c70cb5fdc2694b60fc139f80085678d2841d864dd3e070c
6
+ metadata.gz: 1021062eedb1e2f0a7ceefd6fa7783f55079d415097476b94cea6c399223a8af5a0e8311d560e423744ad35292707280641c81c3c8ebc0ff97baad2b1ec3f9fd
7
+ data.tar.gz: e633c877d5350bc007c0beb931c5eb7a322603b7cd0e4742c48ddc964b44d8da185cb444294c7d7e27e1ece707854948d94e9a534d03c5579eab770a1a6f97ef
@@ -2,8 +2,9 @@
2
2
 
3
3
  module Yalphabetize
4
4
  class Alphabetizer
5
- def initialize(stream_node)
5
+ def initialize(stream_node, order_checker_class:)
6
6
  @stream_node = stream_node
7
+ @order_checker_class = order_checker_class
7
8
  end
8
9
 
9
10
  def call
@@ -14,7 +15,7 @@ module Yalphabetize
14
15
 
15
16
  private
16
17
 
17
- attr_reader :stream_node
18
+ attr_reader :stream_node, :order_checker_class
18
19
 
19
20
  def alphabetize(node)
20
21
  if node.mapping?
@@ -26,7 +27,9 @@ module Yalphabetize
26
27
  end
27
28
 
28
29
  def alphabetize_children(node)
29
- node.children.sort_by! { |key, _value| key.value }
30
+ node.children.sort! do |a, b|
31
+ order_checker_class.compare(a.first.value, b.first.value)
32
+ end
30
33
  end
31
34
 
32
35
  def unpair_children(node)
@@ -10,6 +10,7 @@ module Yalphabetize
10
10
  @autocorrect = options[:autocorrect]
11
11
  @alphabetizer_class = options[:alphabetizer_class]
12
12
  @writer_class = options[:writer_class]
13
+ @order_checker_class = options[:order_checker_class]
13
14
  end
14
15
 
15
16
  def call
@@ -19,14 +20,12 @@ module Yalphabetize
19
20
  else
20
21
  logger.log_no_offence
21
22
  end
22
-
23
- replace_interpolations
24
23
  end
25
24
 
26
25
  private
27
26
 
28
- attr_reader :file_path, :reader_class, :offence_detector_class,
29
- :logger, :autocorrect, :alphabetizer_class, :writer_class
27
+ attr_reader :file_path, :reader_class, :offence_detector_class, :logger, :autocorrect,
28
+ :alphabetizer_class, :writer_class, :order_checker_class
30
29
 
31
30
  def autocorrect_file
32
31
  alphabetize
@@ -36,21 +35,11 @@ module Yalphabetize
36
35
  end
37
36
 
38
37
  def offences?
39
- offence_detector_class.new(stream_node).offences?
40
- end
41
-
42
- def replace_interpolations
43
- interpolations_mapping.each do |uuid, interpolation|
44
- file_content = File.read(file_path)
45
- file_content.sub!(uuid, interpolation)
46
- File.open(file_path, 'w') do |file|
47
- file.write file_content
48
- end
49
- end
38
+ offence_detector_class.new(stream_node, order_checker_class: order_checker_class).offences?
50
39
  end
51
40
 
52
41
  def alphabetize
53
- alphabetizer_class.new(stream_node).call
42
+ alphabetizer_class.new(stream_node, order_checker_class: order_checker_class).call
54
43
  end
55
44
 
56
45
  def handle_aliases
@@ -58,7 +47,7 @@ module Yalphabetize
58
47
  end
59
48
 
60
49
  def rewrite_yml_file
61
- writer_class.new(stream_node, file_path).call
50
+ writer_class.new(stream_node, file_path, interpolations_mapping).call
62
51
  end
63
52
 
64
53
  def print_to_log
@@ -2,8 +2,9 @@
2
2
 
3
3
  module Yalphabetize
4
4
  class OffenceDetector
5
- def initialize(stream_node)
5
+ def initialize(stream_node, order_checker_class:)
6
6
  @stream_node = stream_node
7
+ @order_checker_class = order_checker_class
7
8
  end
8
9
 
9
10
  def offences?
@@ -12,7 +13,7 @@ module Yalphabetize
12
13
 
13
14
  private
14
15
 
15
- attr_reader :stream_node
16
+ attr_reader :stream_node, :order_checker_class
16
17
 
17
18
  def alphabetized?(node)
18
19
  return false if node.mapping? && !alphabetized_children?(node)
@@ -25,7 +26,9 @@ module Yalphabetize
25
26
  end
26
27
 
27
28
  def alphabetized_children?(node)
28
- node.children.each_slice(2).each_cons(2).all? { |a, b| (a.first.value <=> b.first.value) <= 0 }
29
+ node.children.each_slice(2).each_cons(2).all? do |a, b|
30
+ order_checker_class.ordered?(a.first.value, b.first.value)
31
+ end
29
32
  end
30
33
 
31
34
  def each_child_also_alphabetized?(children)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # AaBb
4
+ module Yalphabetize
5
+ module OrderCheckers
6
+ class AlphabeticalThenCapitalizedFirst < Base
7
+ def self.compare(string, other_string)
8
+ alphabetical_comparison(string, other_string).nonzero? || string <=> other_string
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # aAbB
4
+ module Yalphabetize
5
+ module OrderCheckers
6
+ class AlphabeticalThenCapitalizedLast < Base
7
+ def self.compare(string, other_string)
8
+ alphabetical_comparison(string, other_string).nonzero? || string.swapcase <=> other_string.swapcase
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yalphabetize
4
+ module OrderCheckers
5
+ class Base
6
+ def self.ordered?(string, other_string)
7
+ !compare(string, other_string).positive?
8
+ end
9
+
10
+ private_class_method def self.alphabetical_comparison(string, other_string)
11
+ string.downcase <=> other_string.downcase
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yalphabetize
4
+ module OrderCheckers
5
+ class CapitalizedFirstThenAlphabetical < Base
6
+ def self.compare(string, other_string)
7
+ (string <=> other_string)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yalphabetize
4
+ module OrderCheckers
5
+ class CapitalizedLastThenAlphabetical < Base
6
+ def self.compare(string, other_string)
7
+ (string.swapcase <=> other_string.swapcase)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yalphabetize
4
+ module OrderCheckers
5
+ DEFAULT = CapitalizedFirstThenAlphabetical
6
+
7
+ TOKEN_MAPPING = {
8
+ 'abAB' => CapitalizedLastThenAlphabetical,
9
+ 'aAbB' => AlphabeticalThenCapitalizedLast,
10
+ 'AaBb' => AlphabeticalThenCapitalizedFirst,
11
+ 'ABab' => CapitalizedFirstThenAlphabetical
12
+ }.freeze
13
+ end
14
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'psych'
4
+
5
+ module Yalphabetize
6
+ class SequenceIndenter
7
+ def self.call(content)
8
+ new(content).call
9
+ end
10
+
11
+ def initialize(content)
12
+ @content = content
13
+ end
14
+
15
+ def call
16
+ return unless Yalphabetize.config['indent_sequences']
17
+
18
+ find_lines_to_indent
19
+ indent!
20
+ file_lines.join
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :content
26
+
27
+ def find_lines_to_indent
28
+ @nodes_to_indent = []
29
+
30
+ stream_node.each do |node|
31
+ next if node.document?
32
+
33
+ block_sequence_nodes(node)&.each do |sequence_node|
34
+ @nodes_to_indent.push(*sequence_node.children)
35
+ end
36
+ end
37
+ end
38
+
39
+ def block_sequence_nodes(node)
40
+ node.children&.select { |child_node| child_node.sequence? && child_node.style == 1 }
41
+ end
42
+
43
+ def stream_node
44
+ Psych.parse_stream content
45
+ end
46
+
47
+ def indent!
48
+ @nodes_to_indent.each do |node|
49
+ file_lines[node.start_line..node.end_line].each do |line|
50
+ line.gsub!(line, " #{line}")
51
+ end
52
+ end
53
+ end
54
+
55
+ def file_lines
56
+ @_file_lines ||= content.each_line.to_a
57
+ end
58
+ end
59
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yalphabetize
4
4
  class Version
5
- STRING = '0.3.0'
5
+ STRING = '0.4.0'
6
6
  end
7
7
  end
@@ -4,12 +4,16 @@ module Yalphabetize
4
4
  class Writer
5
5
  MAX_LINE_WIDTH = -1
6
6
 
7
- def initialize(stream_node, path)
7
+ def initialize(stream_node, path, interpolations_mapping)
8
8
  @stream_node = stream_node
9
9
  @path = path
10
+ @interpolations_mapping = interpolations_mapping
10
11
  end
11
12
 
12
13
  def call
14
+ indent_sequences
15
+ replace_interpolations
16
+
13
17
  File.open(path, 'w') do |file|
14
18
  file.write new_file_content
15
19
  end
@@ -17,10 +21,20 @@ module Yalphabetize
17
21
 
18
22
  private
19
23
 
20
- attr_reader :stream_node, :path
24
+ attr_reader :stream_node, :path, :interpolations_mapping
21
25
 
22
26
  def new_file_content
23
- stream_node.to_yaml nil, line_width: MAX_LINE_WIDTH
27
+ @_new_file_content ||= stream_node.to_yaml(nil, line_width: MAX_LINE_WIDTH)
28
+ end
29
+
30
+ def replace_interpolations
31
+ interpolations_mapping.each do |uuid, interpolation|
32
+ new_file_content.sub!(uuid, interpolation)
33
+ end
34
+ end
35
+
36
+ def indent_sequences
37
+ @_new_file_content = SequenceIndenter.call(new_file_content)
24
38
  end
25
39
  end
26
40
  end
@@ -37,38 +37,23 @@ module Yalphabetize
37
37
  logger: logger,
38
38
  autocorrect: options[:autocorrect],
39
39
  alphabetizer_class: alphabetizer_class,
40
- writer_class: writer_class
40
+ writer_class: writer_class,
41
+ order_checker_class: order_checker_class
41
42
  ).call
42
43
  end
43
44
 
44
45
  def file_paths
45
- @_file_paths ||= finder.paths(only: only_paths, exclude: excluded_paths)
46
+ @_file_paths ||= finder.paths(only: only_paths, exclude: Yalphabetize.config['exclude'])
46
47
  end
47
48
 
48
49
  def only_paths
49
50
  if args.any?
50
51
  args
51
- elsif config
52
- config['only'] || []
53
52
  else
54
- []
53
+ Yalphabetize.config['only']
55
54
  end
56
55
  end
57
56
 
58
- def excluded_paths
59
- if config
60
- config['exclude'] || []
61
- else
62
- []
63
- end
64
- end
65
-
66
- def config
67
- return unless File.exist?('.yalphabetize.yml')
68
-
69
- @_config ||= Psych.load_file('.yalphabetize.yml') || {}
70
- end
71
-
72
57
  def final_log
73
58
  logger.final_summary
74
59
  logger.offences? ? 1 : 0
@@ -101,5 +86,9 @@ module Yalphabetize
101
86
  def file_yalphabetizer_class
102
87
  Yalphabetize::FileYalphabetizer
103
88
  end
89
+
90
+ def order_checker_class
91
+ OrderCheckers::TOKEN_MAPPING[Yalphabetize.config['sort_by']]
92
+ end
104
93
  end
105
94
  end
data/lib/yalphabetize.rb CHANGED
@@ -8,9 +8,39 @@ require_relative 'yalphabetize/file_yalphabetizer'
8
8
  require_relative 'yalphabetize/logger'
9
9
  require_relative 'yalphabetize/offence_detector'
10
10
  require_relative 'yalphabetize/option_parser'
11
+ require_relative 'yalphabetize/order_checkers/base'
12
+ require_relative 'yalphabetize/order_checkers/alphabetical_then_capitalized_first'
13
+ require_relative 'yalphabetize/order_checkers/alphabetical_then_capitalized_last'
14
+ require_relative 'yalphabetize/order_checkers/capitalized_last_then_alphabetical'
15
+ require_relative 'yalphabetize/order_checkers/capitalized_first_then_alphabetical'
16
+ require_relative 'yalphabetize/order_checkers'
11
17
  require_relative 'yalphabetize/parsing_error'
12
18
  require_relative 'yalphabetize/reader'
19
+ require_relative 'yalphabetize/sequence_indenter'
13
20
  require_relative 'yalphabetize/version'
14
21
  require_relative 'yalphabetize/writer'
15
22
  require_relative 'yalphabetize/yalphabetizer'
16
23
  require_relative 'yalphabetize/yaml_finder'
24
+
25
+ module Yalphabetize
26
+ class << self
27
+ DEFAULT_CONFIG = {
28
+ 'indent_sequences' => true,
29
+ 'exclude' => [],
30
+ 'only' => [],
31
+ 'sort_by' => 'ABab'
32
+ }.freeze
33
+
34
+ def config
35
+ @_config ||= begin
36
+ specified = if File.exist?('.yalphabetize.yml')
37
+ Psych.load_file('.yalphabetize.yml') || {}
38
+ else
39
+ {}
40
+ end
41
+
42
+ DEFAULT_CONFIG.merge(specified)
43
+ end
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yalphabetize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Jenkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-23 00:00:00.000000000 Z
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: psych
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: factory_bot
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -111,8 +111,15 @@ files:
111
111
  - lib/yalphabetize/logger.rb
112
112
  - lib/yalphabetize/offence_detector.rb
113
113
  - lib/yalphabetize/option_parser.rb
114
+ - lib/yalphabetize/order_checkers.rb
115
+ - lib/yalphabetize/order_checkers/alphabetical_then_capitalized_first.rb
116
+ - lib/yalphabetize/order_checkers/alphabetical_then_capitalized_last.rb
117
+ - lib/yalphabetize/order_checkers/base.rb
118
+ - lib/yalphabetize/order_checkers/capitalized_first_then_alphabetical.rb
119
+ - lib/yalphabetize/order_checkers/capitalized_last_then_alphabetical.rb
114
120
  - lib/yalphabetize/parsing_error.rb
115
121
  - lib/yalphabetize/reader.rb
122
+ - lib/yalphabetize/sequence_indenter.rb
116
123
  - lib/yalphabetize/version.rb
117
124
  - lib/yalphabetize/writer.rb
118
125
  - lib/yalphabetize/yalphabetizer.rb