declarative_mapper 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1e992677c36a3cc5ad927ce275958aa86e9ddca151c86c09312f47772bd7961e
4
+ data.tar.gz: f0a9672aca7fe17dac81e7e7c7ed6a627f10a6c6c10e47d8edbfe8d9799f0768
5
+ SHA512:
6
+ metadata.gz: 858e926c4ded4ee3d745734be84e3e0bd8ac0408b9e4a1dd15a220cbdc1a6f608b6133c18e31ed1a287f686c15337c1b868f631d376181e35a2783ac6ea38007
7
+ data.tar.gz: 64396db43247eaa162777abf229a1a1b36dce461bddc888e57594c6afe0659c65a4c026a7c8356063740921fa752cfaccd4915e09d83a55b047f053a7e1d9b61
@@ -0,0 +1,64 @@
1
+ require 'yaml'
2
+ require 'csv'
3
+
4
+ require 'active_support/core_ext/hash/keys.rb'
5
+ require 'active_support/core_ext/string'
6
+
7
+ require_relative 'module_helper'
8
+
9
+ class CSVParser
10
+ attr_reader :yml_path, :client_name, :table_name
11
+
12
+ def initialize(yml_path, client_name)
13
+ @yml_path = yml_path
14
+ @client_name = client_name
15
+ @table_name = File.basename(yml_path, '.yml')
16
+ end
17
+
18
+ def yml
19
+ @yml ||= YAML.load_file(yml_path).deep_symbolize_keys
20
+ end
21
+
22
+ def parse(csv_row)
23
+ result = {}
24
+
25
+ field_names.each do |field_name, csv_field_name|
26
+ result[field_name] =
27
+ if method_needed?(csv_field_name)
28
+ parsed_signature =
29
+ ModuleHelper.parse_signature(csv_field_name, client_name, table_name, field_name)
30
+
31
+ args = argument_values(parsed_signature[:arguments], csv_row)
32
+
33
+ call_shared_or_client_method(
34
+ parsed_signature[:path],
35
+ parsed_signature[:method_name],
36
+ *args
37
+ )
38
+ else
39
+ csv_row[csv_field_name]
40
+ end
41
+ end
42
+
43
+ result.symbolize_keys
44
+ end
45
+
46
+ private
47
+
48
+ def field_names
49
+ yml[:mapping]
50
+ end
51
+
52
+ def method_needed?(csv_field_name)
53
+ # "(status)" will match, "account_name" won't
54
+ !!(csv_field_name =~ /\(.*\)/)
55
+ end
56
+
57
+ def call_shared_or_client_method(path, method_name, *args)
58
+ path.map(&:camelize).join('::').constantize.send(method_name, *args)
59
+ end
60
+
61
+ def argument_values(argument_names, csv_row)
62
+ argument_names.map { |name| csv_row[name] }
63
+ end
64
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'csv_parser'
2
+
3
+ class DeclarativeMapper
4
+ attr_reader :client_path, :client_name, :project_path
5
+
6
+ def initialize(client_path)
7
+ @client_path = client_path
8
+ @client_name = client_path.split('/').last
9
+ @project_path = client_path.sub("/#{@client_name}", '')
10
+
11
+ require_client_mapper_methods
12
+ require_shared_mapper_methods
13
+ end
14
+
15
+ def convert(csv_row, yml_file_name)
16
+ parser = CSVParser.new(yml_path(yml_file_name), client_name)
17
+
18
+ parser.parse(csv_row)
19
+ end
20
+
21
+ private
22
+
23
+ def yml_path(yml_file_name)
24
+ "#{client_path}/#{yml_file_name}.yml"
25
+ end
26
+
27
+ def client_mapper_methods_dir_path
28
+ "#{client_path}/mapper_methods"
29
+ end
30
+
31
+ def shared_mapper_methods_dir_path
32
+ "#{project_path}/shared"
33
+ end
34
+
35
+ def require_client_mapper_methods
36
+ Dir["#{client_mapper_methods_dir_path}/*.rb"].each { |file| require file }
37
+ end
38
+
39
+ def require_shared_mapper_methods
40
+ Dir["#{shared_mapper_methods_dir_path}/**/*.rb"].each { |file| require file }
41
+ end
42
+ end
@@ -0,0 +1,31 @@
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].delete(' ').split(',')}
12
+
13
+ if shared_method?(signature)
14
+ path = 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
+ private
27
+
28
+ def self.shared_method?(signature)
29
+ !!(signature =~ /^shared\//)
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: declarative_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Nemytchenko
8
+ - Vadim Venediktov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-12-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: install.vv@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/csv_parser.rb
21
+ - lib/declarative_mapper.rb
22
+ - lib/module_helper.rb
23
+ homepage: https://gitlab.com/installero/sync_prototype
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.0.6
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Creates an object from a csv row according to yml-file with mapping rules
46
+ test_files: []