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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21589392eef4d3e07b5fd79d24e49c8ba38cd35311de8116eed936b5d60a3dd4
4
- data.tar.gz: db82e07c791b0e887bd054ac9cebb1bf0d318e0237e1b46a3743dbae4de88627
3
+ metadata.gz: 97aeaf7fdf114cc21c687b1e247e55fc95050a1fd71478ac4c43a59e9a6aa9dc
4
+ data.tar.gz: d022f96f8973ac0c86765510675d1e70a2871b1427d7e2b5b9defac9aa38249a
5
5
  SHA512:
6
- metadata.gz: 443817f2894d438195a54513ee91f3afc565d29e244415eaf623578f4527a085a2554dc794afbcc011c8ced775bd26acde6a34850afc5bf45f1b747d89663d8f
7
- data.tar.gz: e82708b56a793f586825661131edca6092b1a63658d9b413fafa727cf3631ec486f6922f57b039f35225c292a21a375bc27f1172d8042e5b2af91f78a238aebc
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 method_needed?(csv_field_name)
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
@@ -23,9 +23,11 @@ module ModuleHelper
23
23
  result
24
24
  end
25
25
 
26
- private
27
-
28
26
  def self.shared_method?(signature)
29
27
  !!(signature =~ /^shared\//)
30
28
  end
29
+
30
+ def self.needs_method?(signature)
31
+ !!(signature =~ /\(.*\)/)
32
+ end
31
33
  end
@@ -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.1
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