hash_tools 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +53 -0
- data/.travis.yml +1 -0
- data/Gemfile +3 -6
- data/Rakefile +3 -50
- data/hash_tools.gemspec +27 -54
- data/lib/hash_tools.rb +1 -1
- data/lib/hash_tools/indifferent.rb +6 -2
- metadata +26 -28
- data/spec/hash_tools/indifferent_spec.rb +0 -90
- data/spec/hash_tools_spec.rb +0 -171
- data/spec/spec_helper.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd2b001e2124881b7b3ceabe56d0970e9d53695a
|
4
|
+
data.tar.gz: f53e06a41c6affa8959f6d41d93a1483362b142c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecc953a68b52a3c1b8a39aa81361f34118a59c4adef6b035f15a75490e8d883a1910a5a6b9535041373aea1e2de1779734337d866e7725b9a79261d927adb744
|
7
|
+
data.tar.gz: 03d991cdc0e98d252f85b0447d64096037d43876b34eab4dc54824fe933cc0bf040ac2e656c0b2e4076f7ddd0631a4aa03cc8411366e12b71279c175653d8d9f
|
data/.gitignore
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
coverage.data
|
4
|
+
|
5
|
+
# rdoc generated
|
6
|
+
rdoc
|
7
|
+
|
8
|
+
# yard generated
|
9
|
+
doc
|
10
|
+
.yardoc
|
11
|
+
|
12
|
+
# bundler
|
13
|
+
.bundle
|
14
|
+
Gemfile.lock
|
15
|
+
|
16
|
+
# docs
|
17
|
+
doc
|
18
|
+
|
19
|
+
# jeweler generated
|
20
|
+
pkg
|
21
|
+
|
22
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
23
|
+
#
|
24
|
+
# * Create a file at ~/.gitignore
|
25
|
+
# * Include files you want ignored
|
26
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
27
|
+
#
|
28
|
+
# After doing this, these files will be ignored in all your git projects,
|
29
|
+
# saving you from having to 'pollute' every project you touch with them
|
30
|
+
#
|
31
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
32
|
+
#
|
33
|
+
# For MacOS:
|
34
|
+
#
|
35
|
+
#.DS_Store
|
36
|
+
|
37
|
+
# For TextMate
|
38
|
+
#*.tmproj
|
39
|
+
#tmtags
|
40
|
+
|
41
|
+
# For emacs:
|
42
|
+
#*~
|
43
|
+
#\#*
|
44
|
+
#.\#*
|
45
|
+
|
46
|
+
# For vim:
|
47
|
+
#*.swp
|
48
|
+
|
49
|
+
# For redcar:
|
50
|
+
#.redcar
|
51
|
+
|
52
|
+
# For rubinius:
|
53
|
+
#*.rbc
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -1,53 +1,6 @@
|
|
1
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
require 'rubygems'
|
6
|
-
require 'bundler'
|
7
|
-
begin
|
8
|
-
Bundler.setup(:default, :development)
|
9
|
-
rescue Bundler::BundlerError => e
|
10
|
-
$stderr.puts e.message
|
11
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
-
exit e.status_code
|
13
|
-
end
|
14
|
-
require 'rake'
|
15
|
-
|
16
|
-
require 'jeweler'
|
17
|
-
Jeweler::Tasks.new do |gem|
|
18
|
-
gem.version = HashTools::VERSION
|
19
|
-
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
20
|
-
gem.name = "hash_tools"
|
21
|
-
gem.homepage = "http://github.com/julik/hash_tools"
|
22
|
-
gem.license = "MIT"
|
23
|
-
gem.description = %Q{Do useful things to Ruby Hashes}
|
24
|
-
gem.summary = %Q{Do useful things to Ruby Hashes}
|
25
|
-
gem.email = "me@julik.nl"
|
26
|
-
gem.authors = ["Julik Tarkhanov"]
|
27
|
-
# dependencies defined in Gemfile
|
28
|
-
end
|
29
|
-
Jeweler::RubygemsDotOrgTasks.new
|
30
|
-
|
31
|
-
require 'rspec/core'
|
32
|
-
require 'rspec/core/rake_task'
|
33
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
34
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
35
|
-
end
|
36
|
-
|
37
|
-
desc "Code coverage detail"
|
38
|
-
task :simplecov do
|
39
|
-
ENV['COVERAGE'] = "true"
|
40
|
-
Rake::Task['spec'].execute
|
41
|
-
end
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
42
5
|
|
43
6
|
task :default => :spec
|
44
|
-
|
45
|
-
require 'rdoc/task'
|
46
|
-
Rake::RDocTask.new do |rdoc|
|
47
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
-
|
49
|
-
rdoc.rdoc_dir = 'rdoc'
|
50
|
-
rdoc.title = "hash_tools #{version}"
|
51
|
-
rdoc.rdoc_files.include('README*')
|
52
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
-
end
|
data/hash_tools.gemspec
CHANGED
@@ -1,60 +1,33 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
# stub: hash_tools 1.2.2 ruby lib
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash_tools'
|
6
5
|
|
7
|
-
Gem::Specification.new do |
|
8
|
-
|
9
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hash_tools"
|
8
|
+
spec.version = HashTools::VERSION
|
9
|
+
spec.authors = ["Julik Tarkhanov"]
|
10
|
+
spec.email = ["me@julik.nl"]
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
s.description = "Do useful things to Ruby Hashes"
|
16
|
-
s.email = "me@julik.nl"
|
17
|
-
s.extra_rdoc_files = [
|
18
|
-
"LICENSE.txt",
|
19
|
-
"README.md"
|
20
|
-
]
|
21
|
-
s.files = [
|
22
|
-
".document",
|
23
|
-
".rspec",
|
24
|
-
".travis.yml",
|
25
|
-
".yardopts",
|
26
|
-
"Gemfile",
|
27
|
-
"LICENSE.txt",
|
28
|
-
"README.md",
|
29
|
-
"Rakefile",
|
30
|
-
"hash_tools.gemspec",
|
31
|
-
"lib/hash_tools.rb",
|
32
|
-
"lib/hash_tools/indifferent.rb",
|
33
|
-
"spec/hash_tools/indifferent_spec.rb",
|
34
|
-
"spec/hash_tools_spec.rb",
|
35
|
-
"spec/spec_helper.rb"
|
36
|
-
]
|
37
|
-
s.homepage = "http://github.com/julik/hash_tools"
|
38
|
-
s.licenses = ["MIT"]
|
39
|
-
s.rubygems_version = "2.2.2"
|
40
|
-
s.summary = "Do useful things to Ruby Hashes"
|
12
|
+
spec.summary = %Q{Do useful things to Ruby Hashes}
|
13
|
+
spec.description = %Q{Do useful things to Ruby Hashes}
|
14
|
+
spec.homepage = "https://github.com/WeTransfer/hash_tools"
|
15
|
+
spec.license = "MIT"
|
41
16
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
s.add_development_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
|
47
|
-
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
48
|
-
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
49
|
-
else
|
50
|
-
s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
|
51
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
52
|
-
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
53
|
-
end
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
54
21
|
else
|
55
|
-
|
56
|
-
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
57
|
-
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
58
23
|
end
|
59
|
-
end
|
60
24
|
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.2.0", '< 3.3'
|
33
|
+
end
|
data/lib/hash_tools.rb
CHANGED
@@ -113,9 +113,13 @@ class HashTools::Indifferent < SimpleDelegator
|
|
113
113
|
def respond_to_missing?(method_name, include_private=false)
|
114
114
|
key?(method_name)
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
|
+
def to_hash
|
118
|
+
__getobj__.to_hash
|
119
|
+
end
|
120
|
+
|
117
121
|
alias_method :has_key?, :key?
|
118
|
-
|
122
|
+
|
119
123
|
private
|
120
124
|
|
121
125
|
def __transform_key__(k)
|
metadata
CHANGED
@@ -1,72 +1,72 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julik Tarkhanov
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "<"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '3.3'
|
20
17
|
- - "~>"
|
21
18
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
19
|
+
version: '1'
|
23
20
|
type: :development
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
|
-
- - "<"
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '3.3'
|
30
24
|
- - "~>"
|
31
25
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
26
|
+
version: '1'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
28
|
+
name: rake
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
36
30
|
requirements:
|
37
31
|
- - "~>"
|
38
32
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
33
|
+
version: '10.0'
|
40
34
|
type: :development
|
41
35
|
prerelease: false
|
42
36
|
version_requirements: !ruby/object:Gem::Requirement
|
43
37
|
requirements:
|
44
38
|
- - "~>"
|
45
39
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
40
|
+
version: '10.0'
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
42
|
+
name: rspec
|
49
43
|
requirement: !ruby/object:Gem::Requirement
|
50
44
|
requirements:
|
51
45
|
- - "~>"
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version: 2.0
|
47
|
+
version: 3.2.0
|
48
|
+
- - "<"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '3.3'
|
54
51
|
type: :development
|
55
52
|
prerelease: false
|
56
53
|
version_requirements: !ruby/object:Gem::Requirement
|
57
54
|
requirements:
|
58
55
|
- - "~>"
|
59
56
|
- !ruby/object:Gem::Version
|
60
|
-
version: 2.0
|
57
|
+
version: 3.2.0
|
58
|
+
- - "<"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.3'
|
61
61
|
description: Do useful things to Ruby Hashes
|
62
|
-
email:
|
62
|
+
email:
|
63
|
+
- me@julik.nl
|
63
64
|
executables: []
|
64
65
|
extensions: []
|
65
|
-
extra_rdoc_files:
|
66
|
-
- LICENSE.txt
|
67
|
-
- README.md
|
66
|
+
extra_rdoc_files: []
|
68
67
|
files:
|
69
68
|
- ".document"
|
69
|
+
- ".gitignore"
|
70
70
|
- ".rspec"
|
71
71
|
- ".travis.yml"
|
72
72
|
- ".yardopts"
|
@@ -77,13 +77,11 @@ files:
|
|
77
77
|
- hash_tools.gemspec
|
78
78
|
- lib/hash_tools.rb
|
79
79
|
- lib/hash_tools/indifferent.rb
|
80
|
-
|
81
|
-
- spec/hash_tools_spec.rb
|
82
|
-
- spec/spec_helper.rb
|
83
|
-
homepage: http://github.com/julik/hash_tools
|
80
|
+
homepage: https://github.com/WeTransfer/hash_tools
|
84
81
|
licenses:
|
85
82
|
- MIT
|
86
|
-
metadata:
|
83
|
+
metadata:
|
84
|
+
allowed_push_host: https://rubygems.org
|
87
85
|
post_install_message:
|
88
86
|
rdoc_options: []
|
89
87
|
require_paths:
|
@@ -100,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
98
|
version: '0'
|
101
99
|
requirements: []
|
102
100
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.
|
101
|
+
rubygems_version: 2.5.2
|
104
102
|
signing_key:
|
105
103
|
specification_version: 4
|
106
104
|
summary: Do useful things to Ruby Hashes
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require_relative '../spec_helper'
|
2
|
-
|
3
|
-
describe HashTools::Indifferent do
|
4
|
-
it 'supports indifferent access' do
|
5
|
-
h_syms = {a: 1, 'b' => 2}
|
6
|
-
wrapper = described_class.new(h_syms)
|
7
|
-
|
8
|
-
expect(wrapper['a']).to eq(1)
|
9
|
-
expect(wrapper[:a]).to eq(1)
|
10
|
-
expect(wrapper.fetch('a')).to eq(1)
|
11
|
-
expect(wrapper.fetch(:a)).to eq(1)
|
12
|
-
|
13
|
-
expect(wrapper['b']).to eq(2)
|
14
|
-
expect(wrapper[:b]).to eq(2)
|
15
|
-
expect(wrapper.fetch('b')).to eq(2)
|
16
|
-
expect(wrapper.fetch(:b)).to eq(2)
|
17
|
-
|
18
|
-
expect(wrapper.a).to eq(1)
|
19
|
-
expect(wrapper.b).to eq(2)
|
20
|
-
|
21
|
-
expect(wrapper.keys).to eq(h_syms.keys)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'serializes as a JSON object when used as an array member' do
|
25
|
-
require 'json'
|
26
|
-
h = {a: 1}
|
27
|
-
i = described_class.new(h)
|
28
|
-
array = [h, i]
|
29
|
-
dumped = JSON.dump(array)
|
30
|
-
loaded = JSON.load(dumped)
|
31
|
-
expect(loaded[1]).to eq(loaded[0]) # The object representations should be equivalent
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'raises NoMethodError when accessing missing keys via dot notation' do
|
35
|
-
h_syms = {a: 1, 'b' => 2}
|
36
|
-
wrapper = described_class.new(h_syms)
|
37
|
-
|
38
|
-
expect {
|
39
|
-
wrapper.c
|
40
|
-
}.to raise_error(NoMethodError)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'supports indifferent access to deeply nested hashes' do
|
44
|
-
h_deep = {a: {:b => 1, 'c' => 2}}
|
45
|
-
|
46
|
-
wrapper = described_class.new(h_deep)
|
47
|
-
expect(wrapper[:a][:b]).to eq(1)
|
48
|
-
expect(wrapper['a']['b']).to eq(1)
|
49
|
-
|
50
|
-
expect(wrapper[:a][:c]).to eq(2)
|
51
|
-
expect(wrapper['a']['c']).to eq(2)
|
52
|
-
|
53
|
-
expect(wrapper.a.b).to eq(1)
|
54
|
-
expect(wrapper.a.c).to eq(2)
|
55
|
-
|
56
|
-
expect(wrapper.keys).to eq(h_deep.keys)
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'supports has_non_empty?' do
|
60
|
-
h_deep = {:a => 1, :b => '', :c => false, :d => nil}
|
61
|
-
wrapper = described_class.new(h_deep)
|
62
|
-
|
63
|
-
expect(wrapper).to be_value_present(:a)
|
64
|
-
|
65
|
-
expect(wrapper).not_to be_value_present(:z)
|
66
|
-
expect(wrapper).not_to be_value_present('z')
|
67
|
-
|
68
|
-
expect(wrapper).not_to be_value_present(:b)
|
69
|
-
expect(wrapper).not_to be_value_present(:c)
|
70
|
-
expect(wrapper).not_to be_value_present(:d)
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'supports map' do
|
74
|
-
h_deep = {:a => {:b => 1}, 'b' => {'b' => 2}}
|
75
|
-
wrapper = described_class.new(h_deep)
|
76
|
-
|
77
|
-
wrapper.map do |(k, v)|
|
78
|
-
expect(v['b']).not_to be_nil
|
79
|
-
expect(v[:b]).not_to be_nil
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'supports indifferent access to inner arrays' do
|
84
|
-
h_deep = {:a => [{:b => 1}]}
|
85
|
-
wrapper = described_class.new(h_deep)
|
86
|
-
|
87
|
-
expect(wrapper[:a][0][:b]).to eq(1)
|
88
|
-
expect(wrapper['a'][0]['b']).to eq(1)
|
89
|
-
end
|
90
|
-
end
|
data/spec/hash_tools_spec.rb
DELETED
@@ -1,171 +0,0 @@
|
|
1
|
-
require_relative 'spec_helper'
|
2
|
-
|
3
|
-
describe HashTools do
|
4
|
-
let(:t) do
|
5
|
-
Class.new do
|
6
|
-
include HashTools
|
7
|
-
end.new
|
8
|
-
end
|
9
|
-
let(:uppercase) { ->(k){ k.upcase } }
|
10
|
-
|
11
|
-
describe '.indifferent' do
|
12
|
-
it 'returns an indifferent wrapper' do
|
13
|
-
h = {'foo' => {bar: 1}}
|
14
|
-
ind = t.indifferent(h)
|
15
|
-
expect(ind[:foo][:bar]).to eq(1)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'transforms hash keys' do
|
20
|
-
s = {"set" => 10}
|
21
|
-
ref = {"SET" => 10}
|
22
|
-
expect(t.transform_keys_of(s, &uppercase)).to eq(ref)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'transforms nested hash keys' do
|
26
|
-
s = {"set" => {"two"=>123}}
|
27
|
-
ref = {"SET"=>{"TWO"=>123}}
|
28
|
-
expect(t.transform_keys_of(s, &uppercase)).to eq(ref)
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'does nothing to the array' do
|
32
|
-
a = %w( a b c d)
|
33
|
-
expect(t.transform_keys_of(a, &uppercase)).to eq(a)
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'transforms hashes embedded in arrays' do
|
37
|
-
a = [{"me"=>"Julik"}]
|
38
|
-
ref = [{"ME"=>"Julik"}]
|
39
|
-
expect(t.transform_keys_of(a, &uppercase)).to eq(ref)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'transforms nested hashes in an array' do
|
43
|
-
a = {"foo" => [{"me"=>"Julik"}]}
|
44
|
-
ref = {"FOO"=>[{"ME"=>"Julik"}]}
|
45
|
-
expect(t.transform_keys_of(a, &uppercase)).to eq(ref)
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'exposes methods on the module itself' do
|
49
|
-
expect(HashTools).to respond_to(:transform_keys_of)
|
50
|
-
end
|
51
|
-
|
52
|
-
describe '.transform_string_values_of' do
|
53
|
-
it 'transforms the string value' do
|
54
|
-
x = "foo"
|
55
|
-
expect(t.transform_string_values_of(x, &uppercase)).to eq('FOO')
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'transforms strings in array' do
|
59
|
-
x = %w( foo bar baz)
|
60
|
-
ref = %w( FOO BAR BAZ )
|
61
|
-
expect(t.transform_string_values_of(x, &uppercase)).to eq(ref)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'transforms string values in a Hash' do
|
65
|
-
x = {"foo" => "bar"}
|
66
|
-
ref = {"foo" => "BAR"}
|
67
|
-
expect(t.transform_string_values_of(x, &uppercase)).to eq(ref)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe '.transform_string_keys_and_values_of' do
|
72
|
-
it 'transforms both keys and values' do
|
73
|
-
x = {"foo" => "bar"}
|
74
|
-
ref = {"FOO" => "BAR"}
|
75
|
-
expect(t.transform_string_keys_and_values_of(x, &uppercase)).to eq(ref)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe '.deep_fetch' do
|
80
|
-
let(:deep) {
|
81
|
-
{
|
82
|
-
'foo' =>1,
|
83
|
-
'bar' => {
|
84
|
-
'baz' => 2
|
85
|
-
},
|
86
|
-
'array' => [1,2,3],
|
87
|
-
'array-with-hashes' => [{'name' => 'Joe'}, {'name' => 'Jane'}]
|
88
|
-
}
|
89
|
-
}
|
90
|
-
|
91
|
-
it 'accepts a block for a default value' do
|
92
|
-
v = described_class.deep_fetch(deep, 'bar/nonexistent') { :default}
|
93
|
-
expect(v).to eq(:default)
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'fetches deep keys from a hash keyed by strings' do
|
97
|
-
expect(described_class.deep_fetch(deep, 'foo')).to eq(deep.fetch('foo'))
|
98
|
-
expect(described_class.deep_fetch(deep, 'bar/baz')).to eq(deep.fetch('bar').fetch('baz'))
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'fetches deep keys with a custom separator' do
|
102
|
-
expect(described_class.deep_fetch(deep, 'bar.baz', separator: '.')).to eq(deep.fetch('bar').fetch('baz'))
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'causes a KeyError to be raised for missing keys' do
|
106
|
-
expect {
|
107
|
-
described_class.deep_fetch(deep, 'bar/nonexistent')
|
108
|
-
}.to raise_error(KeyError, 'key not found: "nonexistent"')
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'allows fetches from arrays' do
|
112
|
-
expect(described_class.deep_fetch(deep, 'array/0')).to eq(1)
|
113
|
-
expect(described_class.deep_fetch(deep, 'array/-1')).to eq(3)
|
114
|
-
end
|
115
|
-
|
116
|
-
it 'allows fetches from hashes within arrays' do
|
117
|
-
expect(described_class.deep_fetch(deep, 'array-with-hashes/0/name')).to eq('Joe')
|
118
|
-
expect {
|
119
|
-
described_class.deep_fetch(deep, 'array-with-hashes/10/name')
|
120
|
-
}.to raise_error(IndexError, /index 10 outside of array bounds/)
|
121
|
-
|
122
|
-
default_value = described_class.deep_fetch(deep, 'array-with-hashes/0/jake') { :default }
|
123
|
-
expect(default_value).to eq(:default)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe '.deep_fetch_multi' do
|
128
|
-
let(:deep) {
|
129
|
-
{
|
130
|
-
'foo' =>1,
|
131
|
-
'bar' => {
|
132
|
-
'baz' => 2
|
133
|
-
},
|
134
|
-
'array' => [1,2,3],
|
135
|
-
'array-with-hashes' => [{'name' => 'Joe'}, {'name' => 'Jane'}]
|
136
|
-
}
|
137
|
-
}
|
138
|
-
|
139
|
-
it 'fetches mutiple keys' do
|
140
|
-
expect(described_class.deep_fetch_multi(deep, 'foo', 'bar/baz')).to eq([1,2])
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'fetches deep keys with a custom separator' do
|
144
|
-
expect(described_class.deep_fetch_multi(deep, 'foo', 'bar.baz', separator: '.')).to eq([1,2])
|
145
|
-
end
|
146
|
-
|
147
|
-
it 'causes a KeyError to be raised for missing keys' do
|
148
|
-
expect {
|
149
|
-
described_class.deep_fetch_multi(deep, 'foo', 'nonexistent')
|
150
|
-
}.to raise_error(KeyError)
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
describe '.deep_map_value' do
|
155
|
-
it 'deep maps the values' do
|
156
|
-
v = [
|
157
|
-
{'foo' => 5},
|
158
|
-
{'foo' => 6},
|
159
|
-
]
|
160
|
-
expect(described_class.deep_map_value(v, "foo")).to eq([5,6])
|
161
|
-
end
|
162
|
-
|
163
|
-
it 'deep maps the values with a custom separator' do
|
164
|
-
v = [
|
165
|
-
{'foo' => {'bar' => 1}},
|
166
|
-
{'foo' => {'bar' => 2}},
|
167
|
-
]
|
168
|
-
expect(described_class.deep_map_value(v, "foo-bar", separator: '-')).to eq([1,2])
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
data/spec/spec_helper.rb
DELETED