declarative_mapper 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/declarative_mapper.rb +22 -1
- metadata +3 -6
- data/bin/declarative_mapper_generate +0 -71
- data/templates/mapper_methods.rb.erb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a03b364c19cadd10122eaf87703d092201f72489ae236b741645dc206556c11
|
4
|
+
data.tar.gz: a31d1e9a6aa4e52a18f01c9bc909d5677df289f9b05535eeed92081aeba431ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b90bf14bc370681bc05e993d61d35ad9a59fd41cb02b57a89767ac634a3f7ef201282c8c2cda92994f58180ba864128e9b4f561dbc16256d0bec59a1e39eed68
|
7
|
+
data.tar.gz: 4963de915d222f2838cb36d85bba185d2b707e23776f15b910282f53a5a946d32fe0b8fedf7d46233fd5dc2da360f00228ff844e11591ea44288171263caf0be
|
data/lib/declarative_mapper.rb
CHANGED
@@ -24,6 +24,16 @@ class DeclarativeMapper
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
def self.required_csv_fields(mapping_hash)
|
28
|
+
deep_inject(mapping_hash, []) do |value, sum|
|
29
|
+
if needs_method?(value)
|
30
|
+
sum += (parse_signature(value)[:arguments])
|
31
|
+
else
|
32
|
+
sum.push(value)
|
33
|
+
end
|
34
|
+
end.uniq.compact
|
35
|
+
end
|
36
|
+
|
27
37
|
def self.argument_values(argument_names, csv_row)
|
28
38
|
argument_names.map { |name| csv_row[name] }
|
29
39
|
end
|
@@ -66,7 +76,18 @@ class DeclarativeMapper
|
|
66
76
|
when Array
|
67
77
|
object.map { |e| deep_transform_values_with_path(e, path, &block) }
|
68
78
|
else
|
69
|
-
yield(object, path)
|
79
|
+
yield(object, path)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.deep_inject(object, acc, &block)
|
84
|
+
case object
|
85
|
+
when Hash
|
86
|
+
object.inject(acc) { |a, (k, v)| deep_inject(v, a, &block) }
|
87
|
+
when Array
|
88
|
+
object.inject(acc) { |a, e| deep_inject(e, a, &block) }
|
89
|
+
else
|
90
|
+
yield(object, acc)
|
70
91
|
end
|
71
92
|
end
|
72
93
|
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Nemytchenko
|
@@ -27,14 +27,11 @@ dependencies:
|
|
27
27
|
version: '0'
|
28
28
|
description:
|
29
29
|
email: install.vv@gmail.com
|
30
|
-
executables:
|
31
|
-
- declarative_mapper_generate
|
30
|
+
executables: []
|
32
31
|
extensions: []
|
33
32
|
extra_rdoc_files: []
|
34
33
|
files:
|
35
|
-
- bin/declarative_mapper_generate
|
36
34
|
- lib/declarative_mapper.rb
|
37
|
-
- templates/mapper_methods.rb.erb
|
38
35
|
homepage: https://gitlab.com/installero/sync_prototype
|
39
36
|
licenses:
|
40
37
|
- MIT
|
@@ -54,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
51
|
- !ruby/object:Gem::Version
|
55
52
|
version: '0'
|
56
53
|
requirements: []
|
57
|
-
rubygems_version: 3.0.
|
54
|
+
rubygems_version: 3.0.3
|
58
55
|
signing_key:
|
59
56
|
specification_version: 4
|
60
57
|
summary: Creates an object from a csv row according to yml-file with mapping rules
|
@@ -1,71 +0,0 @@
|
|
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
|
@@ -1,12 +0,0 @@
|
|
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
|