yalphabetize 0.4.0 → 0.6.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: 7233124faca7d704c5fc49708d03df7bf821863a7ef1470d2525b00dadc13c80
4
- data.tar.gz: 904ee4fb5f329b2b9be7fb33a1e7d4bebb3e15dddfee3b6928756e47aab4cf61
3
+ metadata.gz: 4e19b4e6cc39bf8354bab85e0238605f9d45b0ae4fb8c01436cf73a5ed1ab4c8
4
+ data.tar.gz: 8e960a7474e6f6cd0a5510bd9414b2518277c874f9c2d68f436b80d1a1d0b123
5
5
  SHA512:
6
- metadata.gz: 1021062eedb1e2f0a7ceefd6fa7783f55079d415097476b94cea6c399223a8af5a0e8311d560e423744ad35292707280641c81c3c8ebc0ff97baad2b1ec3f9fd
7
- data.tar.gz: e633c877d5350bc007c0beb931c5eb7a322603b7cd0e4742c48ddc964b44d8da185cb444294c7d7e27e1ece707854948d94e9a534d03c5579eab770a1a6f97ef
6
+ metadata.gz: eeb6ea0a70e67277733cb2c7c75a404506e784a32c93802129b72c64d8370231c2caed23f91a149f515d536cc0b0f63f2e10df02b0e5c3c58c6ed0d9844c14a7
7
+ data.tar.gz: 8adaab2c141d5f6d74d41836678b8797d20a70bb39fa55f2bd0875f6361291a927e92e1df503edf8f2fe60f26a2c86df1d08b2a3444c563bfc01f982b95b07b3
@@ -8,45 +8,26 @@ module Yalphabetize
8
8
  end
9
9
 
10
10
  def call
11
- transform(stream_node)
12
- alphabetize(stream_node)
13
- stream_node
11
+ stream_node.select(&:mapping?).each do |node|
12
+ order_checker = order_checker_class.new_for(node)
13
+ pair_up_children(node)
14
+ alphabetize_children(node, order_checker)
15
+ unpair_children(node)
16
+ end
14
17
  end
15
18
 
16
19
  private
17
20
 
18
21
  attr_reader :stream_node, :order_checker_class
19
22
 
20
- def alphabetize(node)
21
- if node.mapping?
22
- alphabetize_children(node)
23
- unpair_children(node)
24
- end
25
-
26
- node.children&.each { |child_node| alphabetize(child_node) }
27
- end
28
-
29
- def alphabetize_children(node)
23
+ def alphabetize_children(node, order_checker)
30
24
  node.children.sort! do |a, b|
31
- order_checker_class.compare(a.first.value, b.first.value)
25
+ order_checker.compare(a.first.value, b.first.value)
32
26
  end
33
27
  end
34
28
 
35
29
  def unpair_children(node)
36
- children_clone = node.children.dup
37
-
38
- children_clone.each do |slice|
39
- node.children.push(*slice)
40
- node.children.shift
41
- end
42
- end
43
-
44
- def transform(node)
45
- node.children&.each(&method(:transform))
46
-
47
- return unless node.mapping?
48
-
49
- pair_up_children(node)
30
+ node.children.flatten!
50
31
  end
51
32
 
52
33
  def pair_up_children(node)
@@ -54,8 +35,7 @@ module Yalphabetize
54
35
 
55
36
  children_clone.each_slice(2) do |slice|
56
37
  node.children.push(slice)
57
- node.children.shift
58
- node.children.shift
38
+ 2.times { node.children.shift }
59
39
  end
60
40
  end
61
41
  end
@@ -47,7 +47,7 @@ module Yalphabetize
47
47
  end
48
48
 
49
49
  def rewrite_yml_file
50
- writer_class.new(stream_node, file_path, interpolations_mapping).call
50
+ writer_class.new(stream_node, file_path).call
51
51
  end
52
52
 
53
53
  def print_to_log
@@ -61,9 +61,5 @@ module Yalphabetize
61
61
  def stream_node
62
62
  @_stream_node ||= reader.to_ast
63
63
  end
64
-
65
- def interpolations_mapping
66
- reader.interpolations_mapping
67
- end
68
64
  end
69
65
  end
@@ -26,8 +26,10 @@ module Yalphabetize
26
26
  end
27
27
 
28
28
  def alphabetized_children?(node)
29
+ order_checker = order_checker_class.new_for(node)
30
+
29
31
  node.children.each_slice(2).each_cons(2).all? do |a, b|
