hashutil 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0574f7587224a07cd2dc7ff083a9d42bbab93d3
4
+ data.tar.gz: 56dfb6aeb50d456ba918f3b2ba2b0f8cd98e89e6
5
+ SHA512:
6
+ metadata.gz: 727aaedc1fe22adab1fb55352f659e0d5bb6cbc5853efac469ae3dd8b624a76b8929686bfb873c6892da2f5ddc5a282f7d9454d1d8b67857916d1ac2ab30fe38
7
+ data.tar.gz: fe58614a74f171c282880442b05f0d4b31a9d289241183aae23bb119936bf978823b3e1b82e44ed91cf32a2fb7006245f774ff16b554875f772c3c505a02dcac
@@ -0,0 +1,28 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Documentation cache and generated files:
13
+ /.yardoc/
14
+ /_yardoc/
15
+ /doc/
16
+ /rdoc/
17
+
18
+ ## Environment normalisation:
19
+ /.bundle/
20
+ /lib/bundler/man/
21
+
22
+ Gemfile.lock
23
+ .ruby-version
24
+ .ruby-gemset
25
+
26
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
27
+ .rvmrc
28
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,4 @@
1
+ hashutil
2
+ ========
3
+
4
+ Ruby gem for manipulating hashes
File without changes
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ GEM_NAME = 'hashutil'
3
+
4
+ require File.expand_path("../lib/#{GEM_NAME}/version", __FILE__)
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ['Mikhail Bautin']
8
+ gem.email = ['mbautin@gmail.com']
9
+ gem.description = 'Utilities for manipulating hashes'
10
+ gem.summary = 'Utilities for manipulating hashes'
11
+ gem.homepage = "http://github.com/mbautin/#{GEM_NAME}"
12
+ gem.licenses = ['Apache 2.0']
13
+
14
+ gem.executables = `git ls-files -- bin/*`.split('\n').map{ |f| File.basename(f) }
15
+ gem.files = `git ls-files`.split("\n").map(&:strip)
16
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split('\n')
17
+ gem.name = GEM_NAME
18
+ gem.require_paths = ['lib']
19
+ gem.version = HashUtil::VERSION
20
+
21
+ gem.add_dependency 'json', '~> 1.7'
22
+
23
+ gem.add_development_dependency 'rake'
24
+
25
+ end
@@ -0,0 +1,52 @@
1
+ require 'json'
2
+
3
+ module HashUtil
4
+
5
+ # Sort hash keys (Ruby keeps track of key order in hashes).
6
+ def self.multi_level_sort_keys(obj)
7
+ if obj.is_a?(Hash)
8
+ Hash[*obj.sort.map {|k, v| [k, multi_level_sort_keys(v)] }.flatten(1)]
9
+ elsif obj.instance_of?(Array)
10
+ obj.map {|element| multi_level_sort_keys(element) }
11
+ else
12
+ obj
13
+ end
14
+ end
15
+
16
+ # Merge multi-level hashes.
17
+ # Inspired by http://stackoverflow.com/questions/10691318/merge-some-complex-hashes-in-ruby
18
+ #
19
+ # Available options:
20
+ # :keys_to_strings - whether to convert all keys to strings
21
+ def self.multi_level_merge(h1, h2, options = {})
22
+ original_options = options
23
+ options = options.clone
24
+ keys_to_strings = options.delete(:keys_to_strings)
25
+ unless options.empty?
26
+ raise "Invalid options: #{options}"
27
+ end
28
+
29
+ h1 ||= {}
30
+ h2 ||= {}
31
+
32
+ if keys_to_strings
33
+ # Convert all keys to strings as requested.
34
+ h1 = Hash[h1.collect { | k, v | [k.to_s, v] }]
35
+ h2 = Hash[h2.collect { | k, v | [k.to_s, v] }]
36
+ end
37
+
38
+ h1.merge(h2) do |key, first, second|
39
+ if first.is_a?(Hash) && second.is_a?(Hash)
40
+ multi_level_merge(first, second, original_options)
41
+ else
42
+ second
43
+ end
44
+ end
45
+ end
46
+
47
+ # Converts a hash-like object to a proper hash using JSON.
48
+ def self.make_proper_hash(hash_like)
49
+ JSON.parse(JSON.generate(hash_like))
50
+ end
51
+
52
+ end
@@ -0,0 +1,4 @@
1
+ module HashUtil
2
+ VERSION = IO.read(File.expand_path("../../../VERSION", __FILE__))
3
+ end
4
+
@@ -0,0 +1,54 @@
1
+ require 'hashutil'
2
+
3
+ require 'json'
4
+ require 'spec_helper'
5
+
6
+ describe HashUtil do
7
+
8
+ describe '.multi_level_sort_keys' do
9
+ before do
10
+ @hash = {:c => {:zz => 3, :ww => 5, :aa => 17}, :b => 5, :asdf => { :d => 7, :a => 100 }}
11
+ end
12
+
13
+ it 'JSON keys should come in the same order as specified' do
14
+ JSON.generate(@hash).should == '{"c":{"zz":3,"ww":5,"aa":17},"b":5,"asdf":{"d":7,"a":100}}'
15
+ end
16
+
17
+ it 'should sort keys at every level' do
18
+ new_hash = @hash
19
+ JSON.generate(HashUtil.multi_level_sort_keys(new_hash)).should ==
20
+ '{"asdf":{"a":100,"d":7},"b":5,"c":{"aa":17,"ww":5,"zz":3}}'
21
+ # There should be no side effects on the hash being sorted.
22
+ new_hash.should == @hash
23
+ end
24
+ end
25
+
26
+ describe '.multi_level_merge' do
27
+ before do
28
+ @h1 = {:a => 1, :b => { :c => 3, :q => 5 }, 'x' => 15}
29
+ @h2 = {:z => 2, :b => { 'c' => 4, :f => 'foo' }, :x => 'bar'}
30
+ end
31
+
32
+ it 'should work' do
33
+ # Not converting keys to strings.
34
+ expect(
35
+ HashUtil.multi_level_merge(@h1, @h2)
36
+ ).to eq({
37
+ :a => 1, :b=>{ :c => 3, 'c' => 4, :q => 5, :f => 'foo'}, 'x' => 15, :x => 'bar', :z =>2
38
+ })
39
+
40
+ # Converting keys to strings.
41
+ expected_result =
42
+ expect(
43
+ HashUtil.multi_level_merge(@h1, @h2, :keys_to_strings => true)
44
+ ).to eq({
45
+ 'a' => 1, 'b' => { 'c' => 4, 'f' => 'foo', 'q' => 5 }, 'x' => 'bar', 'z' => 2
46
+ })
47
+ end
48
+ end
49
+
50
+ describe '.make_proper_hash' do
51
+ # nothing yes
52
+ end
53
+
54
+ end
File without changes
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashutil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Bautin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ description: Utilities for manipulating hashes
42
+ email:
43
+ - mbautin@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - VERSION
53
+ - hashutil.gemspec
54
+ - lib/hashutil.rb
55
+ - lib/hashutil/version.rb
56
+ - spec/hashutil_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: http://github.com/mbautin/hashutil
59
+ licenses:
60
+ - Apache 2.0
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Utilities for manipulating hashes
82
+ test_files: []