tallty_import_export 1.0.3 → 1.0.9

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: 4a9e60e31f0ac0c3d979739c50f9631e4a678e61aa86325cd655bd40b45bc7d1
4
- data.tar.gz: a75eb5bb8b865c3d1edd470a5704d1bdc0fda9002411dde9816ea1cd3ab44e98
3
+ metadata.gz: 8caa796932bff10fefba6f63f2d084488b1fcb2dd102f7fb1d3d8c84b4500244
4
+ data.tar.gz: c46cdba654ab877946f7810d6d634c0ba326b882d9b177fba7dd17ed1b4350d9
5
5
  SHA512:
6
- metadata.gz: 1d7adcd134a5ead8f4c0eefa8812f5f5578bf2f01dfd396c02c4ce45544e94f97b5397d8e81da5c320117b7034534c65cc0c54569fc66c884d7e4e03bad5e380
7
- data.tar.gz: 3c36721ef2be7db0df34064fa5b02abd55b3b808644ea8f53f3e982ed90d58a60326ab62ceee365ed351faf0440180bf9d68d637b0a442cc31e4215245dab7ba
6
+ metadata.gz: 5d22ead9b9e98e6d2e23672bddcc2b187bea2287f2702c9b83a8bb3e32dc249c4e11d655b7bd2b2c68954bea7998ec4ff80e555ed93b5de8f7467fded4e2508c
7
+ data.tar.gz: efc7da3bd4d17e2878fb2721948ccfabbbdb16b91981982be07b33f8165aa56e8b66dfb61e3242abb1773507bebd7b18cdfa891b601b55a64c3cc4356f586b5d
@@ -6,14 +6,14 @@ module TalltyImportExport
6
6
  end
7
7
 
8
8
  module ClassMethods
9
- def model_headers
9
+ def model_headers **args
10
10
  columns.map do |column|
11
11
  {
12
12
  key: column.name,
13
13
  name: column.comment || column.name,
14
14
  attr_type: column.type,
15
15
  format: column.type == :string ? :string : nil,
16
- primary_key: column.name == 'id' ? true : false
16
+ primary_key: column.name == 'id' ? true : false
17
17
  }
18
18
  end
19
19
  end
@@ -26,6 +26,7 @@ module TalltyImportExport
26
26
  # chain: 导出时对象属性通过链式调用
27
27
  # index: 数组方式,需要嵌套拿到里面的
28
28
  # merge: true/false,默认false,某一列,如果上下行的内容相同,则直接合并单元格
29
+ # json: model_payload,代表存储在某一个列中
29
30
 
30
31
  def export_xlsx records, **options
31
32
  records = with_scope records
@@ -138,7 +139,7 @@ module TalltyImportExport
138
139
  @headers || export_headers
139
140
  end
140
141
 
141
- def export_headers
142
+ def export_headers **args
142
143
  @headers || klass.try(:headers) || klass.try(:model_headers)
143
144
  end
144
145
 
@@ -1,11 +1,10 @@
1
1
  module TalltyImportExport
2
2
  class Import
3
3
  require 'roo'
4
- attr_reader :klass, :context, :headers, :primary_keys, :associations
4
+ attr_reader :klass, :context, :primary_keys, :associations
5
5
 
6
6
  def initialize klass
7
7
  @klass = klass
8
- @headers = import_headers_result.map { |header| header.with_indifferent_access }
9
8
  @context = Context.new({})
10
9
  end
11
10
 
@@ -15,6 +14,7 @@ module TalltyImportExport
15
14
  # format: excel是否需要特定的格式,目前主要是类似于身份证号,可以用string
16
15
  # convert: 导入时候,把excel的内容转换成导入时候代码逻辑需要的内容
17
16
  # primary_key: 是否是主键
17
+ # json: model_payload,代表存储在某一个列中
18
18
 
19
19
  # xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
20
20
  def import_xlsx xlsx_file, associations, **options
@@ -51,10 +51,10 @@ module TalltyImportExport
51
51
 
