rails-importer 0.0.8 → 0.0.9
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 +4 -4
- data/lib/rails_importer/base.rb +22 -22
- data/lib/rails_importer/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3635e5ee04bb01d660a84c489f677c293fd9a2e
|
4
|
+
data.tar.gz: 2dac3d5fd9b21739a18bb9dd89b600709792a631
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa1341f372d03468fa320b8b77a919472abffc0ac68593d927f2270b90e1f899432ed3e0b107b517a66bfc70b8387da7f5c6e46e9066e2574c3d8acf85a97f74
|
7
|
+
data.tar.gz: e9133c2a604f506e05b59864db75c0e5d6cd9bdbd675dae991d6ef0e38316f72772505f4ba2c97610baf20986f6bea9811b80aca7a334c7593d5a952f5d3cdb6
|
data/lib/rails_importer/base.rb
CHANGED
@@ -37,7 +37,7 @@ module RailsImporter
|
|
37
37
|
# end
|
38
38
|
# end
|
39
39
|
|
40
|
-
def importer(name
|
40
|
+
def importer(name = :default, &block)
|
41
41
|
(self.importers ||= {})[name] ||= {fields: [], xml_structure: [:records, :record], each_record: nil}
|
42
42
|
@importer_name = name
|
43
43
|
block.call if block_given?
|
@@ -57,19 +57,19 @@ module RailsImporter
|
|
57
57
|
end
|
58
58
|
|
59
59
|
private
|
60
|
-
def import_from_csv(file, context
|
60
|
+
def import_from_csv(file, context = :default)
|
61
61
|
records = []
|
62
62
|
line = 0
|
63
63
|
CSV.foreach(file.path, {:headers => false, :col_sep => ';', :force_quotes => true}) do |row|
|
64
|
-
if line>0
|
64
|
+
if line > 0
|
65
65
|
records << object_values(row, context) unless array_blank?(row)
|
66
66
|
end
|
67
|
-
line+=1
|
67
|
+
line += 1
|
68
68
|
end
|
69
69
|
records
|
70
70
|
end
|
71
71
|
|
72
|
-
def import_from_xml(file, context
|
72
|
+
def import_from_xml(file, context = :default)
|
73
73
|
records = []
|
74
74
|
xml_structure = self.importers[context][:xml_structure]
|
75
75
|
xml = Hash.from_xml(file.read)
|
@@ -82,50 +82,50 @@ module RailsImporter
|
|
82
82
|
records
|
83
83
|
end
|
84
84
|
|
85
|
-
def import_from_xls(file, context
|
85
|
+
def import_from_xls(file, context = :default)
|
86
86
|
records = []
|
87
87
|
Spreadsheet.client_encoding = 'UTF-8'
|
88
88
|
document = Spreadsheet.open(file.path)
|
89
89
|
spreadsheet = document.worksheet 0
|
90
90
|
spreadsheet.each_with_index do |row, i|
|
91
|
-
next
|
91
|
+
next if i.zero?
|
92
92
|
records << object_values(row, context) unless array_blank?(row)
|
93
93
|
end
|
94
94
|
records
|
95
95
|
end
|
96
96
|
|
97
|
-
def object_values(array, context
|
97
|
+
def object_values(array, context = :default)
|
98
98
|
attributes = self.importers[context][:fields]
|
99
99
|
attributes = attributes.keys if attributes.is_a?(Hash)
|
100
100
|
args = array[0...attributes.size]
|
101
|
-
|
101
|
+
hash_values = Hash[attributes.zip(args)]
|
102
102
|
attributes.each do |attr|
|
103
|
-
if
|
104
|
-
if
|
105
|
-
|
106
|
-
|
107
|
-
elsif
|
108
|
-
|
109
|
-
elsif
|
110
|
-
|
103
|
+
if hash_values[attr].present?
|
104
|
+
if hash_values[attr].is_a?(Numeric)
|
105
|
+
hash_values[attr] = hash_values[attr].to_i if hash_values[attr].modulo(1).zero?
|
106
|
+
hash_values[attr] = (hash_values[attr].to_s.strip rescue '#ERRO AO CONVERTER NUMERO PARA TEXTO')
|
107
|
+
elsif hash_values[attr].is_a?(Date)
|
108
|
+
hash_values[attr] = (I18n.l(hash_values[attr], format: '%d/%m/%Y') rescue hash_values[attr]).to_s
|
109
|
+
elsif hash_values[attr].is_a?(DateTime)
|
110
|
+
hash_values[attr] = (I18n.l(hash_values[attr], format: '%d/%m/%Y %H:%i:%s') rescue hash_values[attr]).to_s
|
111
111
|
else
|
112
112
|
#PARA QUALQUER OUTRO VALOR, FORÇA CONVERTER PARA STRING
|
113
|
-
|
113
|
+
hash_values[attr] = (hash_values[attr].to_s.strip rescue '')
|
114
114
|
end
|
115
115
|
else
|
116
|
-
|
116
|
+
hash_values[attr] = ''
|
117
117
|
end
|
118
118
|
end
|
119
|
-
OpenStruct.new(
|
119
|
+
OpenStruct.new(hash_values)
|
120
120
|
end
|
121
121
|
|
122
122
|
def array_blank?(array)
|
123
|
-
array.all?
|
123
|
+
array.all?(&:blank?)
|
124
124
|
end
|
125
125
|
|
126
126
|
def importer_value(key, attributes)
|
127
127
|
if attributes.present?
|
128
|
-
if key
|
128
|
+
if key == :fields
|
129
129
|
self.importers[@importer_name][key] = (attributes.first.is_a?(Hash) ? attributes.inject(:merge) : attributes)
|
130
130
|
else
|
131
131
|
self.importers[@importer_name][key] = attributes
|
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.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Porto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
93
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.6.
|
94
|
+
rubygems_version: 2.6.12
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Rails Importer
|