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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea2e04834dd794fa817098b82c406eeeb04d455d
4
- data.tar.gz: 9694f70ee38074bd028f79235faf1a4b15086a7b
3
+ metadata.gz: f3635e5ee04bb01d660a84c489f677c293fd9a2e
4
+ data.tar.gz: 2dac3d5fd9b21739a18bb9dd89b600709792a631
5
5
  SHA512:
6
- metadata.gz: 5069b4dc1221033f5d91bf38f42017c992ef1696f839cb73d01d60476869fddbbd5dcda8ce5196833bcfb378af941e76354795133792b66b1a9d7dfb4206cc8d
7
- data.tar.gz: 2868fe0b8ea1cdefae76cdfb1c3a0928d9d18f26fcc919e3bd466bf4bb47c4d362a6dfdc04b23c185847d768812dee7e2b6fc2729ebcbb90fafe56a5273e55c9
6
+ metadata.gz: aa1341f372d03468fa320b8b77a919472abffc0ac68593d927f2270b90e1f899432ed3e0b107b517a66bfc70b8387da7f5c6e46e9066e2574c3d8acf85a97f74
7
+ data.tar.gz: e9133c2a604f506e05b59864db75c0e5d6cd9bdbd675dae991d6ef0e38316f72772505f4ba2c97610baf20986f6bea9811b80aca7a334c7593d5a952f5d3cdb6
@@ -37,7 +37,7 @@ module RailsImporter
37
37
  # end
38
38
  # end
39
39
 
40
- def importer(name=:default, &block)
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=:default)
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=:default)
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=:default)
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 unless i>0
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=:default)
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
- hValues = Hash[attributes.zip(args)]
101
+ hash_values = Hash[attributes.zip(args)]
102
102
  attributes.each do |attr|
103
- if hValues[attr].present?
104
- if hValues[attr].is_a?(Numeric)
105
- hValues[attr] = hValues[attr].to_i if hValues[attr].modulo(1).zero?
106
- hValues[attr] = (hValues[attr].to_s.strip rescue '#ERRO AO CONVERTER NUMERO PARA TEXTO')
107
- elsif hValues[attr].is_a?(Date)
108
- hValues[attr] = (I18n.l(hValues[attr], format: '%d/%m/%Y') rescue hValues[attr]).to_s
109
- elsif hValues[attr].is_a?(DateTime)
110
- hValues[attr] = (I18n.l(hValues[attr], format: '%d/%m/%Y %H:%i:%s') rescue hValues[attr]).to_s
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
- hValues[attr] = (hValues[attr].to_s.strip rescue '')
113
+ hash_values[attr] = (hash_values[attr].to_s.strip rescue '')
114
114
  end
115
115
  else
116
- hValues[attr] = ''
116
+ hash_values[attr] = ''
117
117
  end
118
118
  end
119
- OpenStruct.new(hValues)
119
+ OpenStruct.new(hash_values)
120
120
  end
121
121
 
122
122
  def array_blank?(array)
123
- array.all? {|i|i.nil? or i==''}
123
+ array.all?(&:blank?)
124
124
  end
125
125
 
126
126
  def importer_value(key, attributes)
127
127
  if attributes.present?
128
- if key==:fields
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
@@ -1,3 +1,3 @@
1
1
  module RailsImporter
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
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.8
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: 2017-02-14 00:00:00.000000000 Z
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.8
94
+ rubygems_version: 2.6.12
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: Rails Importer