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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46c17b62a8a0ccd79f67284a397b97f5537b55f9
4
- data.tar.gz: 0a6eb00f7db619fe5b4aa9f7781778c7b5637107
3
+ metadata.gz: da402961c1200966ab167b95e1bc978534018ec5
4
+ data.tar.gz: bb8dfa60b4fae41ca354fb975ff7d1e7ba409c88
5
5
  SHA512:
6
- metadata.gz: 50afc3a5dea7af42cd9c3aa2d5529f4e36dff23cd553199fe424617c4478baa45ebca1554db1390563b253b0102659dd473029f344e090e16c959473e5ec5161
7
- data.tar.gz: 95ea11c6c51596d461cc56249e2298044c10fa78f2469747f22da9a6da3506531ed62add6e11fe4aa97c344e79a7f782151e6328c805b4dcd9ce90aab246b05e
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'
@@ -1,3 +1,3 @@
1
1
  module Trusty
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
data/lib/trusty.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "trusty/version"
2
2
  require "trusty/environment"
3
+ require "trusty/sorting"
3
4
  require "trusty/utilities"
4
5
 
5
6
  # Optional features that need to be required manually when using this gem
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
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-04-30 00:00:00.000000000 Z
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.11
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