30
- order_checker_class.ordered?(a.first.value, b.first.value)
32
+ order_checker.ordered?(a.first.value, b.first.value)
31
33
  end
32
34
  end
33
35
 
@@ -4,7 +4,7 @@
4
4
  module Yalphabetize
5
5
  module OrderCheckers
6
6
  class AlphabeticalThenCapitalizedFirst < Base
7
- def self.compare(string, other_string)
7
+ def compare(string, other_string)
8
8
  alphabetical_comparison(string, other_string).nonzero? || string <=> other_string
9
9
  end
10
10
  end
@@ -4,7 +4,7 @@
4
4
  module Yalphabetize
5
5
  module OrderCheckers
6
6
  class AlphabeticalThenCapitalizedLast < Base
7
- def self.compare(string, other_string)
7
+ def compare(string, other_string)
8
8
  alphabetical_comparison(string, other_string).nonzero? || string.swapcase <=> other_string.swapcase
9
9
  end
10
10
  end
@@ -3,11 +3,25 @@
3
3
  module Yalphabetize
4
4
  module OrderCheckers
5
5
  class Base
6
- def self.ordered?(string, other_string)
6
+ def self.new_for(node)
7
+ node_keys = node.children.select.each_with_index do |_, i|
8
+ i.even?
9
+ end.map(&:value)
10
+
11
+ matched_allowed_order = Yalphabetize.config['allowed_orders']&.find do |allowed_order|
12
+ (node_keys - allowed_order).empty?
13
+ end
14
+
15
+ matched_allowed_order ? Custom.new(matched_allowed_order) : new
16
+ end
17
+
18
+ def ordered?(string, other_string)
7
19
  !compare(string, other_string).positive?
8
20
  end
9
21
 
10
- private_class_method def self.alphabetical_comparison(string, other_string)
22
+ private
23
+
24
+ def alphabetical_comparison(string, other_string)
11
25
  string.downcase <=> other_string.downcase
12
26
  end
13
27
  end
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # ABab
3
4
  module Yalphabetize
4
5
  module OrderCheckers
5
6
  class CapitalizedFirstThenAlphabetical < Base
6
- def self.compare(string, other_string)
7
- (string <=> other_string)
7
+ def compare(string, other_string)
8
+ string <=> other_string
8
9
  end
9
10
  end
10
11
  end
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # abAB
3
4
  module Yalphabetize
4
5
  module OrderCheckers
5
6
  class CapitalizedLastThenAlphabetical < Base
6
- def self.compare(string, other_string)
7
- (string.swapcase <=> other_string.swapcase)
7
+ def compare(string, other_string)
8
+ string.swapcase <=> other_string.swapcase
8
9
  end
9
10
  end
10
11
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yalphabetize
4
+ module OrderCheckers
5
+ class Custom < Base
6
+ def initialize(allowed_order)
7
+ super()
8
+ @allowed_order = allowed_order
9
+ end
10
+
11
+ def compare(string, other_string)
12
+ allowed_order.index(string) <=> allowed_order.index(other_string)
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :allowed_order
18
+ end
19
+ end
20
+ end
@@ -2,8 +2,6 @@
2
2
 
3
3
  module Yalphabetize
4
4
  module OrderCheckers
5
- DEFAULT = CapitalizedFirstThenAlphabetical
6
-
7
5
  TOKEN_MAPPING = {
8
6
  'abAB' => CapitalizedLastThenAlphabetical,
9
7
  'aAbB' => AlphabeticalThenCapitalizedLast,
@@ -1,37 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'psych'
4
- require 'securerandom'
5
4
 
6
5
  module Yalphabetize
7
6
  class Reader
8
7
  def initialize(path)
9
8
  @path = path
10
- @interpolations_mapping = {}
11
9
  end
12
10
 
13
11
  def to_ast
14
- substitute_interpolations
15
12
  stream_node
16
13
  end
17
14
 
18
- attr_reader :interpolations_mapping
19
-
20
15
  private
21
16
 
22
17
  attr_reader :path
23
18
 
24
- def substitute_interpolations
25
- erb_compiler = Yalphabetize::ErbCompiler.new
26
- erb_compiler.compile(file)
27
-
28
- erb_compiler.content.each do |interpolation|
29
- uuid = SecureRandom.uuid
30
- file.sub!(interpolation, uuid)
31
- interpolations_mapping[uuid] = interpolation
32
- end
33
- end
34
-
35
19
  def file
36
20
  @_file ||= File.read(path)
37
21
  end
@@ -27,10 +27,8 @@ module Yalphabetize
27
27
  def find_lines_to_indent
28
28
  @nodes_to_indent = []
29
29
 
30
- stream_node.each do |node|
31
- next if node.document?
32
-
33
- block_sequence_nodes(node)&.each do |sequence_node|
30
+ stream_node.select(&:mapping?).each do |mapping_node|
31
+ block_sequence_nodes(mapping_node)&.each do |sequence_node|
34
32
  @nodes_to_indent.push(*sequence_node.children)
35
33
  end
36
34
  end
@@ -46,8 +44,10 @@ module Yalphabetize
46
44
 
47
45
  def indent!
48
46
  @nodes_to_indent.each do |node|
49
- file_lines[node.start_line..node.end_line].each do |line|
50
- line.gsub!(line, " #{line}")
47
+ node_end_line = node.start_line == node.end_line ? node.end_line : node.end_line - 1
48
+
49
+ file_lines[node.start_line..node_end_line].each do |line|
50
+ line.prepend(' ')
51
51
  end
52
52
  end
53
53
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yalphabetize
4
4
  class Version
5
- STRING = '0.4.0'
5
+ STRING = '0.6.1'
6
6
  end
7
7
  end
@@ -4,35 +4,25 @@ module Yalphabetize
4
4
  class Writer
5
5
  MAX_LINE_WIDTH = -1
6
6
 
7
- def initialize(stream_node, path, interpolations_mapping)
7
+ def initialize(stream_node, path)
8
8
  @stream_node = stream_node
9
9
  @path = path
10
- @interpolations_mapping = interpolations_mapping
11
10
  end
12
11
 
13
12
  def call
14
13
  indent_sequences
15
- replace_interpolations
16
14
 
17
- File.open(path, 'w') do |file|
18
- file.write new_file_content
19
- end
15
+ File.write(path, new_file_content)
20
16
  end
21
17
 
22
18
  private
23
19
 
24
- attr_reader :stream_node, :path, :interpolations_mapping
20
+ attr_reader :stream_node, :path
25
21
 
26
22
  def new_file_content
27
23
  @_new_file_content ||= stream_node.to_yaml(nil, line_width: MAX_LINE_WIDTH)
28
24
  end
29
25
 
30
- def replace_interpolations
31
- interpolations_mapping.each do |uuid, interpolation|
32
- new_file_content.sub!(uuid, interpolation)
33
- end
34
- end
35
-
36
26
  def indent_sequences
37
27
  @_new_file_content = SequenceIndenter.call(new_file_content)
38
28
  end
@@ -26,7 +26,7 @@ module Yalphabetize
26
26
  end
27
27
 
28
28
  def process_files
29
- file_paths.each(&method(:process_file))
29
+ file_paths.each { |file_path| process_file(file_path) }
30
30
  end
31
31
 
32
32
  def process_file(file_path)
@@ -25,7 +25,7 @@ module Yalphabetize
25
25
  process_paths(paths)
26
26
  end
27
27
 
28
- files.flatten.select(&method(:valid?)).map { |f| File.expand_path(f) }.uniq
28
+ files.flatten.select { |file| valid?(file) }.map { |f| File.expand_path(f) }.uniq
29
29
  end
30
30
 
31
31
  private
data/lib/yalphabetize.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  require_relative 'yalphabetize/aliaser'
4
4
  require_relative 'yalphabetize/alphabetizer'
5
5
  require_relative 'yalphabetize/cli'
6
- require_relative 'yalphabetize/erb_compiler'
7
6
  require_relative 'yalphabetize/file_yalphabetizer'
8
7
  require_relative 'yalphabetize/logger'
9
8
  require_relative 'yalphabetize/offence_detector'
@@ -13,6 +12,7 @@ require_relative 'yalphabetize/order_checkers/alphabetical_then_capitalized_firs
13
12
  require_relative 'yalphabetize/order_checkers/alphabetical_then_capitalized_last'
14
13
  require_relative 'yalphabetize/order_checkers/capitalized_last_then_alphabetical'
15
14
  require_relative 'yalphabetize/order_checkers/capitalized_first_then_alphabetical'
15
+ require_relative 'yalphabetize/order_checkers/custom'
16
16
  require_relative 'yalphabetize/order_checkers'
17
17
  require_relative 'yalphabetize/parsing_error'
18
18
  require_relative 'yalphabetize/reader'
metadata CHANGED
@@ -1,31 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yalphabetize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Jenkins
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-05 00:00:00.000000000 Z
11
+ date: 2022-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: psych
14
+ name: factory_bot
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3'
20
- type: :runtime
19
+ version: '0'
20
+ type: :development
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: '3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: factory_bot
28
+ name: pry
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: pry
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,21 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rubocop
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
@@ -94,8 +108,8 @@ dependencies:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
- description:
98
- email:
111
+ description:
112
+ email:
99
113
  executables:
100
114
  - yalphabetize
101
115
  extensions: []
@@ -106,7 +120,6 @@ files:
106
120
  - lib/yalphabetize/aliaser.rb
107
121
  - lib/yalphabetize/alphabetizer.rb
108
122
  - lib/yalphabetize/cli.rb
109
- - lib/yalphabetize/erb_compiler.rb
110
123
  - lib/yalphabetize/file_yalphabetizer.rb
111
124
  - lib/yalphabetize/logger.rb
112
125
  - lib/yalphabetize/offence_detector.rb
@@ -117,6 +130,7 @@ files:
117
130
  - lib/yalphabetize/order_checkers/base.rb
118
131
  - lib/yalphabetize/order_checkers/capitalized_first_then_alphabetical.rb
119
132
  - lib/yalphabetize/order_checkers/capitalized_last_then_alphabetical.rb
133
+ - lib/yalphabetize/order_checkers/custom.rb
120
134
  - lib/yalphabetize/parsing_error.rb
121
135
  - lib/yalphabetize/reader.rb
122
136
  - lib/yalphabetize/sequence_indenter.rb
@@ -124,11 +138,11 @@ files:
124
138
  - lib/yalphabetize/writer.rb
125
139
  - lib/yalphabetize/yalphabetizer.rb
126
140
  - lib/yalphabetize/yaml_finder.rb
127
- homepage:
141
+ homepage:
128
142
  licenses: []
129
143
  metadata:
130
144
  rubygems_mfa_required: 'true'
131
- post_install_message:
145
+ post_install_message:
132
146
  rdoc_options: []
133
147
  require_paths:
134
148
  - lib
@@ -144,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
158
  version: '0'
145
159
  requirements: []
146
160
  rubygems_version: 3.0.3.1
147
- signing_key:
161
+ signing_key:
148
162
  specification_version: 4
149
163
  summary: Alphabetize your YAML files
150
164
  test_files: []
@@ -1,82 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Inspired by ERB::Compiler (https://github.com/ruby/erb/blob/b58b188028fbb403f75d48d62717373fc0908f7a/lib/erb.rb)
4
-
5
- module Yalphabetize
6
- class ErbCompiler
7
- DEFAULT_STAGS = ['<%%', '<%=', '<%#', '<%', '%{', '{{'].freeze
8
- DEFAULT_ETAGS = ['%%>', '%>', '}}', '}'].freeze
9
-
10
- class Scanner
11
- STAG_REG = /(.*?)(#{DEFAULT_STAGS.join('|')}|\z)/m.freeze
12
- ETAG_REG = /(.*?)(#{DEFAULT_ETAGS.join('|')}|\z)/m.freeze
13
-
14
- def initialize(src)
15
- @src = src
16
- @stag = nil
17
- end
18
-
19
- def scan
20
- until scanner.eos?
21
- scanner.scan(stag ? ETAG_REG : STAG_REG)
22
- yield(scanner[1])
23
- yield(scanner[2])
24
- end
25
- end
26
-
27
- attr_accessor :stag
28
-
29
- private
30
-
31
- attr_reader :src
32
-
33
- def scanner
34
- @_scanner ||= StringScanner.new(src)
35
- end
36
- end
37
-
38
- def initialize
39
- @buffer = []
40
- @content = []
41
- end
42
-
43
- def compile(string)
44
- scanner = Scanner.new(string)
45
- scanner.scan do |token|
46
- next if token.nil?
47
- next if token == ''
48
-
49
- if scanner.stag.nil?
50
- compile_stag(token, scanner)
51
- else
52
- compile_etag(token, scanner)
53
- end
54
- end
55
- end
56
-
57
- def compile_stag(stag, scanner)
58
- case stag
59
- when *DEFAULT_STAGS
60
- scanner.stag = stag
61
- buffer << stag
62
- end
63
- end
64
-
65
- def compile_etag(etag, scanner)
66
- buffer << etag
67
-
68
- case etag
69
- when *DEFAULT_ETAGS
70
- content << buffer.join
71
- self.buffer = []
72
- scanner.stag = nil
73
- end
74
- end
75
-
76
- attr_reader :content
77
-
78
- private
79
-
80
- attr_accessor :buffer
81
- end
82
- end