ine-places 0.1.3 → 0.1.4

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: 520d85a6b88bda35a7421a32b03c1466eb9f0905
4
- data.tar.gz: d449952c735544ee62c8349f6df93f4c651ce6e6
3
+ metadata.gz: 2aeb712180cbc9f2584a43b30e9d99057416fc28
4
+ data.tar.gz: ae8db3e9b905b2072c81725132de81e48d43287e
5
5
  SHA512:
6
- metadata.gz: 9cdd68d2602127d859640d2b0b523af92a9987811776e50078a6546e158b929cde2e9a150f55bdd1c563dbcc5d4e625dc82eb6f21bb58fea8affe3da0d9a3724
7
- data.tar.gz: 2a15d0086ad6174d62610541d95596ad4d6f0f8b56474b44e5e1836a092ed0c067aa15f4547712ddd862ce6e16a299bb513943d73f80263065a252b638f285f1
6
+ metadata.gz: 901d1a2771bd5a74d8d294ed08dc1b0a247ae46285006528885718978110d412665f0c3e0dd92f1721cfa7b3f1675896aad3aa3a32b5bab976b2711369f95c8e
7
+ data.tar.gz: b6a293d00f5ab63684070025e9e01ade125b40c56018ab5dee805d3ff4b9432385c22596260bc05c0709464a11dde20a6750e1dcec3f1f862111aae30a9b4eb9
data/README.md CHANGED
@@ -165,6 +165,40 @@ INE::Places::Place.find_by_slug('burjassot')
165
165
  => #<INE::Places::Place id="46078", name="Burjassot", slug="burjassot", province_id="46", lon="-0.4135963", lat="39.5096699", province=#<INE::Places::Province id="46", name="Valencia/València", slug="valencia", autonomous_region_id="10", lon="-0.3762881", lat="39.4699075", autonomous_region=#<INE::Places::AutonomousRegion id="10", name="Comunidad Valenciana", slug="comunidad-valenciana", lon="-0.7532808999999999", lat="39.4840108">>>
166
166
  ```
167
167
 
168
+ ## Hydratating models with data (experimental feature)
169
+
170
+ It's quite common to work with data related with the entities provided by this gem. For example, to know the debt of a place, the amount of schools, the number of inhabitants, and so on. This data lives in external sources, such a CSV or a JSON file, or an API endpoint but can be co-related with the entities using an ID column.
171
+
172
+ Hydratation feature helps you with this issue. For the moment it's very experimental but allows you to add basics sets of data from a local CSV file.
173
+
174
+ For example, given a CSV file with a single row:
175
+
176
+ ```csv
177
+ municipio,value
178
+ 28079,33
179
+ ```
180
+
181
+ You could hydratate the model `INE::Places::Place` using the `.hydrate` function:
182
+
183
+ ```ruby
184
+ INE::Places.hydratate INE::Places::Place, 'spec/fixtures/happiness.csv', id_column: 'municipio',
185
+ as: :happiness,
186
+ value_column: 'value'
187
+ ```
188
+
189
+ You just need to provide:
190
+
191
+ - id column, to match the IDs
192
+ - the `as` column, with the name of the new method
193
+ - the value column, to know exactly where the value is
194
+
195
+ After hydratating a class, every object of the class has the new method in the `.data` property:
196
+
197
+ ```ruby
198
+ place.data.happiness
199
+ #> 100
200
+ ```
201
+
168
202
  ## Development
169
203
 
170
204
  After checking out the repository, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require_relative "../lib/ine/places/places"
4
+ require_relative "../lib/ine/places"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -27,9 +27,10 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- spec.add_runtime_dependency "activesupport", "~> 4.2", ">= 4.2.4"
30
+ spec.add_runtime_dependency "activesupport", ">= 4.2", "<= 5.1"
31
31
 
32
32
  spec.add_development_dependency "bundler", "~> 1.12"
33
33
  spec.add_development_dependency "rake", "~> 11.1"
34
34
  spec.add_development_dependency "rspec", "~> 3.4"
35
+ spec.add_development_dependency "byebug", '~> 9.0', '>= 9.0.5'
35
36
  end
@@ -2,6 +2,7 @@ require "ine/places/version"
2
2
  require "csv"
3
3
  require "ostruct"
4
4
  require "active_support/all"
5
+ require "byebug"
5
6
 
6
7
  module INE
7
8
  module Places
@@ -23,5 +24,21 @@ module INE
23
24
 
24
25
  nil
25
26
  end
27
+
28
+ def self.hydratate(klass, data_path, options)
29
+ unless File.file?(data_path)
30
+ raise "Missing data file: #{data_path}"
31
+ end
32
+
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
+ obj.data.send((options[:as].to_s + '=').to_sym, value)
40
+ end
41
+ end
42
+ end
26
43
  end
27
44
  end
@@ -7,6 +7,10 @@ class INE::Places::Place < OpenStruct
7
7
  collection_klass.records.select{ |place| place.province_id == province_id }
8
8
  end
9
9
 
10
+ def data
11
+ @data ||= OpenStruct.new
12
+ end
13
+
10
14
  private
11
15
 
12
16
  def self.collection_klass
@@ -1,5 +1,5 @@
1
1
  module INE
2
2
  module Places
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,35 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ine-places
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
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-05-09 00:00:00.000000000 Z
11
+ date: 2016-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.2'
20
- - - ">="
20
+ - - "<="
21
21
  - !ruby/object:Gem::Version
22
- version: 4.2.4
22
+ version: '5.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - "~>"
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
29
  version: '4.2'
30
- - - ">="
30
+ - - "<="
31
31
  - !ruby/object:Gem::Version
32
- version: 4.2.4
32
+ version: '5.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +72,26 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '3.4'
75
+ - !ruby/object:Gem::Dependency
76
+ name: byebug
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '9.0'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 9.0.5
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '9.0'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 9.0.5
75
95
  description: Work with Spanish regions using INE codes and data
76
96
  email:
77
97
  - ferblape@gmail.com