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 +4 -4
- data/README.md +2 -1
- data/lib/ine/places/version.rb +1 -1
- data/lib/ine/places.rb +14 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f493d2dcb6ff27f737d8cd440b7f40d19ddc641
|
4
|
+
data.tar.gz: 134911a07f947153b7b13be922714df90644d066
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
data/lib/ine/places/version.rb
CHANGED
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 "
|
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
|
-
|
30
|
-
|
31
|
-
|
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
|
+
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-
|
11
|
+
date: 2016-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|