hash_map 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +25 -28
- data/README.md +42 -0
- data/lib/hash_map/base.rb +14 -14
- data/lib/hash_map/mapper.rb +6 -6
- data/lib/hash_map/matchers.rb +129 -0
- data/lib/hash_map/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bae76ad044288eb46403cd3ea870146d210bc91
|
4
|
+
data.tar.gz: 268a72ffcdc09a0e79a1c7bce05adbeb7f3b3ee2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b938e9d58a66274c45b34769ed5de353189b62c0d4b59ade8a6307057c7accadc4b2b64cd0429186c36e133c322c852ab401fc31d58dc5bfc6797c1989ccfe6
|
7
|
+
data.tar.gz: 4dc6a6f3ab70adbb9e31592614f5dee586d9911f7e99a9df8e8311c2aeaf2041ffb99a48f2f8219fb07d473beaac4701c5e9eee1ced7de66b23860b584a6a544
|
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2016-
|
3
|
+
# on 2016-09-13 00:25:45 +0200 using RuboCop version 0.40.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
@@ -8,19 +8,28 @@
|
|
8
8
|
|
9
9
|
# Offense count: 4
|
10
10
|
# Cop supports --auto-correct.
|
11
|
-
# Configuration parameters:
|
11
|
+
# Configuration parameters: AllowUnusedKeywordArguments.
|
12
12
|
Lint/UnusedBlockArgument:
|
13
13
|
Exclude:
|
14
14
|
- 'spec/examples/blocks_spec.rb'
|
15
15
|
- 'spec/hash_map/dsl_spec.rb'
|
16
16
|
- 'spec/hash_map/mapper_spec.rb'
|
17
17
|
|
18
|
-
# Offense count:
|
18
|
+
# Offense count: 1
|
19
|
+
Metrics/AbcSize:
|
20
|
+
Max: 17
|
21
|
+
|
22
|
+
# Offense count: 37
|
19
23
|
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
|
20
24
|
# URISchemes: http, https
|
21
25
|
Metrics/LineLength:
|
22
26
|
Max: 238
|
23
27
|
|
28
|
+
# Offense count: 1
|
29
|
+
# Configuration parameters: CountComments.
|
30
|
+
Metrics/MethodLength:
|
31
|
+
Max: 12
|
32
|
+
|
24
33
|
# Offense count: 1
|
25
34
|
# Configuration parameters: CountComments.
|
26
35
|
Metrics/ModuleLength:
|
@@ -41,7 +50,15 @@ Style/Alias:
|
|
41
50
|
Exclude:
|
42
51
|
- 'lib/hash_map/base.rb'
|
43
52
|
|
44
|
-
# Offense count:
|
53
|
+
# Offense count: 1
|
54
|
+
# Cop supports --auto-correct.
|
55
|
+
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
56
|
+
# SupportedStyles: braces, no_braces, context_dependent
|
57
|
+
Style/BracesAroundHashParameters:
|
58
|
+
Exclude:
|
59
|
+
- 'lib/hash_map/matchers.rb'
|
60
|
+
|
61
|
+
# Offense count: 10
|
45
62
|
Style/Documentation:
|
46
63
|
Exclude:
|
47
64
|
- 'spec/**/*'
|
@@ -53,6 +70,7 @@ Style/Documentation:
|
|
53
70
|
- 'lib/hash_map/dsl.rb'
|
54
71
|
- 'lib/hash_map/json_adapter.rb'
|
55
72
|
- 'lib/hash_map/mapper.rb'
|
73
|
+
- 'lib/hash_map/matchers.rb'
|
56
74
|
|
57
75
|
# Offense count: 1
|
58
76
|
# Cop supports --auto-correct.
|
@@ -94,13 +112,6 @@ Style/MutableConstant:
|
|
94
112
|
Style/NumericLiterals:
|
95
113
|
MinDigits: 6
|
96
114
|
|
97
|
-
# Offense count: 2
|
98
|
-
# Cop supports --auto-correct.
|
99
|
-
# Configuration parameters: PreferredDelimiters.
|
100
|
-
Style/PercentLiteralDelimiters:
|
101
|
-
Exclude:
|
102
|
-
- 'hash_map.gemspec'
|
103
|
-
|
104
115
|
# Offense count: 3
|
105
116
|
# Cop supports --auto-correct.
|
106
117
|
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
@@ -110,7 +121,7 @@ Style/SignalException:
|
|
110
121
|
- 'lib/hash_map/dsl.rb'
|
111
122
|
- 'lib/hash_map/json_adapter.rb'
|
112
123
|
|
113
|
-
# Offense count:
|
124
|
+
# Offense count: 5
|
114
125
|
# Cop supports --auto-correct.
|
115
126
|
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
116
127
|
# SupportedStyles: space, no_space
|
@@ -138,28 +149,20 @@ Style/SpaceInsideBrackets:
|
|
138
149
|
Exclude:
|
139
150
|
- 'spec/examples/collections_spec.rb'
|
140
151
|
|
141
|
-
# Offense count:
|
152
|
+
# Offense count: 4
|
142
153
|
# Cop supports --auto-correct.
|
143
154
|
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles.
|
144
155
|
# SupportedStyles: space, no_space
|
145
156
|
Style/SpaceInsideHashLiteralBraces:
|
146
157
|
Enabled: false
|
147
158
|
|
148
|
-
# Offense count:
|
159
|
+
# Offense count: 35
|
149
160
|
# Cop supports --auto-correct.
|
150
161
|
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
|
151
162
|
# SupportedStyles: single_quotes, double_quotes
|
152
163
|
Style/StringLiterals:
|
153
164
|
Enabled: false
|
154
165
|
|
155
|
-
# Offense count: 1
|
156
|
-
# Cop supports --auto-correct.
|
157
|
-
# Configuration parameters: EnforcedStyle, SupportedStyles.
|
158
|
-
# SupportedStyles: final_newline, final_blank_line
|
159
|
-
Style/TrailingBlankLines:
|
160
|
-
Exclude:
|
161
|
-
- 'lib/hash_map.rb'
|
162
|
-
|
163
166
|
# Offense count: 3
|
164
167
|
# Cop supports --auto-correct.
|
165
168
|
# Configuration parameters: EnforcedStyleForMultiline, SupportedStyles.
|
@@ -169,9 +172,3 @@ Style/TrailingCommaInLiteral:
|
|
169
172
|
- 'spec/examples/defaults_spec.rb'
|
170
173
|
- 'spec/hash_map/dsl_spec.rb'
|
171
174
|
- 'spec/hash_map/mapper_spec.rb'
|
172
|
-
|
173
|
-
# Offense count: 2
|
174
|
-
# Cop supports --auto-correct.
|
175
|
-
Style/UnneededPercentQ:
|
176
|
-
Exclude:
|
177
|
-
- 'hash_map.gemspec'
|
data/README.md
CHANGED
@@ -362,6 +362,48 @@ hash.hash_map_with(UserMapper)
|
|
362
362
|
# => {"name"=>"John", "surname"=>"Doe"}
|
363
363
|
```
|
364
364
|
|
365
|
+
### Testing
|
366
|
+
|
367
|
+
#### RSpec
|
368
|
+
|
369
|
+
__hash_mapped__
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
it do
|
373
|
+
output = { name: :hello }
|
374
|
+
expect(output).to hash_mapped(:name)
|
375
|
+
end
|
376
|
+
```
|
377
|
+
|
378
|
+
`from`
|
379
|
+
```ruby
|
380
|
+
it do
|
381
|
+
original = { first_name: :hello }
|
382
|
+
output = { name: :hello }
|
383
|
+
expect(output).to hash_mapped(:name).from(original, :first_name)
|
384
|
+
end
|
385
|
+
|
386
|
+
it do
|
387
|
+
original = { user: { first_name: :hello } }
|
388
|
+
output = { name: :hello }
|
389
|
+
expect(output).to hash_mapped(:name).from(original, :user, :first_name)
|
390
|
+
end
|
391
|
+
|
392
|
+
it do
|
393
|
+
original = { user: { first_name: :hello } }
|
394
|
+
output = { user: { name: :hello } }
|
395
|
+
expect(output).to hash_mapped(:user, :name).from(original, :user, :first_name)
|
396
|
+
end
|
397
|
+
```
|
398
|
+
|
399
|
+
`and_eq`
|
400
|
+
|
401
|
+
```ruby
|
402
|
+
it do
|
403
|
+
output = { user: { name: :hello } }
|
404
|
+
expect(output).to hash_mapped(:user, :name).and_eq(:hello)
|
405
|
+
end
|
406
|
+
```
|
365
407
|
|
366
408
|
### Motivation
|
367
409
|
I got bored of doing this:
|
data/lib/hash_map/base.rb
CHANGED
@@ -33,23 +33,13 @@ module HashMap
|
|
33
33
|
private
|
34
34
|
|
35
35
|
def _transforms_output(output)
|
36
|
-
|
37
|
-
|
38
|
-
proccess.call(out)
|
39
|
-
end
|
40
|
-
else
|
41
|
-
output
|
42
|
-
end
|
36
|
+
middlewares = self.class.dsl.instance_variable_get(:@transform_output)
|
37
|
+
run_middlewares(middlewares, output)
|
43
38
|
end
|
44
39
|
|
45
40
|
def _transforms_input(input)
|
46
|
-
|
47
|
-
|
48
|
-
proccess.call(out)
|
49
|
-
end
|
50
|
-
else
|
51
|
-
input
|
52
|
-
end
|
41
|
+
middlewares = self.class.dsl.instance_variable_get(:@transform_input)
|
42
|
+
run_middlewares(middlewares, input)
|
53
43
|
end
|
54
44
|
|
55
45
|
def prepare_input(input)
|
@@ -60,5 +50,15 @@ module HashMap
|
|
60
50
|
input
|
61
51
|
end
|
62
52
|
end
|
53
|
+
|
54
|
+
def run_middlewares(middlewares, input)
|
55
|
+
if middlewares
|
56
|
+
middlewares.inject(input) do |out, proccess|
|
57
|
+
proccess.call(out)
|
58
|
+
end
|
59
|
+
else
|
60
|
+
input
|
61
|
+
end
|
62
|
+
end
|
63
63
|
end
|
64
64
|
end
|
data/lib/hash_map/mapper.rb
CHANGED
@@ -28,7 +28,7 @@ module HashMap
|
|
28
28
|
nil_to_default(value, struct)
|
29
29
|
end
|
30
30
|
|
31
|
-
def after_each_middleware(value,
|
31
|
+
def after_each_middleware(value, _key)
|
32
32
|
after_each_callbacks.inject(value) do |output, middle|
|
33
33
|
middle.call(output)
|
34
34
|
end
|
@@ -57,11 +57,11 @@ module HashMap
|
|
57
57
|
def execute_block(struct)
|
58
58
|
block = struct[:proc]
|
59
59
|
value = if struct[:from_child]
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
nested = get_value_from_key(struct, :from_child)
|
61
|
+
hash_map.instance_exec nested, original, &block
|
62
|
+
else
|
63
|
+
hash_map.instance_exec original, original, &block
|
64
|
+
end
|
65
65
|
after_each_middleware(value, struct[:key].last)
|
66
66
|
end
|
67
67
|
|
@@ -0,0 +1,129 @@
|
|
1
|
+
module HashMap
|
2
|
+
module Matchers
|
3
|
+
def hash_mapped(*original)
|
4
|
+
HashMappedMatcher.new(original)
|
5
|
+
end
|
6
|
+
|
7
|
+
class HashMappedMatcher
|
8
|
+
attr_reader :key, :mapped_hash, :original_hash, :from_key, :mapped_has_key,
|
9
|
+
:description_messages, :failure_messages, :expected, :expected_provided
|
10
|
+
|
11
|
+
def initialize(key)
|
12
|
+
@key = key
|
13
|
+
@error = []
|
14
|
+
@description_messages = []
|
15
|
+
@failure_messages = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def matches?(hash)
|
19
|
+
@mapped_hash = hash
|
20
|
+
_has_key && _from && equality
|
21
|
+
end
|
22
|
+
|
23
|
+
def description
|
24
|
+
description_messages.join(', ')
|
25
|
+
end
|
26
|
+
|
27
|
+
def failure_message
|
28
|
+
failure_messages.join(', ')
|
29
|
+
end
|
30
|
+
|
31
|
+
def from(original_hash, *from_key)
|
32
|
+
@original_hash = original_hash
|
33
|
+
@from_key = from_key
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def and_eq(expected)
|
38
|
+
@expected_provided = true
|
39
|
+
@expected = expected
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def mapped_has_key?
|
46
|
+
mapped_results[:has_key]
|
47
|
+
end
|
48
|
+
|
49
|
+
def mapped_value
|
50
|
+
mapped_results[:value]
|
51
|
+
end
|
52
|
+
|
53
|
+
def original_has_key?
|
54
|
+
original_results[:has_key]
|
55
|
+
end
|
56
|
+
|
57
|
+
def original_value
|
58
|
+
original_results[:value]
|
59
|
+
end
|
60
|
+
|
61
|
+
def mapped_results
|
62
|
+
@mapped_results ||= _nested_keys(mapped_hash, key)
|
63
|
+
end
|
64
|
+
|
65
|
+
def original_results
|
66
|
+
@original_results ||= _nested_keys(original_hash, from_key)
|
67
|
+
end
|
68
|
+
|
69
|
+
def _has_key
|
70
|
+
if mapped_has_key?
|
71
|
+
description_messages << "have key #{key_to_message} after been mapped"
|
72
|
+
else
|
73
|
+
failure_messages << "has no key #{key_to_message} after been mapped"
|
74
|
+
false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def _from
|
79
|
+
return true unless original_hash
|
80
|
+
if original_has_key?
|
81
|
+
if original_value == mapped_value
|
82
|
+
description_messages << "#{key_to_message} and original #{from_key_to_message} are the same"
|
83
|
+
else
|
84
|
+
failure_messages << "#{key_to_message} and original #{from_key_to_message} are NOT the same"
|
85
|
+
false
|
86
|
+
end
|
87
|
+
else
|
88
|
+
failure_messages << "original has no key: #{from_key_to_message}"
|
89
|
+
false
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def from_key_to_message
|
94
|
+
keys_to_message from_key
|
95
|
+
end
|
96
|
+
|
97
|
+
def key_to_message
|
98
|
+
keys_to_message key
|
99
|
+
end
|
100
|
+
|
101
|
+
def keys_to_message(keys)
|
102
|
+
children = keys.map { |k| key_representation(k) }.join(' -> ')
|
103
|
+
"`#{children}`"
|
104
|
+
end
|
105
|
+
|
106
|
+
def key_representation(k)
|
107
|
+
k.is_a?(Symbol) ? ":#{k}" : "'#{k}'"
|
108
|
+
end
|
109
|
+
|
110
|
+
def equality
|
111
|
+
return true unless expected_provided
|
112
|
+
if mapped_value == expected
|
113
|
+
description_messages << "and eq `#{expected}`"
|
114
|
+
else
|
115
|
+
failure_messages << "key #{key_to_message} expected to eq `#{expected}`"
|
116
|
+
false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def _nested_keys(hash, keys)
|
121
|
+
keys.inject({ value: hash, has_key: true }) do |out, key|
|
122
|
+
return { value: nil, has_key: false } unless out[:value]
|
123
|
+
return { value: nil, has_key: false } unless out[:has_key]
|
124
|
+
{ value: out[:value][key], has_key: out[:value].key?(key) }
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/lib/hash_map/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Pañach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- lib/hash_map/dsl.rb
|
95
95
|
- lib/hash_map/json_adapter.rb
|
96
96
|
- lib/hash_map/mapper.rb
|
97
|
+
- lib/hash_map/matchers.rb
|
97
98
|
- lib/hash_map/plugins.rb
|
98
99
|
- lib/hash_map/version.rb
|
99
100
|
homepage: https://github.com/arturictus/hash_map
|
@@ -116,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
117
|
version: '0'
|
117
118
|
requirements: []
|
118
119
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.
|
120
|
+
rubygems_version: 2.2.3
|
120
121
|
signing_key:
|
121
122
|
specification_version: 4
|
122
123
|
summary: Library to map easily hashes.
|