deep_sort 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26160361331231287e80b544f188a67a91875d2f
4
+ data.tar.gz: b25fc265071a4058a68dd35b0d78f9d690dc5ecf
5
+ SHA512:
6
+ metadata.gz: 76adbb6f13c84127763a61317e710ecf3e955de8aac18f2c49b7eba12937d563e0920f14084886f044c6a88d5d44ae7852613b4e1023ca237cba4b6239456bed
7
+ data.tar.gz: 9ba9905f5e213a87de772453925f69fc83f7e9af94546f7c0966c759c0ffbb4b27bd65f995d4c3959f560a4bbf81e71b4edc050da3e1648172c38bc23ee11940
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in deep_sort.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 winebarrel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # deep_sort
2
+
3
+ Sort Array/Hash/Enumerable recursively.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/deep_sort.svg)](https://badge.fury.io/rb/deep_sort)
6
+ [![Build Status](https://travis-ci.org/winebarrel/deep_sort.svg?branch=master)](https://travis-ci.org/winebarrel/deep_sort)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'deep_sort'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install deep_sort
23
+
24
+ ## Additional methods
25
+
26
+ * `Array#deep_sort`
27
+ * `Array#deep_sort!`
28
+ * `Hash#deep_sort`
29
+ * `Hash#deep_sort!`
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ require 'deep_sort'
35
+ require 'pp'
36
+ require 'set'
37
+
38
+ hash = {"firstName"=>"John",
39
+ "lastName"=>"Smith",
40
+ "age"=>25,
41
+ "address"=>
42
+ {"streetAddress"=>"21 2nd Street",
43
+ "city"=>"New York",
44
+ "state"=>"NY",
45
+ "postalCode"=>"10021"},
46
+ "phoneNumber"=>
47
+ [{"type"=>"home", "number"=>"212 555-1234"},
48
+ {"type"=>"fax", "number"=>"646 555-4567"}],
49
+ "gender"=>{"type"=>"male"},
50
+ "number"=>[5, 4, 9, 7],
51
+ "tags"=>Set["staff", "engineer", {"joined_at"=>"2016", "emp_no"=>10}, "writer"]}
52
+
53
+
54
+ pp hash.deep_sort
55
+ #=> {"address"=>
56
+ # {"city"=>"New York",
57
+ # "postalCode"=>"10021",
58
+ # "state"=>"NY",
59
+ # "streetAddress"=>"21 2nd Street"},
60
+ # "age"=>25,
61
+ # "firstName"=>"John",
62
+ # "gender"=>{"type"=>"male"},
63
+ # "lastName"=>"Smith",
64
+ # "phoneNumber"=>
65
+ # [{"number"=>"212 555-1234", "type"=>"home"},
66
+ # {"number"=>"646 555-4567", "type"=>"fax"}],
67
+ # "number" => [4, 5, 7, 9],
68
+ # "tags"=>Set["staff", "engineer", {"joined_at"=>"2016", "emp_no"=>10}, "writer"]}
69
+ ```
70
+
71
+ ### `sort_enum: true`
72
+
73
+ ```ruby
74
+ pp hash.deep_sort(sort_enum: true)
75
+ #=> {"address"=>
76
+ # {"city"=>"New York",
77
+ # "postalCode"=>"10021",
78
+ # "state"=>"NY",
79
+ # "streetAddress"=>"21 2nd Street"},
80
+ # "age"=>25,
81
+ # "firstName"=>"John",
82
+ # "gender"=>{"type"=>"male"},
83
+ # "lastName"=>"Smith",
84
+ # "phoneNumber"=>
85
+ # [{"number"=>"212 555-1234", "type"=>"home"},
86
+ # {"number"=>"646 555-4567", "type"=>"fax"}],
87
+ # "number" => [4, 5, 7, 9],
88
+ # "tags"=>["engineer", "staff", "writer", {"emp_no"=>10, "joined_at"=>"2016"}]}
89
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "deep_sort"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/deep_sort.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deep_sort/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'deep_sort'
8
+ spec.version = DeepSort::VERSION
9
+ spec.authors = ['winebarrel']
10
+ spec.email = ['sgwr_dts@yahoo.co.jp']
11
+
12
+ spec.summary = %q{Sort Array/Hash/Enumerable recursively.}
13
+ spec.description = %q{Sort Array/Hash/Enumerable recursively.}
14
+ spec.homepage = 'https://github.com/winebarrel/deep_sort'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_development_dependency 'pp_sort_hash'
26
+ end
data/lib/deep_sort.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'deep_sort/version'
2
+ require 'deep_sort/array_ext'
3
+ require 'deep_sort/hash_ext'
@@ -0,0 +1,27 @@
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
+ self.map {|value|
9
+ case value
10
+ when Array, Hash
11
+ value.deep_sort(options)
12
+ when Enumerable
13
+ if options[:sort_enum]
14
+ value.to_a.deep_sort(options)
15
+ else
16
+ value
17
+ end
18
+ else
19
+ value
20
+ end
21
+ }.sort_by(&:to_s)
22
+ end
23
+ end
24
+
25
+ class Array
26
+ prepend DeepSort::ArrayExt
27
+ end
@@ -0,0 +1,31 @@
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
+ new_hash = {}
9
+
10
+ self.keys.sort_by(&:to_s).each do |key|
11
+ value = self[key]
12
+
13
+ case value
14
+ when Array, Hash
15
+ value = value.deep_sort(options)
16
+ when Enumerable
17
+ if options[:sort_enum]
18
+ value = value.to_a.deep_sort(options)
19
+ end
20
+ end
21
+
22
+ new_hash[key] = value
23
+ end
24
+
25
+ new_hash
26
+ end
27
+ end
28
+
29
+ class Hash
30
+ prepend DeepSort::HashExt
31
+ end
@@ -0,0 +1,3 @@
1
+ module DeepSort
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deep_sort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - winebarrel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pp_sort_hash
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Sort Array/Hash/Enumerable recursively.
70
+ email:
71
+ - sgwr_dts@yahoo.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - deep_sort.gemspec
86
+ - lib/deep_sort.rb
87
+ - lib/deep_sort/array_ext.rb
88
+ - lib/deep_sort/hash_ext.rb
89
+ - lib/deep_sort/version.rb
90
+ homepage: https://github.com/winebarrel/deep_sort
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Sort Array/Hash/Enumerable recursively.
114
+ test_files: []