rails-importer 0.0.11 → 0.0.12

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: 573081d673ea028ad52dedeb5b6492358a0e7e1e909b1f867e064bb01112ca9c
4
- data.tar.gz: 83d313fdbd28f692b740e84c0ee2563f43f8ae2670489de0190da2db4e19bd5e
3
+ metadata.gz: 21fedbfaf0623b35213ec6e37dafab5d3a11040ffaae8add9613e726743b441c
4
+ data.tar.gz: 3ba9d86e6cdd1b2cd4217c52b9718ebc2e654ac128d1d14551165891c3501282
5
5
  SHA512:
6
- metadata.gz: 3a5f67741c4dd49bde921f88451fac6cc275b2270d7137380915c0c7fcb2ce2b93528b70b5029048a6dffa2b5ba6787b17a7258f8f6f8b22a4fc6cf28f1ac157
7
- data.tar.gz: f6cf858f4b81136dd9af761a00a882548f42d25658143cc522811ecdc1439918c9f47ec0d41adc31a62d887b55538b0c77a9b225e3ac0145aa77dcfd73e1a3b8
6
+ metadata.gz: dddecdf677d03410d18c8df7977be529c5b571ee550263f40aff43544face6b4ef8a9227ae6c5e3f940db62640f49b6f0f6a098707bea9afeb3f7fbd1cb32f76
7
+ data.tar.gz: d20d9a502f0943fe733b655af1c42342565ed04461a3e5c2a4c9d4c86048786e41a16e849aea12c85c6f5a20135afd0efcd4489d320bb771b3e40f9c05d9c5f2
data/README.md CHANGED
@@ -40,6 +40,7 @@ class ExampleImporter < RailsImporter::Base
40
40
  end
41
41
 
42
42
  # importer :simple do
43
+ # csv_params col_sep: ';'
43
44
  # xml_structure :root, :row
44
45
  # fields :name, :email, :age
45
46
  # each_record do |record, params|
@@ -8,6 +8,7 @@ class <%= @importer_name %>Importer < RailsImporter::Base
8
8
  end
9
9
 
10
10
  # importer :simple do
11
+ # csv_params col_sep: ';'
11
12
  # xml_structure :root, :row
12
13
  # fields :name, :email, :age
13
14
  # each_record do |record, params|
@@ -43,7 +43,12 @@ module RailsImporter
43
43
  # end
44
44
 
45
45
  def importer(name = :default, &block)
46
- (self.importers ||= {})[name] ||= {fields: [], xml_structure: [:records, :record], each_record: nil}
46
+ (self.importers ||= {})[name] ||= {
47
+ fields: [],
48
+ csv_params: {headers: false, col_sep: ',', force_quotes: true},
49
+ xml_structure: %i[records record],
50
+ each_record: nil
51
+ }
47
52
  @importer_name = name
48
53
  block.call if block_given?
49
54
  self.importers[name]
@@ -57,6 +62,13 @@ module RailsImporter
57
62
  importer_value(:xml_structure, attributes)
58
63
  end
59
64
 
65
+ def csv_params(*attributes)
66
+ options = importer_value(:csv_params)
67
+ params = attributes.first
68
+ options = options.merge(params) if params.is_a?(Hash)
69
+ importer_value(:csv_params, options)
70
+ end
71
+
60
72
  def each_record(&block)
61
73
  importer_value(:each_record, block)
62
74
  end
@@ -64,12 +76,15 @@ module RailsImporter
64
76
  private
65
77
  def import_from_csv(file, context = :default, custom_fields = :nil)
66
78
  records = []
67
- line = 0
68
- CSV.foreach(file.path, {:headers => false, :col_sep => ';', :force_quotes => true}) do |row|
69
- if line > 0
70
- records << object_values(row, context, custom_fields) unless array_blank?(row)
79
+ first_line = nil
80
+ options = self.importers[context][:csv_params]
81
+ CSV.foreach(file.path, options.merge(headers: false)) do |row|
82
+ # Skip headers
83
+ if first_line.nil?
84
+ first_line = row
85
+ next
71
86
  end
72
- line += 1
87
+ records << object_values(row, context, custom_fields) unless array_blank?(row)
73
88
  end
74
89
  records
75
90
  end
@@ -128,16 +143,10 @@ module RailsImporter
128
143
  array.all?(&:blank?)
129
144
  end
130
145
 
131
- def importer_value(key, attributes)
132
- if attributes.present?
133
- if key == :fields
134
- self.importers[@importer_name][key] = normalize_fields(attributes)
135
- else
136
- self.importers[@importer_name][key] = attributes
137
- end
138
- else
139
- self.importers[@importer_name][key]
140
- end
146
+ def importer_value(key, attributes=nil)
147
+ return self.importers[@importer_name][key] if attributes.nil?
148
+ attributes = normalize_fields(attributes) if key == :fields
149
+ self.importers[@importer_name][key] = attributes
141
150
  end
142
151
 
143
152
  def normalize_fields(attributes)
@@ -1,3 +1,3 @@
1
1
  module RailsImporter
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Porto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-18 00:00:00.000000000 Z
11
+ date: 2021-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails