data_maps 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/Gemfile.lock +11 -11
- data/README.md +30 -23
- data/data_maps.gemspec +6 -3
- data/lib/data_maps/concerns/configurable.rb +14 -0
- data/lib/data_maps/concerns/factory.rb +1 -1
- data/lib/data_maps/condition.rb +2 -3
- data/lib/data_maps/converter/base.rb +13 -0
- data/lib/data_maps/converter/{keys.rb → key.rb} +1 -1
- data/lib/data_maps/dsl/mapping/conditions_dsl.rb +21 -0
- data/lib/data_maps/dsl/mapping/field_dsl.rb +32 -0
- data/lib/data_maps/dsl/mapping_dsl.rb +16 -0
- data/lib/data_maps/mapping.rb +3 -0
- data/lib/data_maps/statement.rb +32 -7
- data/lib/data_maps/then/convert.rb +1 -1
- data/lib/data_maps/version.rb +1 -1
- data/lib/data_maps/when/empty.rb +1 -0
- data/lib/data_maps.rb +2 -0
- data/spec/data_maps/concerns/configurable_spec.rb +21 -0
- data/spec/data_maps/concerns/factory_spec.rb +4 -4
- data/spec/data_maps/condition_spec.rb +2 -2
- data/spec/data_maps/converter/base_spec.rb +2 -2
- data/spec/data_maps/converter/{keys_spec.rb → key_spec.rb} +2 -2
- data/spec/data_maps/dsl/mapping/conditions_dsl_spec.rb +15 -0
- data/spec/data_maps/dsl/mapping/field_dsl_spec.rb +48 -0
- data/spec/data_maps/dsl/mapping_dsl_spec.rb +37 -0
- data/spec/data_maps/mapping_spec.rb +5 -5
- data/spec/data_maps/statement_spec.rb +1 -1
- data/spec/data_maps/then/base_spec.rb +2 -2
- data/spec/data_maps/then/convert_spec.rb +6 -1
- data/spec/data_maps/when/base_spec.rb +2 -2
- data/spec/data_maps/when/empty_spec.rb +8 -0
- metadata +43 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a79a64393fa8251abdd2a54b6ca2faafacccfa3d
|
4
|
+
data.tar.gz: 3235818191fc647bc508a57e8e2f7628ded1e82c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0e63152f7f12963838b41ed9a1361fae1a3ce6012a04ad1635dff4780a378e685a3ca6e48963da84a3a9b4fad842a7452a39d811f7bd791a4c1021273ec4a53
|
7
|
+
data.tar.gz: c88340e27fd74d55637de2d0357dd618e9c46cadafeb47906765f9c156d0b67d62c1e8486383e524d81b70c92cb80418bfec6a250b2ff78edee528cee8b6faf3
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
data_maps (0.2.0)
|
5
|
-
activesupport (~> 3.2.21)
|
5
|
+
activesupport (~> 3.2, >= 3.2.21)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -10,7 +10,7 @@ GEM
|
|
10
10
|
activesupport (3.2.21)
|
11
11
|
i18n (~> 0.6, >= 0.6.4)
|
12
12
|
multi_json (~> 1.0)
|
13
|
-
codeclimate-test-reporter (0.4.
|
13
|
+
codeclimate-test-reporter (0.4.7)
|
14
14
|
simplecov (>= 0.7.1, < 1.0.0)
|
15
15
|
diff-lcs (1.2.5)
|
16
16
|
docile (1.1.5)
|
@@ -21,28 +21,28 @@ GEM
|
|
21
21
|
rspec-core (~> 3.2.0)
|
22
22
|
rspec-expectations (~> 3.2.0)
|
23
23
|
rspec-mocks (~> 3.2.0)
|
24
|
-
rspec-core (3.2.
|
24
|
+
rspec-core (3.2.1)
|
25
25
|
rspec-support (~> 3.2.0)
|
26
26
|
rspec-expectations (3.2.0)
|
27
27
|
diff-lcs (>= 1.2.0, < 2.0)
|
28
28
|
rspec-support (~> 3.2.0)
|
29
|
-
rspec-mocks (3.2.
|
29
|
+
rspec-mocks (3.2.1)
|
30
30
|
diff-lcs (>= 1.2.0, < 2.0)
|
31
31
|
rspec-support (~> 3.2.0)
|
32
|
-
rspec-support (3.2.
|
33
|
-
simplecov (0.9.
|
32
|
+
rspec-support (3.2.2)
|
33
|
+
simplecov (0.9.2)
|
34
34
|
docile (~> 1.1.0)
|
35
35
|
multi_json (~> 1.0)
|
36
|
-
simplecov-html (~> 0.
|
37
|
-
simplecov-html (0.
|
36
|
+
simplecov-html (~> 0.9.0)
|
37
|
+
simplecov-html (0.9.0)
|
38
38
|
|
39
39
|
PLATFORMS
|
40
40
|
ruby
|
41
41
|
|
42
42
|
DEPENDENCIES
|
43
|
-
bundler (~> 1.
|
44
|
-
codeclimate-test-reporter
|
43
|
+
bundler (~> 1.8)
|
44
|
+
codeclimate-test-reporter (~> 0.4.7)
|
45
45
|
data_maps!
|
46
46
|
rake (~> 10.0)
|
47
47
|
rspec (~> 3.2)
|
48
|
-
simplecov (~> 0.9.
|
48
|
+
simplecov (~> 0.9.2)
|
data/README.md
CHANGED
@@ -4,9 +4,12 @@ DataMaps
|
|
4
4
|
[](https://codeclimate.com/github/dino115/data_maps)
|
5
5
|
[](https://codeclimate.com/github/dino115/data_maps)
|
6
6
|
[](http://badge.fury.io/rb/data_maps)
|
7
|
+
[](https://gemnasium.com/dino115/data_maps)
|
7
8
|
|
8
9
|
Create mappings to convert structured data into another format!
|
9
|
-
This is useful
|
10
|
+
This is useful when you need a dynamic generated or serializable mapping.
|
11
|
+
|
12
|
+
**Attention: The API can change before the gem version reach 1.0!**
|
10
13
|
|
11
14
|
## Installation
|
12
15
|
|
@@ -215,9 +218,9 @@ The only exception is when using `then: { filter: true }`, then the execution br
|
|
215
218
|
Apply the configured converter. See converter section for more information.
|
216
219
|
|
217
220
|
```ruby
|
218
|
-
convert:
|
219
|
-
numeric: 'Integer'
|
220
|
-
|
221
|
+
convert: [
|
222
|
+
{ apply: :numeric, option: 'Integer' }
|
223
|
+
]
|
221
224
|
```
|
222
225
|
- **Then: filter**
|
223
226
|
When this is set to true then the whole field will filtered.
|
@@ -249,12 +252,9 @@ Apply one or many converters to the input data. Converters applied procedural.
|
|
249
252
|
```ruby
|
250
253
|
'field' => {
|
251
254
|
from: 'source',
|
252
|
-
convert:
|
253
|
-
map: {
|
254
|
-
|
255
|
-
2: 'B'
|
256
|
-
}
|
257
|
-
}
|
255
|
+
convert: [
|
256
|
+
{ apply: :map, option: { 1: 'A', 2: 'B' } }
|
257
|
+
]
|
258
258
|
}
|
259
259
|
```
|
260
260
|
|
@@ -266,7 +266,8 @@ Apply one or many converters to the input data. Converters applied procedural.
|
|
266
266
|
For arrays and hashes it returns nil if the value is not in the mapping. For flat values it returns the original data.
|
267
267
|
|
268
268
|
```ruby
|
269
|
-
|
269
|
+
apply: :map,
|
270
|
+
option: {
|
270
271
|
from: to
|
271
272
|
}
|
272
273
|
```
|
@@ -275,30 +276,32 @@ Apply one or many converters to the input data. Converters applied procedural.
|
|
275
276
|
Can raise an error if the value is not convertable.
|
276
277
|
|
277
278
|
```ruby
|
278
|
-
|
279
|
-
|
280
|
-
|
279
|
+
apply: :numeric,
|
280
|
+
option 'Integer'
|
281
|
+
# option: 'Float'
|
282
|
+
# option: 2
|
281
283
|
```
|
282
284
|
- **Converter: String**
|
283
285
|
Cast explicit to string. Doesn't work with collections.
|
284
286
|
Can raise error if the value is not convertable.
|
285
287
|
|
286
288
|
```ruby
|
287
|
-
|
289
|
+
apply: :string
|
288
290
|
```
|
289
291
|
- **Converter: Boolean**
|
290
292
|
Cast explicit to bool (by double negotiation). Doesn't work with collections.
|
291
293
|
Can return unexpected values, e.g. a double negotiated empty array is true! `!![] #=> true`
|
292
294
|
|
293
295
|
```ruby
|
294
|
-
|
296
|
+
apply: :bool
|
295
297
|
```
|
296
298
|
- **Converter: keys**
|
297
299
|
This maps the hash keys when the input data is a hash or when you select multiple *from* fields. Only works with hashes.
|
298
300
|
Return the original data when the data isn't a hash.
|
299
301
|
|
300
302
|
```ruby
|
301
|
-
|
303
|
+
apply: :key,
|
304
|
+
option: {
|
302
305
|
'address1' => 'street'
|
303
306
|
}
|
304
307
|
```
|
@@ -306,21 +309,24 @@ Apply one or many converters to the input data. Converters applied procedural.
|
|
306
309
|
This prefixes the data with a given value. Call `to_s` on data and always returns a string.
|
307
310
|
|
308
311
|
```ruby
|
309
|
-
|
312
|
+
apply: :prefix,
|
313
|
+
option: '$'
|
310
314
|
```
|
311
315
|
- **Converter: Postfix**
|
312
316
|
This postfixes the data with a given value. Call `to_s` on data and always returns a string.
|
313
317
|
|
314
318
|
```ruby
|
315
|
-
|
319
|
+
apply: :postfix,
|
320
|
+
option: '€'
|
316
321
|
```
|
317
322
|
- **Converter: ruby**
|
318
323
|
Apply any method on the current data object.
|
319
324
|
|
320
325
|
```ruby
|
321
|
-
|
322
|
-
|
323
|
-
|
326
|
+
apply: :ruby,
|
327
|
+
option: :upcase
|
328
|
+
# option: [:slice, 5]
|
329
|
+
# option: [:join, ', ']
|
324
330
|
```
|
325
331
|
- **Converter: custom**
|
326
332
|
Define your own *converter* by defining them in the `DataMaps::Converter` module.
|
@@ -336,7 +342,8 @@ Apply one or many converters to the input data. Converters applied procedural.
|
|
336
342
|
```
|
337
343
|
|
338
344
|
```ruby
|
339
|
-
|
345
|
+
apply: to_person_object,
|
346
|
+
option: { as: :importer } # passed value are available via option
|
340
347
|
```
|
341
348
|
|
342
349
|
Have fun using the `DataMaps` gem :)
|
data/data_maps.gemspec
CHANGED
@@ -18,10 +18,13 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.
|
21
|
+
spec.required_ruby_version = '>= 1.9.2'
|
22
22
|
|
23
|
-
spec.
|
23
|
+
spec.add_runtime_dependency 'activesupport', '~> 3.2', '>= 3.2.21'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.8'
|
24
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
25
27
|
spec.add_development_dependency 'rspec', '~> 3.2'
|
26
|
-
spec.add_development_dependency 'simplecov', '~> 0.9.
|
28
|
+
spec.add_development_dependency 'simplecov', '~> 0.9.2'
|
29
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4.7'
|
27
30
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module DataMaps
|
2
|
+
module Concerns
|
3
|
+
module Configurable
|
4
|
+
# Concern to allow configuration via instance evaluation
|
5
|
+
#
|
6
|
+
# @param [Block] block
|
7
|
+
# @return self
|
8
|
+
def configure(&block)
|
9
|
+
instance_eval &block if block_given?
|
10
|
+
self
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -19,7 +19,7 @@ module DataMaps
|
|
19
19
|
#
|
20
20
|
# @param [Hash] mapping
|
21
21
|
# @return [Array] of factorized classes
|
22
|
-
def
|
22
|
+
def factory_from_map(mapping)
|
23
23
|
raise ArgumentError.new("#{self.name} mapping has to be an hash") unless mapping.is_a?(Hash)
|
24
24
|
|
25
25
|
mapping.map do |name, option|
|
data/lib/data_maps/condition.rb
CHANGED
@@ -10,15 +10,14 @@ module DataMaps
|
|
10
10
|
# Helper method to create conditions from a mapping
|
11
11
|
#
|
12
12
|
# @param [Array] mapping
|
13
|
-
#
|
14
13
|
# @return [Array] of Condition
|
15
14
|
def self.create_from_map(mapping)
|
16
15
|
raise ArgumentError.new('Conditions mapping has to be an array') unless mapping.is_a?(Array)
|
17
16
|
|
18
17
|
mapping.map do |condition|
|
19
18
|
self.new(
|
20
|
-
DataMaps::When.
|
21
|
-
DataMaps::Then.
|
19
|
+
DataMaps::When.factory_from_map(condition[:when]),
|
20
|
+
DataMaps::Then.factory_from_map(condition[:then])
|
22
21
|
)
|
23
22
|
end
|
24
23
|
end
|
@@ -2,6 +2,19 @@ module DataMaps
|
|
2
2
|
module Converter
|
3
3
|
extend DataMaps::Concerns::Factory
|
4
4
|
|
5
|
+
# Helper method to create converts from a mapping_hash
|
6
|
+
#
|
7
|
+
# @param [Array] mapping
|
8
|
+
# @return [Array] of factorized classes
|
9
|
+
def self.create_from_map(mapping)
|
10
|
+
raise ArgumentError.new('Converter mapping has to be an array') unless mapping.is_a?(Array)
|
11
|
+
|
12
|
+
mapping.map do |converter|
|
13
|
+
raise ArgumentError.new('Converter must be specified with the apply key') unless converter.key?(:apply)
|
14
|
+
self.factory(converter[:apply], converter[:option])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
5
18
|
# Base class for converter
|
6
19
|
#
|
7
20
|
# @since 0.0.1
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module DataMaps
|
2
|
+
module Dsl
|
3
|
+
module Mapping
|
4
|
+
# Structure to describe a field mapping
|
5
|
+
class ConditionsDsl < Struct.new(:whens, :thens)
|
6
|
+
include DataMaps::Concerns::Configurable
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
self.whens = {}
|
10
|
+
self.thens = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
# Serialize DSL to an Hash
|
14
|
+
def to_h
|
15
|
+
data = super
|
16
|
+
data.stringify_keys
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module DataMaps
|
2
|
+
module Dsl
|
3
|
+
module Mapping
|
4
|
+
# Structure to describe a field mapping
|
5
|
+
class FieldMappingDsl < Struct.new(:from, :conditions, :converter)
|
6
|
+
include DataMaps::Concerns::Configurable
|
7
|
+
|
8
|
+
def initialize(options = {})
|
9
|
+
self.from = options[:from]
|
10
|
+
self.conditions = []
|
11
|
+
self.converter = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_condition(&block)
|
15
|
+
dsl = DataMaps::Dsl::Mapping::ConditionsDsl.new
|
16
|
+
dsl.configure(&block) if block_given?
|
17
|
+
self.conditions << dsl.to_h
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_converter(converter, options = nil)
|
21
|
+
self.converter << { apply: converter, option: options }
|
22
|
+
end
|
23
|
+
|
24
|
+
# Serialize DSL to an Hash
|
25
|
+
def to_h
|
26
|
+
data = super
|
27
|
+
data.stringify_keys
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module DataMaps
|
2
|
+
module Dsl
|
3
|
+
module Mapping
|
4
|
+
# DSL method to describe a field
|
5
|
+
#
|
6
|
+
# @param [String|Symbol] destination
|
7
|
+
# @param [Hash] options
|
8
|
+
# @param [Block] block
|
9
|
+
def field(destination, options = {}, &block)
|
10
|
+
dsl = FieldMappingDsl.new(options)
|
11
|
+
dsl.configure(&block) if block_given?
|
12
|
+
@mapping_hash[destination.to_s] = dsl.to_h
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/data_maps/mapping.rb
CHANGED
@@ -5,6 +5,9 @@ module DataMaps
|
|
5
5
|
# attr_reader [Hash] mapping the compiled mapping
|
6
6
|
# attr_reader [Hash] mapping_hash the mapping description
|
7
7
|
class Mapping
|
8
|
+
include DataMaps::Concerns::Configurable
|
9
|
+
include DataMaps::Dsl::Mapping
|
10
|
+
|
8
11
|
attr_reader :mapping, :mapping_hash
|
9
12
|
|
10
13
|
# Initializer for the Mapping class
|
data/lib/data_maps/statement.rb
CHANGED
@@ -12,14 +12,13 @@ module DataMaps
|
|
12
12
|
# Create statement from a mapping hash
|
13
13
|
#
|
14
14
|
# @param [Hash] mapping
|
15
|
-
#
|
16
15
|
# @return [Statement]
|
17
16
|
def self.create_from_map(mapping)
|
18
17
|
self.new(
|
19
18
|
mapping[:from],
|
20
19
|
mapping[:to],
|
21
20
|
DataMaps::Condition.create_from_map(mapping[:conditions] || []),
|
22
|
-
DataMaps::Converter.create_from_map(mapping[:converter] ||
|
21
|
+
DataMaps::Converter.create_from_map(mapping[:converter] || [])
|
23
22
|
)
|
24
23
|
end
|
25
24
|
|
@@ -45,11 +44,7 @@ module DataMaps
|
|
45
44
|
# @param [mixed] data
|
46
45
|
# @return [Array] key and value of the result
|
47
46
|
def execute(data)
|
48
|
-
data =
|
49
|
-
when Array then from.reduce(data) { |val, f| val.fetch(f) if val.is_a?(Hash) }
|
50
|
-
when Hash then Hash[from.map { |k,v| [v.is_a?(TrueClass) ? k.to_s : v.to_s, data[k.to_s]] }]
|
51
|
-
else data[from.to_s]
|
52
|
-
end
|
47
|
+
data = _fetch_from_data(data)
|
53
48
|
|
54
49
|
data = execute_conditions(data)
|
55
50
|
data = execute_converter(data)
|
@@ -80,5 +75,35 @@ module DataMaps
|
|
80
75
|
|
81
76
|
data
|
82
77
|
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
# Helper method to fetch the estimated value from data
|
82
|
+
#
|
83
|
+
# @param [mixed] data
|
84
|
+
# @return [mixed] estimated value
|
85
|
+
def _fetch_from_data(data)
|
86
|
+
case from
|
87
|
+
when Array then _fetch_value_from_nested(data)
|
88
|
+
when Hash then _fetch_values_from_hash(data)
|
89
|
+
else data[from.to_s]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# Helper method to fetch the value from a nested data structure
|
94
|
+
#
|
95
|
+
# @param [mixed] data
|
96
|
+
# @return [mixed] estimated value
|
97
|
+
def _fetch_value_from_nested(data)
|
98
|
+
from.reduce(data) { |val, f| val.fetch(f) if val.is_a?(Hash) }
|
99
|
+
end
|
100
|
+
|
101
|
+
# Helper method to fetch values from a hash
|
102
|
+
#
|
103
|
+
# @param [mixed] data
|
104
|
+
# @return [mixed] estimated value
|
105
|
+
def _fetch_values_from_hash(data)
|
106
|
+
Hash[from.map { |k,v| [v.is_a?(TrueClass) ? k.to_s : v.to_s, data[k.to_s]] }]
|
107
|
+
end
|
83
108
|
end
|
84
109
|
end
|
data/lib/data_maps/version.rb
CHANGED
data/lib/data_maps/when/empty.rb
CHANGED
data/lib/data_maps.rb
CHANGED
@@ -4,6 +4,8 @@ require 'active_support/all'
|
|
4
4
|
# Require all project files
|
5
5
|
Dir.glob(File.join(__dir__, 'data_maps', 'executable.rb'), &method(:require))
|
6
6
|
Dir.glob(File.join(__dir__, 'data_maps', '{concerns,errors}', '*.rb'), &method(:require))
|
7
|
+
Dir.glob(File.join(__dir__, 'data_maps', 'dsl', '{mapping}', '*.rb'), &method(:require))
|
8
|
+
Dir.glob(File.join(__dir__, 'data_maps', 'dsl', '*.rb'), &method(:require))
|
7
9
|
Dir.glob(File.join(__dir__, 'data_maps', '{converter,then,when}', 'base.rb'), &method(:require))
|
8
10
|
Dir.glob(File.join(__dir__, 'data_maps', '{converter,then,when}', '*.rb'), &method(:require))
|
9
11
|
Dir.glob(File.join(__dir__, 'data_maps', '*.rb'), &method(:require))
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMaps::Concerns::Configurable do
|
4
|
+
class Dummy
|
5
|
+
include DataMaps::Concerns::Configurable
|
6
|
+
end
|
7
|
+
|
8
|
+
subject{ Dummy.new }
|
9
|
+
|
10
|
+
describe '#configure' do
|
11
|
+
it 'evaluates the given block in instance context' do
|
12
|
+
expect do |block|
|
13
|
+
subject.configure(&block)
|
14
|
+
end.to yield_with_args subject
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns self' do
|
18
|
+
expect(subject.configure).to eq subject
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -39,16 +39,16 @@ describe DataMaps::Concerns::Factory do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
describe '#
|
42
|
+
describe '#factory_from_map' do
|
43
43
|
it 'raises an error if mapping isn\'t a hash' do
|
44
|
-
expect{ MyModule.
|
45
|
-
expect{ MyModule.
|
44
|
+
expect{ MyModule.factory_from_map([]) }.to raise_error ArgumentError
|
45
|
+
expect{ MyModule.factory_from_map('something') }.to raise_error ArgumentError
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'calls the factory method' do
|
49
49
|
mapping = { a: :b }
|
50
50
|
expect(MyModule).to receive(:factory).with(:a, :b).and_call_original
|
51
|
-
MyModule.
|
51
|
+
MyModule.factory_from_map(mapping)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -7,8 +7,8 @@ describe DataMaps::Condition do
|
|
7
7
|
{ when: { empty: true }, then: { filter: true } }
|
8
8
|
]
|
9
9
|
|
10
|
-
expect(DataMaps::When).to receive(:
|
11
|
-
expect(DataMaps::Then).to receive(:
|
10
|
+
expect(DataMaps::When).to receive(:factory_from_map).with(mapping.first[:when]).and_call_original
|
11
|
+
expect(DataMaps::Then).to receive(:factory_from_map).with(mapping.first[:then]).and_call_original
|
12
12
|
|
13
13
|
expect(DataMaps::Condition).to receive(:new).with([DataMaps::When::Empty], [DataMaps::Then::Filter])
|
14
14
|
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DataMaps::Converter do
|
4
|
-
describe '::
|
4
|
+
describe '::factory_from_map' do
|
5
5
|
it 'creates new converter' do
|
6
6
|
mapping = { ruby: :upcase }
|
7
7
|
|
8
8
|
expect(DataMaps::Converter).to receive(:factory).with(:ruby, :upcase).and_call_original
|
9
9
|
|
10
|
-
DataMaps::Converter.
|
10
|
+
DataMaps::Converter.factory_from_map(mapping)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe DataMaps::Converter::
|
3
|
+
describe DataMaps::Converter::Key do
|
4
4
|
describe '#execute' do
|
5
|
-
subject { DataMaps::Converter::
|
5
|
+
subject { DataMaps::Converter::Key.new({ a: 'x', b: 'y', c: 'z' }) }
|
6
6
|
|
7
7
|
it 'converts the keys' do
|
8
8
|
expect(
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMaps::Dsl::Mapping::ConditionsDsl do
|
4
|
+
subject{ DataMaps::Dsl::Mapping::ConditionsDsl.new }
|
5
|
+
|
6
|
+
describe '#initialze' do
|
7
|
+
it 'set whens to an empty hash' do
|
8
|
+
expect(subject.whens).to eq({})
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'set thens to an empty hash' do
|
12
|
+
expect(subject.thens).to eq({})
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMaps::Dsl::Mapping::FieldMappingDsl do
|
4
|
+
subject{ DataMaps::Dsl::Mapping::FieldMappingDsl.new(from: 'some-field') }
|
5
|
+
|
6
|
+
describe '#initialze' do
|
7
|
+
it 'sets the from attribute from options' do
|
8
|
+
expect(subject.from).to eq 'some-field'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'sets conditions to an empty array' do
|
12
|
+
expect(subject.conditions).to eq []
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'sets converter to an empty hash' do
|
16
|
+
expect(subject.converter).to eq([])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#add_condition' do
|
21
|
+
it 'creates a new ConditionsDsl object' do
|
22
|
+
dsl = DataMaps::Dsl::Mapping::ConditionsDsl.new
|
23
|
+
|
24
|
+
expect(DataMaps::Dsl::Mapping::ConditionsDsl).to receive(:new).with(no_args).and_return(dsl)
|
25
|
+
subject.add_condition
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'calls configure with given block' do
|
29
|
+
expect do |block|
|
30
|
+
subject.add_condition(&block)
|
31
|
+
end.to yield_control
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#add_converter' do
|
36
|
+
it 'adds the defined converter to converter hash' do
|
37
|
+
expect{ subject.add_converter(:ruby, :downcase) }.to change{ subject.converter.count }.from(0).to(1)
|
38
|
+
expect(subject.converter.first).to eq({ apply: :ruby, option: :downcase })
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#to_h' do
|
43
|
+
it 'returns a hash with string keys' do
|
44
|
+
expect(subject.to_h).to be_a Hash
|
45
|
+
expect(subject.to_h.has_key?('from')).to be_truthy
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DataMaps::Dsl::Mapping do
|
4
|
+
class Dummy
|
5
|
+
include DataMaps::Dsl::Mapping
|
6
|
+
|
7
|
+
attr_reader :mapping_hash
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@mapping_hash = {}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject{ Dummy.new }
|
15
|
+
|
16
|
+
describe '#field' do
|
17
|
+
it 'creates a new FieldMappingDsl object with given options' do
|
18
|
+
options = { a: 'A', b: 'B' }
|
19
|
+
dsl = DataMaps::Dsl::Mapping::FieldMappingDsl.new
|
20
|
+
|
21
|
+
expect(DataMaps::Dsl::Mapping::FieldMappingDsl).to receive(:new).with(options).and_return(dsl)
|
22
|
+
subject.field(:myfield, options)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'calls configure with given block' do
|
26
|
+
expect do |block|
|
27
|
+
subject.field(:myfield, {}, &block)
|
28
|
+
end.to yield_control
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'adds the serialized result to the mapping_hash' do
|
32
|
+
subject.field(:myfield, {})
|
33
|
+
|
34
|
+
expect(subject.mapping_hash.key?('myfield')).to be_truthy
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -14,7 +14,7 @@ describe DataMaps::Mapping do
|
|
14
14
|
from: 'source1',
|
15
15
|
conditions: [
|
16
16
|
{ when: { empty: true }, then: { filter: true } },
|
17
|
-
{ when: { regex: /[a-z]/ }, then: { convert: { ruby: :upcase } } },
|
17
|
+
{ when: { regex: /[a-z]/ }, then: { convert: [ { apply: :ruby, option: :upcase } ] } },
|
18
18
|
]
|
19
19
|
}
|
20
20
|
}
|
@@ -24,9 +24,9 @@ describe DataMaps::Mapping do
|
|
24
24
|
{
|
25
25
|
'destination2' => {
|
26
26
|
from: %w[ source2 source3 ],
|
27
|
-
converter:
|
28
|
-
ruby: [:join, ', ']
|
29
|
-
|
27
|
+
converter: [
|
28
|
+
{ apply: :ruby, option: [:join, ', '] }
|
29
|
+
]
|
30
30
|
}
|
31
31
|
}
|
32
32
|
end
|
@@ -112,7 +112,7 @@ describe DataMaps::Mapping do
|
|
112
112
|
end
|
113
113
|
|
114
114
|
# Test mapping generation of a mapping hash with converter
|
115
|
-
describe '
|
115
|
+
describe 'converter mapping hash' do
|
116
116
|
subject{ DataMaps::Mapping.new(converter_mapping_hash) }
|
117
117
|
|
118
118
|
it 'has correct destination key' do
|
@@ -6,7 +6,7 @@ describe DataMaps::Statement do
|
|
6
6
|
mapping = { from: 'a', to: 'b' }
|
7
7
|
|
8
8
|
expect(DataMaps::Condition).to receive(:create_from_map).with([]).and_return([])
|
9
|
-
expect(DataMaps::Converter).to receive(:create_from_map).with(
|
9
|
+
expect(DataMaps::Converter).to receive(:create_from_map).with([]).and_return([])
|
10
10
|
|
11
11
|
expect(DataMaps::Statement).to receive(:new).with('a', 'b', [], [])
|
12
12
|
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DataMaps::Then do
|
4
|
-
describe '::
|
4
|
+
describe '::factory_from_map' do
|
5
5
|
it 'creates new thens' do
|
6
6
|
mapping = { filter: true }
|
7
7
|
|
8
8
|
expect(DataMaps::Then).to receive(:factory).with(:filter, true).and_call_original
|
9
9
|
|
10
|
-
DataMaps::Then.
|
10
|
+
DataMaps::Then.factory_from_map(mapping)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -1,7 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DataMaps::Then::Convert do
|
4
|
-
subject
|
4
|
+
subject do
|
5
|
+
DataMaps::Then::Convert.new([
|
6
|
+
{ apply: :map, option: { a: 'x', b: 'y' } },
|
7
|
+
{ apply: :ruby, option: [:join, ','] }
|
8
|
+
])
|
9
|
+
end
|
5
10
|
|
6
11
|
describe 'initialization' do
|
7
12
|
it 'Creates the converter' do
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DataMaps::When do
|
4
|
-
describe '::
|
4
|
+
describe '::factory_from_map' do
|
5
5
|
it 'creates new whens' do
|
6
6
|
mapping = { empty: true }
|
7
7
|
|
8
8
|
expect(DataMaps::When).to receive(:factory).with(:empty, true).and_call_original
|
9
9
|
|
10
|
-
DataMaps::When.
|
10
|
+
DataMaps::When.factory_from_map(mapping)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -12,6 +12,10 @@ describe DataMaps::When::Empty do
|
|
12
12
|
it 'returns false for non empty values' do
|
13
13
|
expect(subject.execute('something')).to be_falsey
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'returns true for nil values' do
|
17
|
+
expect(subject.execute(nil)).to be_truthy
|
18
|
+
end
|
15
19
|
end
|
16
20
|
|
17
21
|
describe 'empty false' do
|
@@ -24,6 +28,10 @@ describe DataMaps::When::Empty do
|
|
24
28
|
it 'returns true for non empty values' do
|
25
29
|
expect(subject.execute('something')).to be_truthy
|
26
30
|
end
|
31
|
+
|
32
|
+
it 'returns false for nil values' do
|
33
|
+
expect(subject.execute(nil)).to be_falsey
|
34
|
+
end
|
27
35
|
end
|
28
36
|
end
|
29
37
|
end
|
metadata
CHANGED
@@ -1,20 +1,23 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_maps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Axel Wahlen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - ">="
|
18
21
|
- !ruby/object:Gem::Version
|
19
22
|
version: 3.2.21
|
20
23
|
type: :runtime
|
@@ -22,6 +25,9 @@ dependencies:
|
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - ">="
|
25
31
|
- !ruby/object:Gem::Version
|
26
32
|
version: 3.2.21
|
27
33
|
- !ruby/object:Gem::Dependency
|
@@ -30,14 +36,14 @@ dependencies:
|
|
30
36
|
requirements:
|
31
37
|
- - "~>"
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1.
|
39
|
+
version: '1.8'
|
34
40
|
type: :development
|
35
41
|
prerelease: false
|
36
42
|
version_requirements: !ruby/object:Gem::Requirement
|
37
43
|
requirements:
|
38
44
|
- - "~>"
|
39
45
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1.
|
46
|
+
version: '1.8'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: rake
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,14 +78,28 @@ dependencies:
|
|
72
78
|
requirements:
|
73
79
|
- - "~>"
|
74
80
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.9.
|
81
|
+
version: 0.9.2
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 0.9.2
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: codeclimate-test-reporter
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 0.4.7
|
76
96
|
type: :development
|
77
97
|
prerelease: false
|
78
98
|
version_requirements: !ruby/object:Gem::Requirement
|
79
99
|
requirements:
|
80
100
|
- - "~>"
|
81
101
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.
|
102
|
+
version: 0.4.7
|
83
103
|
description: " Maps data to another structure through a mapping "
|
84
104
|
email:
|
85
105
|
- axel.wahlen@mixxt.de
|
@@ -97,16 +117,20 @@ files:
|
|
97
117
|
- Rakefile
|
98
118
|
- data_maps.gemspec
|
99
119
|
- lib/data_maps.rb
|
120
|
+
- lib/data_maps/concerns/configurable.rb
|
100
121
|
- lib/data_maps/concerns/factory.rb
|
101
122
|
- lib/data_maps/condition.rb
|
102
123
|
- lib/data_maps/converter/affixes.rb
|
103
124
|
- lib/data_maps/converter/base.rb
|
104
125
|
- lib/data_maps/converter/bool.rb
|
105
|
-
- lib/data_maps/converter/
|
126
|
+
- lib/data_maps/converter/key.rb
|
106
127
|
- lib/data_maps/converter/map.rb
|
107
128
|
- lib/data_maps/converter/numeric.rb
|
108
129
|
- lib/data_maps/converter/ruby.rb
|
109
130
|
- lib/data_maps/converter/string.rb
|
131
|
+
- lib/data_maps/dsl/mapping/conditions_dsl.rb
|
132
|
+
- lib/data_maps/dsl/mapping/field_dsl.rb
|
133
|
+
- lib/data_maps/dsl/mapping_dsl.rb
|
110
134
|
- lib/data_maps/errors/invalid_data.rb
|
111
135
|
- lib/data_maps/executable.rb
|
112
136
|
- lib/data_maps/filtered_value.rb
|
@@ -122,16 +146,20 @@ files:
|
|
122
146
|
- lib/data_maps/when/comparison.rb
|
123
147
|
- lib/data_maps/when/empty.rb
|
124
148
|
- lib/data_maps/when/regex.rb
|
149
|
+
- spec/data_maps/concerns/configurable_spec.rb
|
125
150
|
- spec/data_maps/concerns/factory_spec.rb
|
126
151
|
- spec/data_maps/condition_spec.rb
|
127
152
|
- spec/data_maps/converter/affixes_spec.rb
|
128
153
|
- spec/data_maps/converter/base_spec.rb
|
129
154
|
- spec/data_maps/converter/bool_spec.rb
|
130
|
-
- spec/data_maps/converter/
|
155
|
+
- spec/data_maps/converter/key_spec.rb
|
131
156
|
- spec/data_maps/converter/map_spec.rb
|
132
157
|
- spec/data_maps/converter/numeric_spec.rb
|
133
158
|
- spec/data_maps/converter/ruby_spec.rb
|
134
159
|
- spec/data_maps/converter/string_spec.rb
|
160
|
+
- spec/data_maps/dsl/mapping/conditions_dsl_spec.rb
|
161
|
+
- spec/data_maps/dsl/mapping/field_dsl_spec.rb
|
162
|
+
- spec/data_maps/dsl/mapping_dsl_spec.rb
|
135
163
|
- spec/data_maps/executable_spec.rb
|
136
164
|
- spec/data_maps/filtered_value_spec.rb
|
137
165
|
- spec/data_maps/mapper_spec.rb
|
@@ -159,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
187
|
requirements:
|
160
188
|
- - ">="
|
161
189
|
- !ruby/object:Gem::Version
|
162
|
-
version:
|
190
|
+
version: 1.9.2
|
163
191
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
192
|
requirements:
|
165
193
|
- - ">="
|
@@ -167,21 +195,25 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
195
|
version: '0'
|
168
196
|
requirements: []
|
169
197
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.4.
|
198
|
+
rubygems_version: 2.4.6
|
171
199
|
signing_key:
|
172
200
|
specification_version: 4
|
173
201
|
summary: Maps data to another structure through a mapping
|
174
202
|
test_files:
|
203
|
+
- spec/data_maps/concerns/configurable_spec.rb
|
175
204
|
- spec/data_maps/concerns/factory_spec.rb
|
176
205
|
- spec/data_maps/condition_spec.rb
|
177
206
|
- spec/data_maps/converter/affixes_spec.rb
|
178
207
|
- spec/data_maps/converter/base_spec.rb
|
179
208
|
- spec/data_maps/converter/bool_spec.rb
|
180
|
-
- spec/data_maps/converter/
|
209
|
+
- spec/data_maps/converter/key_spec.rb
|
181
210
|
- spec/data_maps/converter/map_spec.rb
|
182
211
|
- spec/data_maps/converter/numeric_spec.rb
|
183
212
|
- spec/data_maps/converter/ruby_spec.rb
|
184
213
|
- spec/data_maps/converter/string_spec.rb
|
214
|
+
- spec/data_maps/dsl/mapping/conditions_dsl_spec.rb
|
215
|
+
- spec/data_maps/dsl/mapping/field_dsl_spec.rb
|
216
|
+
- spec/data_maps/dsl/mapping_dsl_spec.rb
|
185
217
|
- spec/data_maps/executable_spec.rb
|
186
218
|
- spec/data_maps/filtered_value_spec.rb
|
187
219
|
- spec/data_maps/mapper_spec.rb
|