csvash 0.0.1 → 0.1.0
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.
- data/.gitignore +1 -0
- data/README.md +2 -0
- data/csvash.gemspec +2 -0
- data/lib/csvash/version.rb +1 -1
- data/lib/csvash.rb +21 -3
- data/test/hashifying_test.rb +2 -2
- data/test/modelifying_test.rb +33 -0
- metadata +20 -4
- data/test/example.csv +0 -5
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Csvash
|
2
2
|
|
3
|
+
[](http://travis-ci.org/lukasalexandre/csvash) [](https://codeclimate.com/github/lukasalexandre/csvash)
|
4
|
+
|
3
5
|
Csvash automates your CSV extraction by mapping its headers with the columns contents and turning them into hashes that can be easily converted into ruby models.
|
4
6
|
|
5
7
|
## Installation
|
data/csvash.gemspec
CHANGED
data/lib/csvash/version.rb
CHANGED
data/lib/csvash.rb
CHANGED
@@ -2,9 +2,27 @@ require "csvash/version"
|
|
2
2
|
require 'csv'
|
3
3
|
|
4
4
|
module Csvash
|
5
|
+
# <b>DEPRECATED:</b> Please use <tt>useful</tt> instead.
|
5
6
|
def self.import_from_path(path)
|
6
|
-
|
7
|
+
warn "[DEPRECATION] `import_from_path` is deprecated. Please use `hashify` instead."
|
8
|
+
hashify path
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.hashify(path)
|
12
|
+
import path do |collection, current_line|
|
13
|
+
collection << current_line
|
14
|
+
end
|
15
|
+
end
|
7
16
|
|
17
|
+
def self.modelify(path, klass)
|
18
|
+
klass = klass.to_s.classify.constantize if klass.is_a?(String) || klass.is_a?(Symbol)
|
19
|
+
params = import path do |collection, current_line|
|
20
|
+
collection << klass.new(current_line)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def self.import(path, &block)
|
8
26
|
cols = nil
|
9
27
|
first_line = true
|
10
28
|
collection = []
|
@@ -19,8 +37,8 @@ module Csvash
|
|
19
37
|
|
20
38
|
# map the csv columns with the current line values
|
21
39
|
current_line = Hash[cols.zip(line.flatten)]
|
22
|
-
|
23
|
-
collection
|
40
|
+
|
41
|
+
block.call(collection, current_line)
|
24
42
|
end
|
25
43
|
collection
|
26
44
|
end
|
data/test/hashifying_test.rb
CHANGED
@@ -7,13 +7,13 @@ include TestHelper
|
|
7
7
|
|
8
8
|
describe 'Hashifiyng' do
|
9
9
|
it "checking columns" do
|
10
|
-
hash = Csvash.
|
10
|
+
hash = Csvash.hashify fetch_fixture_path('example.csv')
|
11
11
|
hash.first.keys.size.must_equal 5
|
12
12
|
hash.first.keys.must_equal [:year, :make, :model, :description, :price]
|
13
13
|
end
|
14
14
|
|
15
15
|
it "checking values" do
|
16
|
-
hash = Csvash.
|
16
|
+
hash = Csvash.hashify fetch_fixture_path('example.csv')
|
17
17
|
txt = File.read fetch_fixture_path('example_values.txt')
|
18
18
|
hash.map(&:values).must_equal eval(txt)
|
19
19
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'csvash'
|
4
|
+
require "./test/test_helper"
|
5
|
+
|
6
|
+
include TestHelper
|
7
|
+
|
8
|
+
describe 'Modelifying' do
|
9
|
+
it "passing the class" do
|
10
|
+
cars_extracted = Csvash.modelify fetch_fixture_path('example.csv'), Car
|
11
|
+
car = Car.new(
|
12
|
+
year: '1997',
|
13
|
+
make: 'Ford',
|
14
|
+
model: 'E350',
|
15
|
+
description: 'ac, abs, moon',
|
16
|
+
price: '3000.00'
|
17
|
+
)
|
18
|
+
cars_extracted.first.instance_variables.each do |name|
|
19
|
+
cars_extracted.first.instance_variable_get(name).must_equal car.instance_variable_get(name)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Car
|
25
|
+
def initialize(params)
|
26
|
+
self.year = params[:year]
|
27
|
+
self.make = params[:make]
|
28
|
+
self.model = params[:model]
|
29
|
+
self.description = params[:description]
|
30
|
+
self.price = params[:price]
|
31
|
+
end
|
32
|
+
attr_accessor :year, :make, :model, :description, :price
|
33
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csvash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,23 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-08-29 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
description: Csvash automates your CSV extraction by mapping its headers with the
|
15
31
|
columns contents and turning them into hashes that can be easily converted into
|
16
32
|
ruby models.
|
@@ -28,10 +44,10 @@ files:
|
|
28
44
|
- csvash.gemspec
|
29
45
|
- lib/csvash.rb
|
30
46
|
- lib/csvash/version.rb
|
31
|
-
- test/example.csv
|
32
47
|
- test/fixtures/example.csv
|
33
48
|
- test/fixtures/example_values.txt
|
34
49
|
- test/hashifying_test.rb
|
50
|
+
- test/modelifying_test.rb
|
35
51
|
- test/test_helper.rb
|
36
52
|
homepage: https://github.com/lukasalexandre/csvash
|
37
53
|
licenses: []
|
@@ -59,8 +75,8 @@ specification_version: 3
|
|
59
75
|
summary: Csvash automates your CSV extraction by mapping its headers with the columns
|
60
76
|
contents and turning them into hashes that can be easily converted into ruby models.
|
61
77
|
test_files:
|
62
|
-
- test/example.csv
|
63
78
|
- test/fixtures/example.csv
|
64
79
|
- test/fixtures/example_values.txt
|
65
80
|
- test/hashifying_test.rb
|
81
|
+
- test/modelifying_test.rb
|
66
82
|
- test/test_helper.rb
|
data/test/example.csv
DELETED