declarative_mapper 0.0.1 → 0.0.2
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/bin/declarative_mapper_generate +71 -0
- data/lib/csv_parser.rb +1 -7
- data/lib/module_helper.rb +4 -2
- data/templates/mapper_methods.rb.erb +12 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97aeaf7fdf114cc21c687b1e247e55fc95050a1fd71478ac4c43a59e9a6aa9dc
|
4
|
+
data.tar.gz: d022f96f8973ac0c86765510675d1e70a2871b1427d7e2b5b9defac9aa38249a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e8f1e3ae86f2d0dccc8c9605f670864b388609b5b1f6571c85dfcb00ee10cfc999a97e80f6d99c7242d66e303e4a3e787c638df1f6858fdc325218c7b90c211
|
7
|
+
data.tar.gz: 6fc7e46bdf8147ab1f899e3a49196999f8137ee1520bb7ad1c10788718f04e7608ee057a2e1030740730b74cd274aa7e69644175ac539a9c67a9037a1294a700
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'active_support/core_ext/string'
|
4
|
+
require 'active_support/core_ext/hash/keys.rb'
|
5
|
+
|
6
|
+
require 'yaml'
|
7
|
+
require 'erb'
|
8
|
+
require 'fileutils'
|
9
|
+
|
10
|
+
require_relative '../lib/module_helper'
|
11
|
+
|
12
|
+
def normalize_args(string)
|
13
|
+
return if string.nil?
|
14
|
+
|
15
|
+
string.delete('() ').split(',').join(', ')
|
16
|
+
end
|
17
|
+
|
18
|
+
def customer_methods_only(hash)
|
19
|
+
hash.reject do |_, value|
|
20
|
+
!ModuleHelper.needs_method?(value) || ModuleHelper.shared_method?(value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
unless ARGV[0]
|
25
|
+
puts "Yaml file name needed, example:"
|
26
|
+
puts
|
27
|
+
puts "ruby generate.rb akme/customers.yml"
|
28
|
+
puts
|
29
|
+
exit 1
|
30
|
+
end
|
31
|
+
|
32
|
+
yml_path = ARGV[0]
|
33
|
+
|
34
|
+
# Getting client & table names from yml file path
|
35
|
+
client_dir, yml_filename = File.split(yml_path)
|
36
|
+
|
37
|
+
table_name = File.basename(yml_filename, '.yml')
|
38
|
+
client_name = client_dir.split(File::SEPARATOR).last
|
39
|
+
|
40
|
+
# Getting list of methods (with arrays of args) from yml file
|
41
|
+
client_mapping = YAML.load_file(yml_path).deep_symbolize_keys[:mapping]
|
42
|
+
|
43
|
+
methods =
|
44
|
+
customer_methods_only(client_mapping)
|
45
|
+
.map { |key, value| [key, normalize_args(value)] }
|
46
|
+
.to_h
|
47
|
+
|
48
|
+
puts "Methods #{methods.keys.join(', ')} found"
|
49
|
+
|
50
|
+
# Getting mapper methods file template
|
51
|
+
template_path = "#{__dir__}/../templates/mapper_methods.rb.erb"
|
52
|
+
template = ERB.new(File.read(template_path), nil, '-')
|
53
|
+
|
54
|
+
# Constructing path for the methods file
|
55
|
+
methods_file_dir = File.join(client_dir, ModuleHelper::CLIENT_METHODS_FOLDER)
|
56
|
+
methods_file_name = "#{table_name}.rb"
|
57
|
+
methods_file_path = File.join(methods_file_dir, methods_file_name)
|
58
|
+
|
59
|
+
# Creating directory
|
60
|
+
FileUtils.mkdir_p(methods_file_dir) unless File.directory?(methods_file_dir)
|
61
|
+
|
62
|
+
# Writing to file
|
63
|
+
if File.exist?(methods_file_path)
|
64
|
+
puts "File #{methods_file_name} already exists!"
|
65
|
+
puts
|
66
|
+
else
|
67
|
+
File.write(methods_file_path, template.result(binding))
|
68
|
+
|
69
|
+
puts "Wrote to #{methods_file_name}!"
|
70
|
+
puts
|
71
|
+
end
|
data/lib/csv_parser.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'yaml'
|
2
|
-
require 'csv'
|
3
2
|
|
4
3
|
require 'active_support/core_ext/hash/keys.rb'
|
5
4
|
require 'active_support/core_ext/string'
|
@@ -24,7 +23,7 @@ class CSVParser
|
|
24
23
|
|
25
24
|
field_names.each do |field_name, csv_field_name|
|
26
25
|
result[field_name] =
|
27
|
-
if
|
26
|
+
if ModuleHelper.needs_method?(csv_field_name)
|
28
27
|
parsed_signature =
|
29
28
|
ModuleHelper.parse_signature(csv_field_name, client_name, table_name, field_name)
|
30
29
|
|
@@ -49,11 +48,6 @@ class CSVParser
|
|
49
48
|
yml[:mapping]
|
50
49
|
end
|
51
50
|
|
52
|
-
def method_needed?(csv_field_name)
|
53
|
-
# "(status)" will match, "account_name" won't
|
54
|
-
!!(csv_field_name =~ /\(.*\)/)
|
55
|
-
end
|
56
|
-
|
57
51
|
def call_shared_or_client_method(path, method_name, *args)
|
58
52
|
path.map(&:camelize).join('::').constantize.send(method_name, *args)
|
59
53
|
end
|
data/lib/module_helper.rb
CHANGED
@@ -0,0 +1,12 @@
|
|
1
|
+
module <%= client_name.camelize %>
|
2
|
+
module MapperMethods
|
3
|
+
module <%= table_name.camelize %>
|
4
|
+
<% methods.each_with_index do |(method_name, arg_names), index| -%>
|
5
|
+
def self.<%= method_name %>(<%= arg_names %>)
|
6
|
+
# Implement method <%= method_name %> here
|
7
|
+
end
|
8
|
+
<%= "\n" unless index == methods.count - 1 -%>
|
9
|
+
<% end -%>
|
10
|
+
end
|
11
|
+
end
|
12
|
+
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Nemytchenko
|
@@ -27,13 +27,16 @@ dependencies:
|
|
27
27
|
version: '0'
|
28
28
|
description:
|
29
29
|
email: install.vv@gmail.com
|
30
|
-
executables:
|
30
|
+
executables:
|
31
|
+
- declarative_mapper_generate
|
31
32
|
extensions: []
|
32
33
|
extra_rdoc_files: []
|
33
34
|
files:
|
35
|
+
- bin/declarative_mapper_generate
|
34
36
|
- lib/csv_parser.rb
|
35
37
|
- lib/declarative_mapper.rb
|
36
38
|
- lib/module_helper.rb
|
39
|
+
- templates/mapper_methods.rb.erb
|
37
40
|
homepage: https://gitlab.com/installero/sync_prototype
|
38
41
|
licenses:
|
39
42
|
- MIT
|