deep_hash_transformer 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: 5e254ec8ac5385b1a8cf03df06b0e55a7cc11c0b
4
+ data.tar.gz: 12af967373ce94acfea044c2096d926c436ab5d7
5
+ SHA512:
6
+ metadata.gz: a145f30b62ecceb24c9be67ba977bba4129399d46d58043c536be0b2f17f1d81284d29547c639f8e3126ceebd074b72350d41b39a231e7e435d37a97f0283876
7
+ data.tar.gz: a04bbbcd42b9d88e7620381ab30cd7a68d202a80992dd9c5065b450e76748360a7b5e664e051a6d0a244355f95889d044a0202b33affee09c345971408e54101
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Martin Spickermann
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.
@@ -0,0 +1,3 @@
1
+ class DeepHashTransformer
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'deep_hash_transformer/version'
2
+
3
+ class DeepHashTransformer
4
+ attr_reader :hash
5
+
6
+ OPS = {
7
+ dasherize: ->(val) { val.to_s.tr('_'.freeze, '-'.freeze) },
8
+ identity: ->(val) { val },
9
+ stringify: ->(val) { val.to_s },
10
+ symbolize: ->(val) { val.to_sym },
11
+ underscore: ->(val) { val.to_s.tr('-'.freeze, '_'.freeze) }
12
+ }.freeze
13
+
14
+ def initialize(hash)
15
+ @hash = hash
16
+ end
17
+
18
+ def tr(*ops)
19
+ transform_value(hash, ops)
20
+ end
21
+
22
+ OPS.keys.each do |operation|
23
+ define_method(operation) { tr(operation) }
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :hash
29
+
30
+ def transform_value(value, ops)
31
+ case value
32
+ when Array
33
+ value.map { |e| transform_value(e, ops) }
34
+ when Hash
35
+ value.map { |k, v| [transform_key(k, ops), transform_value(v, ops)] }.to_h
36
+ else
37
+ value
38
+ end
39
+ end
40
+
41
+ def transform_key(key, ops)
42
+ return key unless [String, Symbol].include?(key.class)
43
+
44
+ ops.inject(key) { |k, op| OPS.fetch(op).call(k) }
45
+ end
46
+ end
@@ -0,0 +1,85 @@
1
+ RSpec.describe DeepHashTransformer do
2
+ let(:hash) do
3
+ {
4
+ Integer => 123,
5
+ :foobar => { bar: 'bar' },
6
+ 'aa_zz' => [{ 'bar' => :bar, 'a-z' => 'a-z' }]
7
+ }
8
+ end
9
+
10
+ describe '#tr with `%i[underscore symbolize]`' do
11
+ subject { DeepHashTransformer.new(hash).tr(:underscore, :symbolize) }
12
+
13
+ it do
14
+ is_expected.to eq(
15
+ Integer => 123,
16
+ :foobar => { bar: 'bar' },
17
+ :aa_zz => [{ bar: :bar, a_z: 'a-z' }]
18
+ )
19
+ end
20
+ end
21
+
22
+ describe '#dasherize' do
23
+ subject { DeepHashTransformer.new(hash).dasherize }
24
+
25
+ it do
26
+ is_expected.to eq(
27
+ Integer => 123,
28
+ 'foobar' => { 'bar' => 'bar' },
29
+ 'aa-zz' => [{ 'bar' => :bar, 'a-z' => 'a-z' }]
30
+ )
31
+ end
32
+ end
33
+
34
+ describe '#identity' do
35
+ subject { DeepHashTransformer.new(hash).identity }
36
+
37
+ it do
38
+ is_expected.to eq(
39
+ Integer => 123,
40
+ :foobar => { bar: 'bar' },
41
+ 'aa_zz' => [{ 'bar' => :bar, 'a-z' => 'a-z' }]
42
+ )
43
+ end
44
+ end
45
+
46
+ describe '#stringify' do
47
+ subject { DeepHashTransformer.new(hash).stringify }
48
+
49
+ it do
50
+ is_expected.to eq(
51
+ Integer => 123,
52
+ 'foobar' => { 'bar' => 'bar' },
53
+ 'aa_zz' => [{ 'bar' => :bar, 'a-z' => 'a-z' }]
54
+ )
55
+ end
56
+ end
57
+
58
+ describe '#symbolize' do
59
+ subject { DeepHashTransformer.new(hash).symbolize }
60
+
61
+ it do
62
+ is_expected.to eq(
63
+ Integer => 123,
64
+ :foobar => { bar: 'bar' },
65
+ :aa_zz => [{ :bar => :bar, :'a-z' => 'a-z' }]
66
+ )
67
+ end
68
+ end
69
+
70
+ describe '#underscore' do
71
+ subject { DeepHashTransformer.new(hash).underscore }
72
+
73
+ it do
74
+ is_expected.to eq(
75
+ Integer => 123,
76
+ 'foobar' => { 'bar' => 'bar' },
77
+ 'aa_zz' => [{ 'bar' => :bar, 'a_z' => 'a-z' }]
78
+ )
79
+ end
80
+ end
81
+
82
+ it 'has a version number' do
83
+ expect(DeepHashTransformer::VERSION).not_to be nil
84
+ end
85
+ end
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ require 'deep_hash_transformer'
3
+
4
+ RSpec.configure do |config|
5
+ # Enable flags like --only-failures and --next-failure
6
+ config.example_status_persistence_file_path = '.rspec_status'
7
+
8
+ config.expect_with :rspec do |c|
9
+ c.syntax = :expect
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deep_hash_transformer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Spickermann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
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: rspec
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: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
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: " DeepHashTransformer helps to transform deeply nested hash structures\n"
70
+ email:
71
+ - spickermann@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - lib/deep_hash_transformer.rb
78
+ - lib/deep_hash_transformer/version.rb
79
+ - spec/deep_hash_transformer_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/spickermann/deep_hash_transformer
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.11
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Transforms deeply nested hash structure
105
+ test_files: []