declarative_mapper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97aeaf7fdf114cc21c687b1e247e55fc95050a1fd71478ac4c43a59e9a6aa9dc
4
- data.tar.gz: d022f96f8973ac0c86765510675d1e70a2871b1427d7e2b5b9defac9aa38249a
3
+ metadata.gz: 10539dff30e5685fc48c7e114d864b099287be1887103b1f973d406c0bbdb728
4
+ data.tar.gz: dd743aabb3b023f133de96501dc3c22bfbd5961356366ee91dc2dbf3938e73c0
5
5
  SHA512:
6
- metadata.gz: 8e8f1e3ae86f2d0dccc8c9605f670864b388609b5b1f6571c85dfcb00ee10cfc999a97e80f6d99c7242d66e303e4a3e787c638df1f6858fdc325218c7b90c211
7
- data.tar.gz: 6fc7e46bdf8147ab1f899e3a49196999f8137ee1520bb7ad1c10788718f04e7608ee057a2e1030740730b74cd274aa7e69644175ac539a9c67a9037a1294a700
6
+ metadata.gz: 996424166a6534124e4a6ef57f3f14499f278a2c3283d55bac6fe8f20b18df9df5349c5a2ff6d9012b3865fc5d0e591388a680a043b89eb09e2514ff70fe33aa
7
+ data.tar.gz: b38785bfddf2bcd24209834a7e6c7987fae3d57e5ae1901a898c8246c24958b676119835d98e7ef1a67cdd518c7a18597eb3e6079af88d82eda832a40961ef28
data/lib/csv_parser.rb CHANGED
@@ -19,37 +19,34 @@ class CSVParser
19
19
  end
20
20
 
21
21
  def parse(csv_row)
22
- result = {}
23
-
24
- field_names.each do |field_name, csv_field_name|
25
- result[field_name] =
26
- if ModuleHelper.needs_method?(csv_field_name)
27
- parsed_signature =
28
- ModuleHelper.parse_signature(csv_field_name, client_name, table_name, field_name)
29
-
30
- args = argument_values(parsed_signature[:arguments], csv_row)
31
-
32
- call_shared_or_client_method(
33
- parsed_signature[:path],
34
- parsed_signature[:method_name],
35
- *args
36
- )
37
- else
38
- csv_row[csv_field_name]
39
- end
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
+ call_shared_or_client_method(
32
+ parsed_signature[:path] + path[0..-2],
33
+ parsed_signature[:method_name],
34
+ *args
35
+ )
36
+ else
37
+ csv_row[csv_field_name]
38
+ end
40
39
  end
41
-
42
- result.symbolize_keys
43
40
  end
44
41
 
45
42
  private
46
43
 
47
- def field_names
44
+ def mapping_hash
48
45
  yml[:mapping]
49
46
  end
50
47
 
51
48
  def call_shared_or_client_method(path, method_name, *args)
52
- path.map(&:camelize).join('::').constantize.send(method_name, *args)
49
+ path.map(&:to_s).map(&:camelize).join('::').constantize.send(method_name, *args)
53
50
  end
54
51
 
55
52
  def argument_values(argument_names, csv_row)
@@ -33,7 +33,7 @@ class DeclarativeMapper
33
33
  end
34
34
 
35
35
  def require_client_mapper_methods
36
- Dir["#{client_mapper_methods_dir_path}/*.rb"].each { |file| require file }
36
+ Dir["#{client_mapper_methods_dir_path}/**/*.rb"].each { |file| require file }
37
37
  end
38
38
 
39
39
  def require_shared_mapper_methods
data/lib/module_helper.rb CHANGED
@@ -30,4 +30,23 @@ module ModuleHelper
30
30
  def self.needs_method?(signature)
31
31
  !!(signature =~ /\(.*\)/)
32
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
+ return yield(object, path) unless object.is_a?(Hash)
47
+
48
+ object.map do |key, value|
49
+ [key, deep_transform_values_with_path(value, path + [key], &block)]
50
+ end.to_h
51
+ end
33
52
  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.2
4
+ version: 0.0.3
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-11 00:00:00.000000000 Z
12
+ date: 2019-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport