yalphabetize 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fdf038d72b2c031731160247a71b2b29375360fd23b9099f3ce6cb97f022cbc
4
- data.tar.gz: 54906536110194ebccaf0d04137b68d65939a43951d7785aa3eb33938ef6e5d6
3
+ metadata.gz: b8a46ac2b83620bfb782115c3619f6db45dc085ac588bf3911a9c5c0380ac27e
4
+ data.tar.gz: 76a8548aee15525a38afa21d1f21958184f8b77346778626a06c0323dd62fee5
5
5
  SHA512:
6
- metadata.gz: 6d9cbba9a9b4775f275e46f85750517a23ecb552b69a0f53993377bdf1e2a4a6a52ea44b89d3ca9fae276330e909be376cea3e10977fb1c26029352bb2b32ef4
7
- data.tar.gz: eea1b0ce802afd10154823735d675a1e7427a28bec2611017fec957fbaed28c3eff3a66a62ab6606ee31a398f9d9fbfc23baa09d3d0f217a3578911fb1204abd
6
+ metadata.gz: 24727bad57547da439e2c63d17be8469c8f605bcfd1dcee96685e274b89933e716a07f3cbd84ff9e8f1c88c37561efe82ec11a477758614e7e75351cd5ffd61d
7
+ data.tar.gz: f841fe74158715af2a2836b8637d9363e9b7371cb77ddd0ac43d109712a9832dd04b5bb81b741218e30f76bbb56b77b6c90b1a6560913ad8d5e5114cfaabb2ea
@@ -9,8 +9,9 @@ module Yalphabetize
9
9
 
10
10
  def call
11
11
  stream_node.select(&:mapping?).each do |node|
12
+ order_checker = order_checker_class.new_for(node)
12
13
  pair_up_children(node)
13
- alphabetize_children(node)
14
+ alphabetize_children(node, order_checker)
14
15
  unpair_children(node)
15
16
  end
16
17
  end
@@ -19,9 +20,9 @@ module Yalphabetize
19
20
 
20
21
  attr_reader :stream_node, :order_checker_class
21
22
 
22
- def alphabetize_children(node)
23
+ def alphabetize_children(node, order_checker)
23
24
  node.children.sort! do |a, b|
24
- order_checker_class.compare(a.first.value, b.first.value)
25
+ order_checker.compare(a.first.value, b.first.value)
25
26
  end
26
27
  end
27
28
 
@@ -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
@@ -4,7 +4,7 @@
4
4
  module Yalphabetize
5
5
  module OrderCheckers
6
6
  class CapitalizedFirstThenAlphabetical < Base
7
- def self.compare(string, other_string)
7
+ def compare(string, other_string)
8
8
  string <=> other_string
9
9
  end
10
10
  end
@@ -4,7 +4,7 @@
4
4
  module Yalphabetize
5
5
  module OrderCheckers
6
6
  class CapitalizedLastThenAlphabetical < Base
7
- def self.compare(string, other_string)
7
+ def compare(string, other_string)
8
8
  string.swapcase <=> other_string.swapcase
9
9
  end
10
10
  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
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Yalphabetize
4
4
  class Version
5
- STRING = '0.5.0'
5
+ STRING = '0.6.0'
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yalphabetize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.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: 2022-01-06 00:00:00.000000000 Z
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_bot
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
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
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: simplecov
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -92,7 +120,6 @@ files:
92
120
  - lib/yalphabetize/aliaser.rb
93
121
  - lib/yalphabetize/alphabetizer.rb
94
122
  - lib/yalphabetize/cli.rb
95
- - lib/yalphabetize/erb_compiler.rb
96
123
  - lib/yalphabetize/file_yalphabetizer.rb
97
124
  - lib/yalphabetize/logger.rb
98
125
  - lib/yalphabetize/offence_detector.rb
@@ -103,6 +130,7 @@ files:
103
130
  - lib/yalphabetize/order_checkers/base.rb
104
131
  - lib/yalphabetize/order_checkers/capitalized_first_then_alphabetical.rb
105
132
  - lib/yalphabetize/order_checkers/capitalized_last_then_alphabetical.rb
133
+ - lib/yalphabetize/order_checkers/custom.rb
106
134
  - lib/yalphabetize/parsing_error.rb
107
135
  - lib/yalphabetize/reader.rb
108
136
  - lib/yalphabetize/sequence_indenter.rb
@@ -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