ine-places 0.1.4 → 0.1.5

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
  SHA1:
3
- metadata.gz: 2aeb712180cbc9f2584a43b30e9d99057416fc28
4
- data.tar.gz: ae8db3e9b905b2072c81725132de81e48d43287e
3
+ metadata.gz: 1f493d2dcb6ff27f737d8cd440b7f40d19ddc641
4
+ data.tar.gz: 134911a07f947153b7b13be922714df90644d066
5
5
  SHA512:
6
- metadata.gz: 901d1a2771bd5a74d8d294ed08dc1b0a247ae46285006528885718978110d412665f0c3e0dd92f1721cfa7b3f1675896aad3aa3a32b5bab976b2711369f95c8e
7
- data.tar.gz: b6a293d00f5ab63684070025e9e01ade125b40c56018ab5dee805d3ff4b9432385c22596260bc05c0709464a11dde20a6750e1dcec3f1f862111aae30a9b4eb9
6
+ metadata.gz: ace4ecc42cfa96bc60015318e3b7d5aa075261420233d03a0fa8f44c48981276141cf6025c459193864e9bff60d20e51a5ceb22b16bc4cd7bfac063a6de0b7f0
7
+ data.tar.gz: f236a3d158ec2728ed01d9dac4eeb13c378bd65b03caf51a19af1246ce5d19fae562174c5d0600acd3d1eafbf6b89ced374b3e673d9b9967fe2d6ff6e98a8ea2
data/README.md CHANGED
@@ -183,7 +183,8 @@ You could hydratate the model `INE::Places::Place` using the `.hydrate` function
183
183
  ```ruby
184
184
  INE::Places.hydratate INE::Places::Place, 'spec/fixtures/happiness.csv', id_column: 'municipio',
185
185
  as: :happiness,
186
- value_column: 'value'
186
+ value_column: 'value',
187
+ convert_to: :float
187
188
  ```
188
189
 
189
190
  You just need to provide:
@@ -1,5 +1,5 @@
1
1
  module INE
2
2
  module Places
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
data/lib/ine/places.rb CHANGED
@@ -2,7 +2,7 @@ require "ine/places/version"
2
2
  require "csv"
3
3
  require "ostruct"
4
4
  require "active_support/all"
5
- require "byebug"
5
+ require "open-uri"
6
6
 
7
7
  module INE
8
8
  module Places
@@ -26,19 +26,23 @@ module INE
26
26
  end
27
27
 
28
28
  def self.hydratate(klass, data_path, options)
29
- unless File.file?(data_path)
30
- raise "Missing data file: #{data_path}"
31
- end
29
+ data = CSV.read(open(data_path), headers: true)
30
+
31
+ data.each do |row|
32
+ if obj = klass.find(row[options[:id_column]])
33
+ value = row[options[:value_column]]
34
+ case options[:convert_to]
35
+ when :float
36
+ value = value.to_f
37
+ when :integer, :int
38
+ value = value.to_i
39
+ end
32
40
 
33
- data = CSV.read(data_path, headers: true)
34
- klass.all.map do |obj|
35
- obj.tap do
36
- value = if r = data.detect{|row| row[options[:id_column]] == obj.id }
37
- r[options[:value_column]]
38
- end
39
41
  obj.data.send((options[:as].to_s + '=').to_sym, value)
40
42
  end
41
43
  end
44
+
45
+ nil
42
46
  end
43
47
  end
44
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ine-places
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Blat
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-08 00:00:00.000000000 Z
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport