yalphabetize 0.4.1 → 0.6.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: 86d76847f2e23ff6fe3cd5a5e58304236f308cd0541a19bd3ab96f7db10bb601
4
- data.tar.gz: 8c00fcc4c33db1396aeae8384d1fc62d12bb27dd5db2ef0e7115c6a1f4ccce09
3
+ metadata.gz: 6988242d325c39c0c114fce1300814c74456806bed554987a30b5ad5f2d6ca9b
4
+ data.tar.gz: 7db44e443592581729084a86b16a58248223e324f935be63af5786624edfe4da
5
5
  SHA512:
6
- metadata.gz: 6e83b56bf3dafb17675575d038cb07fd69c66d7732846eb5ade8379a84d6ac2994471784b2ba6bbdfa306bcbecddcc5d7f175f31c95fd7eaf1b5b509a4d0ba91
7
- data.tar.gz: 37c7d42a08c8a1ac39bfc7bdfb4ce65ec9a658884bbc433df5504f636a9797ad1695bffca1847d7a651331b9929f72bf3a2253f258be03383319e67857af0039
6
+ metadata.gz: de2c63f96b4d3b182f79c0c57256a94f0f9f357cc49378b76b37c2dac21156256c8193d7dd45f8471992a3e9e83847db324dd0c216668da492ed72206606ed9a
7
+ data.tar.gz: acf1e2c0c0ae38053c6b5e705768c06dc4ea15e52746d6361133ccbe7b2f6be475777e0ab7af92c92d867f28fd3ffc2ce3501c2908b279906ed2213116e8ae76
@@ -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
@@ -47,7 +47,7 @@ module Yalphabetize
47
47
  node_end_line = node.start_line == node.end_line ? node.end_line : node.end_line - 1
48
48
 
49
49
  file_lines[node.start_line..node_end_line].each do |line|
50
- line.gsub!(line, " #{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.1'
5
+ STRING = '0.6.2'
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.1
4
+ version: 0.6.2
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