declarative_mapper 0.0.8 → 0.0.9
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/lib/declarative_mapper.rb +54 -24
- metadata +2 -4
- data/lib/csv_parser.rb +0 -58
- data/lib/module_helper.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ff58bf672273051481f11c20268a5035dc684faaf9d743935d76dd8517660ea
|
4
|
+
data.tar.gz: 507afff4fd2335fdcd7b2747e9ab4adf60cf4b1ef326703b7459593c69499d7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9917fb15e228b41435879958bacf27dee3c569028ddf3f1a3f5dfdfa5c1e93a8779fb9a255adc8b67818a938314c26221402157bf25c5573581ca479ce8b159
|
7
|
+
data.tar.gz: 8eb67c05efbe6d2a243b88b16d4dda7ddfcf7d2f926f9abd927628fd0ef870a35732328c95e20a34640f127f513604877d4ee7ddab935cb6faf6621160334302
|
data/lib/declarative_mapper.rb
CHANGED
@@ -1,42 +1,72 @@
|
|
1
|
-
|
1
|
+
require 'active_support/core_ext/hash/keys.rb'
|
2
|
+
require 'active_support/core_ext/string'
|
2
3
|
|
3
4
|
class DeclarativeMapper
|
4
|
-
|
5
|
+
def self.convert(mapper_methods, mapping_hash, csv_row)
|
6
|
+
deep_transform_values_with_path(mapping_hash) do |csv_field_name, path|
|
7
|
+
field_name = path.last
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@project_path = client_path.sub("/#{@client_name}", '')
|
9
|
+
if needs_method?(csv_field_name)
|
10
|
+
parsed_signature =
|
11
|
+
parse_signature(csv_field_name, field_name)
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
end
|
13
|
+
args = argument_values(parsed_signature[:arguments], csv_row)
|
14
|
+
method_name = parsed_signature[:method_name]
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
unless shared_method?(csv_field_name)
|
17
|
+
method_name = path[0..-2].push(method_name).join('_')
|
18
|
+
end
|
17
19
|
|
18
|
-
|
20
|
+
mapper_methods.send(method_name, *args)
|
21
|
+
else
|
22
|
+
csv_row[csv_field_name]
|
23
|
+
end
|
24
|
+
end
|
19
25
|
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
def yml_path(yml_file_name)
|
24
|
-
"#{client_path}/#{yml_file_name}.yml"
|
27
|
+
def self.argument_values(argument_names, csv_row)
|
28
|
+
argument_names.map { |name| csv_row[name] }
|
25
29
|
end
|
26
30
|
|
27
|
-
def
|
28
|
-
|
31
|
+
def self.parse_signature(signature, field_name=nil)
|
32
|
+
match = signature.match(/^(.*)\((.*)\)/)
|
33
|
+
|
34
|
+
method_name = match[1]
|
35
|
+
method_name = field_name if method_name.empty?
|
36
|
+
|
37
|
+
{
|
38
|
+
arguments: match[2].gsub(/,\s+/, ',').split(','),
|
39
|
+
method_name: method_name
|
40
|
+
}
|
29
41
|
end
|
30
42
|
|
31
|
-
def
|
32
|
-
|
43
|
+
def self.shared_method?(signature)
|
44
|
+
!!(signature =~ /^.+\(.*\)/)
|
33
45
|
end
|
34
46
|
|
35
|
-
def
|
36
|
-
|
47
|
+
def self.needs_method?(signature)
|
48
|
+
!!(signature =~ /\(.*\)/)
|
37
49
|
end
|
38
50
|
|
39
|
-
|
40
|
-
|
51
|
+
# Example of usage:
|
52
|
+
#
|
53
|
+
# my_hash = {a: 0, b: {c: 1, d: 2, e: {f: 3}}}
|
54
|
+
# values = %w(mother washed the ceiling)
|
55
|
+
#
|
56
|
+
# result = deep_transform_values_with_path(my_hash) do |value, path|
|
57
|
+
# path.join('/') + '/' + values[value]
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# {:a=>"a/mother", :b=>{:c=>"b/c/washed", :d=>"b/d/the", :e=>{:f=>"b/e/f/ceiling"}}}
|
61
|
+
#
|
62
|
+
def self.deep_transform_values_with_path(object, path=[], &block)
|
63
|
+
case object
|
64
|
+
when Hash
|
65
|
+
object.map { |k, v| [k, deep_transform_values_with_path(v, path + [k], &block)] }.to_h
|
66
|
+
when Array
|
67
|
+
object.map { |e| deep_transform_values_with_path(e, path, &block) }
|
68
|
+
else
|
69
|
+
yield(object, path) unless object.is_a?(Hash)
|
70
|
+
end
|
41
71
|
end
|
42
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: declarative_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Nemytchenko
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-12-
|
12
|
+
date: 2019-12-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -33,9 +33,7 @@ extensions: []
|
|
33
33
|
extra_rdoc_files: []
|
34
34
|
files:
|
35
35
|
- bin/declarative_mapper_generate
|
36
|
-
- lib/csv_parser.rb
|
37
36
|
- lib/declarative_mapper.rb
|
38
|
-
- lib/module_helper.rb
|
39
37
|
- templates/mapper_methods.rb.erb
|
40
38
|
homepage: https://gitlab.com/installero/sync_prototype
|
41
39
|
licenses:
|
data/lib/csv_parser.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
require 'active_support/core_ext/hash/keys.rb'
|
4
|
-
require 'active_support/core_ext/string'
|
5
|
-
|
6
|
-
require_relative 'module_helper'
|
7
|
-
|
8
|
-
class CSVParser
|
9
|
-
attr_reader :yml_path, :client_name, :table_name
|
10
|
-
|
11
|
-
def initialize(yml_path, client_name)
|
12
|
-
@yml_path = yml_path
|
13
|
-
@client_name = client_name
|
14
|
-
@table_name = File.basename(yml_path, '.yml')
|
15
|
-
end
|
16
|
-
|
17
|
-
def yml
|
18
|
-
@yml ||= YAML.load_file(yml_path).deep_symbolize_keys
|
19
|
-
end
|
20
|
-
|
21
|
-
def parse(csv_row)
|
22
|
-
ModuleHelper.deep_transform_values_with_path(mapping_hash) do |csv_field_name, path|
|
23
|
-
field_name = path.last
|
24
|
-
|
25
|
-
if ModuleHelper.needs_method?(csv_field_name)
|
26
|
-
parsed_signature =
|
27
|
-
ModuleHelper.parse_signature(csv_field_name, client_name, table_name, field_name)
|
28
|
-
|
29
|
-
args = argument_values(parsed_signature[:arguments], csv_row)
|
30
|
-
|
31
|
-
module_path = parsed_signature[:path]
|
32
|
-
module_path += path[0..-2] unless ModuleHelper.shared_method?(csv_field_name)
|
33
|
-
|
34
|
-
call_shared_or_client_method(
|
35
|
-
module_path,
|
36
|
-
parsed_signature[:method_name],
|
37
|
-
*args
|
38
|
-
)
|
39
|
-
else
|
40
|
-
csv_row[csv_field_name]
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def mapping_hash
|
48
|
-
yml[:mapping]
|
49
|
-
end
|
50
|
-
|
51
|
-
def call_shared_or_client_method(path, method_name, *args)
|
52
|
-
path.map(&:to_s).map(&:camelize).join('::').constantize.send(method_name, *args)
|
53
|
-
end
|
54
|
-
|
55
|
-
def argument_values(argument_names, csv_row)
|
56
|
-
argument_names.map { |name| csv_row[name] }
|
57
|
-
end
|
58
|
-
end
|
data/lib/module_helper.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
module ModuleHelper
|
2
|
-
CLIENT_METHODS_FOLDER = 'mapper_methods'
|
3
|
-
# 'shared/phone/number/normalize(phonenumber)'
|
4
|
-
# -> ['shared', 'phone', 'number'], 'normalize', ['phonenumber']
|
5
|
-
#
|
6
|
-
# '(status)', client_name: 'reliable', field_name: 'active'
|
7
|
-
# -> ['reliable', 'mapper_methods'], 'active', ['status']
|
8
|
-
def self.parse_signature(signature, client_name=nil, table_name=nil, field_name=nil)
|
9
|
-
match = signature.match(/^(.*)\((.*)\)/)
|
10
|
-
|
11
|
-
result = {arguments: match[2].gsub(/,\s+/, ',').split(',')}
|
12
|
-
|
13
|
-
if shared_method?(signature)
|
14
|
-
path = ['shared'] + match[1].split('/')
|
15
|
-
|
16
|
-
result[:method_name] = path.pop.to_sym
|
17
|
-
result[:path] = path
|
18
|
-
else
|
19
|
-
result[:method_name] = field_name
|
20
|
-
result[:path] = [client_name, CLIENT_METHODS_FOLDER, table_name]
|
21
|
-
end
|
22
|
-
|
23
|
-
result
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.shared_method?(signature)
|
27
|
-
!!(signature =~ /^.+\(.*\)/)
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.needs_method?(signature)
|
31
|
-
!!(signature =~ /\(.*\)/)
|
32
|
-
end
|
33
|
-
|
34
|
-
# Example of usage:
|
35
|
-
#
|
36
|
-
# my_hash = {a: 0, b: {c: 1, d: 2, e: {f: 3}}}
|
37
|
-
# values = %w(mother washed the ceiling)
|
38
|
-
#
|
39
|
-
# result = deep_transform_values_with_path(my_hash) do |value, path|
|
40
|
-
# path.join('/') + '/' + values[value]
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# {:a=>"a/mother", :b=>{:c=>"b/c/washed", :d=>"b/d/the", :e=>{:f=>"b/e/f/ceiling"}}}
|
44
|
-
#
|
45
|
-
def self.deep_transform_values_with_path(object, path=[], &block)
|
46
|
-
case object
|
47
|
-
when Hash
|
48
|
-
object.map { |k, v| [k, deep_transform_values_with_path(v, path + [k], &block)] }.to_h
|
49
|
-
when Array
|
50
|
-
object.map { |e| deep_transform_values_with_path(e, path, &block) }
|
51
|
-
else
|
52
|
-
yield(object, path) unless object.is_a?(Hash)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|