deep_sort 0.1.3 → 0.1.4

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: 0c9b855a4e11c26b3e2457ace5526499257ab6f3
4
- data.tar.gz: 467acbe09f058c085db9d474f219e94c04afb90a
3
+ metadata.gz: 81628694ecb8fc9d940f5eb522df36d5e7d2404a
4
+ data.tar.gz: 6b4ac16178c3d8d91adfd72cab177752e3a6d748
5
5
  SHA512:
6
- metadata.gz: 54c30b41f01880c8f709154524cd2d4a4a6728a5b498ad0cd7172700a4880621ec44410c67a76304e55c378b49ef058f1c1c4a81f441713ce13c65dec845d65c
7
- data.tar.gz: 1a1aca25d50c37431d79c59af8ac947e98597cf651fe1194863afd9e4c2f8e47c0dd5cb8e981481886b43d55323a9ba2b54c95ad4838d17527c9762f8df9b35d
6
+ metadata.gz: 56dc5281a15a70d8fefc8731903014eb1c0c30adc58a11c117e7dc9fe8db09b269bb636b92cba1668ed7ee8033f17e9732208cef7f866b6ffadbe48606ab5046
7
+ data.tar.gz: 6bc041e786bd01a272b86bc3dda4440f9c40d70bcebdec7b151150e8e168984e439b6779280c0207237dcb3de10ec037f7aade977e05c09e9e49bdaca63cd3ba
@@ -1,20 +1,15 @@
1
1
  require 'deep_sort/version'
2
- require 'deep_sort/array'
3
- require 'deep_sort/hash'
2
+ require 'deep_sort/array_ext'
3
+ require 'deep_sort/hash_ext'
4
4
 
5
5
  module DeepSort
6
6
  def deep_sort(obj, options = {})
7
- case obj
8
- when Array
9
- DeepSort::Array.deep_sort(obj, options)
10
- when Hash
11
- DeepSort::Hash.deep_sort(obj, options)
12
- when Enumerable
13
- if options[:sort_enum]
14
- DeepSort::Array.deep_sort(obj.to_a, options)
15
- else
16
- obj
17
- end
7
+ if obj.is_a?(Array)
8
+ DeepSort::ArrayExt.deep_sort(obj, options)
9
+ elsif obj.is_a?(Hash)
10
+ DeepSort::HashExt.deep_sort(obj, options)
11
+ elsif options[:sort_enum] and obj.is_a?(Enumerable)
12
+ DeepSort::ArrayExt.deep_sort(obj.to_a, options)
18
13
  else
19
14
  obj
20
15
  end
@@ -0,0 +1,24 @@
1
+ module DeepSort::ArrayExt
2
+ def deep_sort!(options = {})
3
+ new_array = self.deep_sort(options)
4
+ self.replace(new_array)
5
+ end
6
+
7
+ def deep_sort(options)
8
+ DeepSort::ArrayExt.deep_sort(self, options)
9
+ end
10
+
11
+ def self.deep_sort(array, options = {})
12
+ array.map {|value|
13
+ if value.is_a?(Array)
14
+ DeepSort::ArrayExt.deep_sort(value, options)
15
+ elsif value.is_a?(Hash)
16
+ DeepSort::HashExt.deep_sort(value, options)
17
+ elsif options[:sort_enum] and value.is_a?(Enumerable)
18
+ DeepSort::ArrayExt.deep_sort(value.to_a, options)
19
+ else
20
+ value
21
+ end
22
+ }.sort_by(&:to_s)
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module Enumerable
2
2
  def deep_sort
3
- DeepSort::Array.deep_sort(self.to_a, sort_enum: true)
3
+ DeepSort::ArrayExt.deep_sort(self.to_a, sort_enum: true)
4
4
  end
5
5
  end
@@ -1,9 +1,9 @@
1
1
  require 'deep_sort/enum_ext'
2
2
 
3
3
  class Array
4
- prepend DeepSort::Array
4
+ prepend DeepSort::ArrayExt
5
5
  end
6
6
 
7
7
  class Hash
8
- prepend DeepSort::Hash
8
+ prepend DeepSort::HashExt
9
9
  end
@@ -0,0 +1,30 @@
1
+ module DeepSort::HashExt
2
+ def deep_sort!(options = {})
3
+ new_hash = self.deep_sort(options)
4
+ self.replace(new_hash)
5
+ end
6
+
7
+ def deep_sort(options = {})
8
+ DeepSort::HashExt.deep_sort(self, options)
9
+ end
10
+
11
+ def self.deep_sort(hash, options = {})
12
+ new_hash = {}
13
+
14
+ hash.keys.sort_by(&:to_s).each do |key|
15
+ value = hash[key]
16
+
17
+ if value.is_a?(Array)
18
+ value = DeepSort::ArrayExt.deep_sort(value, options)
19
+ elsif value.is_a?(Hash)
20
+ value = DeepSort::HashExt.deep_sort(value, options)
21
+ elsif options[:sort_enum] and value.is_a?(Enumerable)
22
+ value = DeepSort::ArrayExt.deep_sort(value.to_a, options)
23
+ end
24
+
25
+ new_hash[key] = value
26
+ end
27
+
28
+ new_hash
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module DeepSort
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel
@@ -84,10 +84,10 @@ files:
84
84
  - bin/setup
85
85
  - deep_sort.gemspec
86
86
  - lib/deep_sort.rb
87
- - lib/deep_sort/array.rb
87
+ - lib/deep_sort/array_ext.rb
88
88
  - lib/deep_sort/enum_ext.rb
89
89
  - lib/deep_sort/extend.rb
90
- - lib/deep_sort/hash.rb
90
+ - lib/deep_sort/hash_ext.rb
91
91
  - lib/deep_sort/version.rb
92
92
  homepage: https://github.com/winebarrel/deep_sort
93
93
  licenses:
@@ -1,27 +0,0 @@
1
- module DeepSort::Array
2
- def deep_sort!(options = {})
3
- new_array = self.deep_sort(options)
4
- self.replace(new_array)
5
- end
6
-
7
- def deep_sort(options)
8
- DeepSort::Array.deep_sort(self, options)
9
- end
10
-
11
- def self.deep_sort(array, options = {})
12
- array.map {|value|
13
- case value
14
- when Array, Hash
15
- DeepSort.deep_sort(value, options)
16
- when Enumerable
17
- if options[:sort_enum]
18
- DeepSort.deep_sort(value.to_a, options)
19
- else
20
- value
21
- end
22
- else
23
- value
24
- end
25
- }.sort_by(&:to_s)
26
- end
27
- end
@@ -1,31 +0,0 @@
1
- module DeepSort::Hash
2
- def deep_sort!(options = {})
3
- new_hash = self.deep_sort(options)
4
- self.replace(new_hash)
5
- end
6
-
7
- def deep_sort(options = {})
8
- DeepSort::Hash.deep_sort(self, options)
9
- end
10
-
11
- def self.deep_sort(hash, options = {})
12
- new_hash = {}
13
-
14
- hash.keys.sort_by(&:to_s).each do |key|
15
- value = hash[key]
16
-
17
- case value
18
- when Array, Hash
19
- value = DeepSort.deep_sort(value, options)
20
- when Enumerable
21
- if options[:sort_enum]
22
- value = DeepSort.deep_sort(value.to_a, options)
23
- end
24
- end
25
-
26
- new_hash[key] = value
27
- end
28
-
29
- new_hash
30
- end
31
- end