csvash 1.0.0 → 1.1.1

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/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Csvash
2
2
 
3
- 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.
3
+ Got tired of parsing CSV files to your Models, or exporting into it?
4
+ Csvash automates that process for you. It handles your CSV extraction by mapping the document headers with your model properties and automagically creating new filled instances.
4
5
 
5
6
  ## Installation
6
7
 
@@ -16,7 +17,7 @@ And then execute:
16
17
  $ bundle
17
18
  ```
18
19
 
19
- Or install it yourself as:
20
+ Not using Rails? install it yourself with:
20
21
 
21
22
  ```
22
23
  $ gem install csvash
@@ -30,30 +31,27 @@ First of all you have to require it in your file:
30
31
  require 'csvash'
31
32
  ```
32
33
 
33
- To get a collection of hashes containing the csv data you can call:
34
+ In order to import from a CSV file you can pass the path where the file is allocated and a class:
34
35
 
35
36
  ```ruby
36
- Csvash.hashify '/path/of/your/file.csv'
37
+ Csvash.modelify_and_import '/path/of/your/file.csv', User
37
38
  ```
38
39
 
39
- Note: You can use `modelify_and_export` or `modelify_and_import` methods to generate or parse csv files respectively.
40
-
41
- In order to export, you can pass the desired path and CSV filename. Finally, you can pass a collection of already filled objects from a specific class you can pass the array:
40
+ However, to export you can pass the desired path and CSV filename. Finally, you can pass a collection of already filled objects from a specific class:
42
41
 
43
42
  ```ruby
44
43
  Csvash.modelify_and_export '/path/to/file.csv', collection
45
44
  ```
46
45
 
47
46
  _Where **collection** is an array of User objects, for example_
47
+ _* The path and its directories are created automatically_
48
48
 
49
- However, to import from a csv file you can pass the path where the file is allocated and a class, as usual:
49
+ To get just a collection of hashes containing the csv data you can call:
50
50
 
51
51
  ```ruby
52
- Csvash.modelify_and_import '/path/of/your/file.csv', User
52
+ Csvash.hashify '/path/of/your/file.csv'
53
53
  ```
54
54
 
55
- _* The path and its directories are created automatically_
56
-
57
55
  ##Options
58
56
 
59
57
  There are some behavior options that can be changed with its initializer. To create it just type:
@@ -67,6 +65,9 @@ These are the avaiable options
67
65
  ```ruby
68
66
  # If set to true it will only retain fields that matches the class to be filled. Default is false.
69
67
  # config.mass_assignment_safe = true
68
+
69
+ # Defines the default column separator to be used. Default is ';'
70
+ # column_separator = ';'
70
71
  ```
71
72
 
72
73
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module Csvash
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.1"
3
3
  end
data/lib/csvash.rb CHANGED
@@ -2,8 +2,11 @@ require 'csvash/version'
2
2
  require 'csv'
3
3
 
4
4
  module Csvash
5
- class << self; attr_accessor :mass_assignment_safe end
6
- @@mass_assignment_safe = false
5
+ class << self
6
+ attr_accessor :mass_assignment_safe, :column_separator
7
+ end
8
+ self.mass_assignment_safe = false
9
+ self.column_separator = ";"
7
10
 
8
11
  def self.setup
9
12
  yield self
@@ -34,7 +37,7 @@ private
34
37
  collection = []
35
38
  first_line = true
36
39
 
37
- CSV.foreach(path, :encoding => "ISO-8859-1:UTF-8", :col_sep => "\;") do |line|
40
+ CSV.foreach(path, :encoding => "ISO-8859-1:UTF-8", :col_sep => self.column_separator) do |line|
38
41
  if first_line
39
42
  # getting colum names and turning them into symbols
40
43
  cols = line.map! { |c| c.downcase.to_sym }
@@ -58,7 +61,7 @@ private
58
61
  unless collection.empty?
59
62
  headers = retrieve_headers collection
60
63
  begin
61
- CSV.open(file, 'wb', :col_sep => "\;") do |csv|
64
+ CSV.open(file, 'wb', :col_sep => self.column_separator) do |csv|
62
65
  csv << headers
63
66
  collection.each do |item|
64
67
  csv << retrieve_fields(item, headers)
@@ -1,4 +1,7 @@
1
1
  Csvash.setup do |config|
2
2
  # If set to true it will only retain fields that matches the class to be filled. Default is false.
3
3
  # config.mass_assignment_safe = true
4
+
5
+ # Defines the default column separator to be used. Default is ';'
6
+ # column_separator = ';'
4
7
  end
@@ -0,0 +1,5 @@
1
+ Year,Make,Model,Description,Price
2
+ 1997,Ford,E350,"ac, abs, moon",3000.00
3
+ 1999,"Chevy","Venture Extended Edition","",4900.00
4
+ 1999,"Chevy","Venture Extended Edition","Very Large","",5000.00
5
+ 1996,"Jeep","Grand Cherokee","MUST SELL! air, moon roof, loaded",4799.00
@@ -7,6 +7,12 @@ include TestHelper
7
7
 
8
8
  describe 'Modelifying' do
9
9
  describe "import" do
10
+ before :each do
11
+ Csvash.setup do |config|
12
+ config.mass_assignment_safe = false
13
+ config.column_separator = ";"
14
+ end
15
+ end
10
16
  it "passing the class" do
11
17
  cars_extracted = Csvash.modelify_and_import fetch_fixture_path('example.csv'), Car
12
18
  car = Car.new(
@@ -25,6 +31,11 @@ describe 'Modelifying' do
25
31
  Csvash.setup { |config| config.mass_assignment_safe = true }
26
32
  cars_extracted = Csvash.modelify_and_import fetch_fixture_path('example_mass_assignment.csv'), Car
27
33
  end
34
+
35
+ it "Changing default column separator" do
36
+ Csvash.setup { |config| config.column_separator = "," }
37
+ cars_extracted = Csvash.modelify_and_import fetch_fixture_path('example_comma.csv'), Car
38
+ end
28
39
  end
29
40
  describe "export" do
30
41
  before :each do
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: 1.0.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-27 00:00:00.000000000 Z
13
+ date: 2012-10-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -56,6 +56,7 @@ files:
56
56
  - test/.DS_Store
57
57
  - test/fixtures/.DS_Store
58
58
  - test/fixtures/example.csv
59
+ - test/fixtures/example_comma.csv
59
60
  - test/fixtures/example_mass_assignment.csv
60
61
  - test/fixtures/example_values.txt
61
62
  - test/hashifying_test.rb
@@ -91,6 +92,7 @@ test_files:
91
92
  - test/.DS_Store
92
93
  - test/fixtures/.DS_Store
93
94
  - test/fixtures/example.csv
95
+ - test/fixtures/example_comma.csv
94
96
  - test/fixtures/example_mass_assignment.csv
95
97
  - test/fixtures/example_values.txt
96
98
  - test/hashifying_test.rb