52
52
  def process_options options
53
53
  options = options.with_indifferent_access
54
- self.headers = options.delete(:headers) || []
55
- @primary_keys = options.delete(:primary_keys) || headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact
54
+ @headers = options.delete(:headers)
55
+ @primary_keys = options.delete(:primary_keys) || @headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact
56
56
 
57
- @excel_hash = headers.reduce({}) do |h, header|
57
+ @excel_hash = @headers.reduce({}) do |h, header|
58
58
  h[header[:key].to_sym] = header[:name]
59
59
  h
60
60
  end
@@ -75,7 +75,7 @@ module TalltyImportExport
75
75
 
76
76
  def convert_data line_info
77
77
  line_info.with_indifferent_access.reduce({}) do |h, (k, v)|
78
- header = headers.find do |_header|
78
+ header = @headers.find do |_header|
79
79
  _header[:key].to_sym == k.to_sym
80
80
  end
81
81
  # header[:convert] = handle_xxx
@@ -100,33 +100,33 @@ module TalltyImportExport
100
100
  @headers || import_headers
101
101
  end
102
102
 
103
- def import_headers
104
- @headers || klass.try(:import_headers) || klass.try(:model_headers) || (raise ArgumentError.new('missing import_headers'))
103
+ def import_headers **args
104
+ @headers || klass.try(:headers) || klass.try(:model_headers) || (raise ArgumentError.new('missing import_headers'))
105
105
  end
106
106
 
107
- # 只保留 key, name, json, 合并到 import_header
108
- def headers= val
109
- if val.empty?
110
- @headers = import_headers_result.map { |header| header.with_indifferent_access }
111
- return
112
- end
113
-
114
- key_to_coming_header = val.reduce({}) do |out, header|
115
- out[header.with_indifferent_access[:key].to_sym] = header.with_indifferent_access
116
- out
117
- end
118
-
119
- result = []
120
- val.map do |header|
121
- if (exist_header = import_headers_result.find { |model_header| model_header[:key] === header[:key] })
122
- result.push(exist_header.merge(name: header[:name], json: header[:json]))
123
- else
124
- result.push({ key: header[:key], name: header[:name], json: header[:json]})
125
- end
126
- end
127
-
128
- @headers = result
129
- end
107
+ # # 只保留 key, name, json, 合并到 import_header
108
+ # def headers= val
109
+ # if val.empty?
110
+ # @headers = import_headers_result.map { |header| header.with_indifferent_access }
111
+ # return
112
+ # end
113
+ #
114
+ # key_to_coming_header = val.reduce({}) do |out, header|
115
+ # out[header.with_indifferent_access[:key].to_sym] = header.with_indifferent_access
116
+ # out
117
+ # end
118
+ #
119
+ # result = []
120
+ # val.map do |header|
121
+ # if (exist_header = import_headers_result.find { |model_header| model_header[:key] === header[:key] })
122
+ # result.push(exist_header.merge(header.compact))
123
+ # else
124
+ # result.push(header)
125
+ # end
126
+ # end
127
+ #
128
+ # @headers = result
129
+ # end
130
130
 
131
131
  def skip val, processing_line_info, raw_line_info
132
132
  # do nothing there, use for header[:convert]
@@ -1,3 +1,3 @@
1
1
  module TalltyImportExport
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.9"
3
3
  end
Binary file
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tallty_import_export
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - liyijie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-23 00:00:00.000000000 Z
11
+ date: 2021-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zip-zip
@@ -149,6 +149,8 @@ files:
149
149
  - lib/tallty_import_export/import.rb
150
150
  - lib/tallty_import_export/importable.rb
151
151
  - lib/tallty_import_export/version.rb
152
+ - tallty_import_export-1.0.3.gem
153
+ - tallty_import_export-1.0.5.gem
152
154
  - tallty_import_export.gemspec
153
155
  homepage: https://git.tallty.com/open-source/tallty_import_export
154
156
  licenses: