csv-monster 1.0.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/lib/csvmonster.rb +53 -0
- metadata +46 -0
data/lib/csvmonster.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
class CSVMonster
|
2
|
+
|
3
|
+
attr_reader :input_file, :delimiter, :output_table_object, :object_attributes
|
4
|
+
|
5
|
+
def initialize(input_file , delimiter)
|
6
|
+
@input_file = input_file
|
7
|
+
@delimiter = delimiter || ","
|
8
|
+
@output_table_object = []
|
9
|
+
@object_attributes = extract_the_line_of_attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
### this is the main method to read the file: csv_monster_obj.parse_csv
|
13
|
+
def parse_csv
|
14
|
+
i=0
|
15
|
+
output_matrix = []
|
16
|
+
trim_return_carriage(fulltext).each_line do |line|
|
17
|
+
output_matrix << trim_line_ends(line).split(delimiter) unless i == 0
|
18
|
+
i+=1
|
19
|
+
end
|
20
|
+
output_matrix.each do |rec|
|
21
|
+
temp_hsh = {}
|
22
|
+
(0..rec.size-1).each do |i|
|
23
|
+
temp_hsh.merge! object_attributes[i] => rec[i]
|
24
|
+
end
|
25
|
+
@output_table_object << temp_hsh
|
26
|
+
end
|
27
|
+
|
28
|
+
output_table_object
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
protected
|
33
|
+
def extract_the_line_of_attributes
|
34
|
+
trim_return_carriage(fulltext).each_line do |line|
|
35
|
+
return trim_line_ends(line).split(delimiter || ',').map{|x| x.to_s.downcase.to_sym}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def fulltext
|
40
|
+
File.open(input_file).read
|
41
|
+
end
|
42
|
+
|
43
|
+
def trim_return_carriage(text)
|
44
|
+
text.gsub!(/\r\n?/, "\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
def trim_line_ends(text)
|
48
|
+
text.gsub!("\n","")
|
49
|
+
text.gsub!("\t","")
|
50
|
+
text
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv-monster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aghyad Saleh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-23 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: CSV parsing gem
|
15
|
+
email: aghyadsaleh@hotmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/csvmonster.rb
|
21
|
+
homepage: http://rubygems.org/gems/csvmonster
|
22
|
+
licenses:
|
23
|
+
- N/A
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.25
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: CSV parsing gem
|
46
|
+
test_files: []
|