trusty 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trusty/sorting/atom_sorter.rb +102 -0
- data/lib/trusty/sorting/atoms/version.rb +20 -0
- data/lib/trusty/sorting.rb +1 -0
- data/lib/trusty/version.rb +1 -1
- data/lib/trusty.rb +1 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da402961c1200966ab167b95e1bc978534018ec5
|
4
|
+
data.tar.gz: bb8dfa60b4fae41ca354fb975ff7d1e7ba409c88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 554ac7e31363efa90cea9a2eef425036146d8ac35bf7c2ee1d6d574168ae58f70a8cc00051b8094deee5d3aa8603ac3973bd08df0887d13f7a109fa1abac581c
|
7
|
+
data.tar.gz: 7e7795d47f04e934856e64f683764264c05ff68ae21d71c3cf582e25f00817f110b50d8479dd29a92135f433b946ca0348a75f2eac197e531de1d6ae860e366b
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'trusty/sorting/atoms/version'
|
2
|
+
#
|
3
|
+
# Based on https://makandracards.com/makandra/9185-ruby-natural-sort-strings-with-umlauts-and-other-funny-characters
|
4
|
+
#
|
5
|
+
module Trusty
|
6
|
+
module Sorting
|
7
|
+
class AtomSorter
|
8
|
+
|
9
|
+
LEFT_FIRST = -1
|
10
|
+
RIGHT_FIRST = 1
|
11
|
+
BOTH_EQUAL = 0
|
12
|
+
|
13
|
+
COMPARERS = {}
|
14
|
+
|
15
|
+
# /(?<=^|\s)[-][\d\.]+|[\d\.]+|.+?/
|
16
|
+
# /(?<=^|\s)[-][\d\.]+|[\d\.]+|[^\d\.\-]+|[^\d\.]+/
|
17
|
+
PATTERNS_RE = /\d+(?:\.\d+){2,}|(?:(?<=^|\s)[-])?\d+(?:\.\d+)?|.+?/
|
18
|
+
NUMERIC_RE = /[-]?\d+(\.\d+)?/
|
19
|
+
|
20
|
+
attr_reader :value, :index
|
21
|
+
|
22
|
+
def initialize(value, index)
|
23
|
+
@value, @index = value, index
|
24
|
+
end
|
25
|
+
|
26
|
+
def <=>(other)
|
27
|
+
other.is_a?(self.class) || raise("#{self.class} can only compare with classes of the same kind")
|
28
|
+
|
29
|
+
self.class.compare_values(self.value, other.value)
|
30
|
+
end
|
31
|
+
|
32
|
+
class << self
|
33
|
+
def compare_values(left_value, right_value)
|
34
|
+
if left_value.class == right_value.class
|
35
|
+
left_value <=> right_value
|
36
|
+
elsif comparer = COMPARERS[comparer_key_for(left_value.class, right_value.class)]
|
37
|
+
comparer.call(left_value, right_value)
|
38
|
+
else
|
39
|
+
raise "No comparer defined for #{left_value.class} <=> #{right_value.class}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Loosely based on http://stackoverflow.com/a/4079031
|
44
|
+
def parse(string)
|
45
|
+
string.scan(PATTERNS_RE).each_with_index.map do |atom, index|
|
46
|
+
if !atom.match(NUMERIC_RE) then new(normalize_string(atom), index)
|
47
|
+
elsif !atom.include?('.') then new(atom.to_i, index)
|
48
|
+
elsif atom.include?('-') then new(atom.to_f, index)
|
49
|
+
else new(Atoms::Version.new(atom), index)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def sort(list)
|
55
|
+
list.sort_by{ |item| parse(item) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def add_comparer(left_type, right_type, &comparer)
|
59
|
+
COMPARERS[comparer_key_for(left_type, right_type)] = comparer_builder(left_type, right_type, &comparer)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def normalize_string(string)
|
65
|
+
ActiveSupport::Inflector.transliterate(string).downcase
|
66
|
+
end
|
67
|
+
|
68
|
+
def comparer_builder(left_type, right_type, &comparer)
|
69
|
+
->(left_value, right_value) do
|
70
|
+
if left_value.is_a?(left_type)
|
71
|
+
comparer.call(left_value, right_value)
|
72
|
+
else
|
73
|
+
-comparer.call(right_value, left_value)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def check_result(left_value, right_value, if_this, then_that)
|
79
|
+
result = left_value <=> right_value
|
80
|
+
if_this == result ? then_that : result
|
81
|
+
end
|
82
|
+
|
83
|
+
def comparer_key_for(*types)
|
84
|
+
types.map(&:to_s).sort
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Default comparers
|
89
|
+
|
90
|
+
add_comparer(Atoms::Version, Integer) do |left_value, right_value|
|
91
|
+
right_value.negative? ? RIGHT_FIRST : check_result(left_value, [right_value], BOTH_EQUAL, RIGHT_FIRST)
|
92
|
+
end
|
93
|
+
add_comparer(Atoms::Version, Float) do |left_value, right_value|
|
94
|
+
right_value.negative? ? RIGHT_FIRST : check_result(left_value, Atoms::Version.new(right_value), BOTH_EQUAL, LEFT_FIRST)
|
95
|
+
end
|
96
|
+
add_comparer(Atoms::Version, String){ |left_value, right_value| right_value[/^\s/] ? RIGHT_FIRST : LEFT_FIRST }
|
97
|
+
add_comparer(Integer, Float){ |left_value, right_value| check_result(left_value, right_value, BOTH_EQUAL, LEFT_FIRST) }
|
98
|
+
add_comparer(Integer, String){ |left_value, right_value| right_value[/^\s/] ? RIGHT_FIRST : LEFT_FIRST }
|
99
|
+
add_comparer(Float, String){ |left_value, right_value| right_value[/^\s/] ? RIGHT_FIRST : LEFT_FIRST }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Trusty
|
2
|
+
module Sorting
|
3
|
+
module Atoms
|
4
|
+
class Version < Array
|
5
|
+
def initialize(version_string, *minor_and_patch)
|
6
|
+
case version_string
|
7
|
+
when Integer
|
8
|
+
super [version_string] + minor_and_patch
|
9
|
+
when Array
|
10
|
+
super
|
11
|
+
when String
|
12
|
+
super version_string.split('.').map(&:to_i)
|
13
|
+
else
|
14
|
+
raise "Invalid version specified"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'trusty/sorting/atom_sorter'
|
data/lib/trusty/version.rb
CHANGED
data/lib/trusty.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trusty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Van Horn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -178,6 +178,9 @@ files:
|
|
178
178
|
- lib/trusty/rails/controller_extensions.rb
|
179
179
|
- lib/trusty/rails/engine.rb
|
180
180
|
- lib/trusty/rake.rb
|
181
|
+
- lib/trusty/sorting.rb
|
182
|
+
- lib/trusty/sorting/atom_sorter.rb
|
183
|
+
- lib/trusty/sorting/atoms/version.rb
|
181
184
|
- lib/trusty/utilities.rb
|
182
185
|
- lib/trusty/utilities/method_name.rb
|
183
186
|
- lib/trusty/utilities/method_name_extensions.rb
|
@@ -206,7 +209,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
206
209
|
version: '0'
|
207
210
|
requirements: []
|
208
211
|
rubyforge_project:
|
209
|
-
rubygems_version: 2.6.
|
212
|
+
rubygems_version: 2.6.8
|
210
213
|
signing_key:
|
211
214
|
specification_version: 4
|
212
215
|
summary: Trusty allows you to manage environment variables and other common configuration
|