csv_class_maker 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/csv_class_maker/csv_class_maker.rb +16 -5
- data/lib/csv_class_maker/csv_find.rb +6 -3
- data/spec/csv_class_maker_spec.rb +5 -0
- metadata +16 -12
- checksums.yaml +0 -7
@@ -5,8 +5,15 @@
|
|
5
5
|
|
6
6
|
module CsvClassMaker
|
7
7
|
require 'csv'
|
8
|
-
|
9
|
-
|
8
|
+
|
9
|
+
def self.generate_class(class_name, file_name, options = {})
|
10
|
+
options.merge!(
|
11
|
+
headers: true,
|
12
|
+
header_converters: :symbol,
|
13
|
+
return_headers: false
|
14
|
+
)
|
15
|
+
|
16
|
+
Object.const_set class_name, Struct.new(*extract_headers(file_name, options)) {
|
10
17
|
|
11
18
|
# Class definition for dynamically generated classes.
|
12
19
|
require 'csv_class_maker/csv_find'
|
@@ -18,7 +25,8 @@ module CsvClassMaker
|
|
18
25
|
end
|
19
26
|
end
|
20
27
|
|
21
|
-
@@
|
28
|
+
@@file_options = options
|
29
|
+
@@file = CSV.new(File.open(file_name, 'r'), @@file_options)
|
22
30
|
@@first_line = 2
|
23
31
|
@@last_line = `wc -l #{file_name}`.split(' ').first.to_i
|
24
32
|
@@middle_line = (@@last_line/2)+1
|
@@ -30,6 +38,7 @@ module CsvClassMaker
|
|
30
38
|
def self.first_line; return @@first_line; end
|
31
39
|
def self.middle_line; return @@middle_line; end
|
32
40
|
def self.last_line; return @@last_line; end
|
41
|
+
def self.file_options; return @@file_options; end
|
33
42
|
|
34
43
|
# End of class definition.
|
35
44
|
|
@@ -38,9 +47,11 @@ module CsvClassMaker
|
|
38
47
|
|
39
48
|
private
|
40
49
|
|
41
|
-
def self.extract_headers(file_name)
|
50
|
+
def self.extract_headers(file_name, options)
|
42
51
|
@csv_headers = []
|
43
|
-
|
52
|
+
csv_file = File.open(file_name,'r')
|
53
|
+
|
54
|
+
CSV.new(csv_file, options).first.each do |headers, values|
|
44
55
|
@csv_headers << headers
|
45
56
|
end
|
46
57
|
@csv_headers
|
@@ -38,7 +38,8 @@ module CsvClassMaker::CsvFind
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def last
|
41
|
-
|
41
|
+
command = `head -n 1 #{file.path} && tail -n 1 #{file.path}`
|
42
|
+
last_row = CSV.new(command, file_options).first
|
42
43
|
build_instance last_row, last_line
|
43
44
|
end
|
44
45
|
|
@@ -93,10 +94,12 @@ module CsvClassMaker::CsvFind
|
|
93
94
|
end
|
94
95
|
|
95
96
|
def front_find(line_number, file_path)
|
96
|
-
|
97
|
+
command = `head -n 1 #{file_path} && head -n #{line_number} #{file_path} | tail -n 1`
|
98
|
+
CSV.new(command, file_options).first
|
97
99
|
end
|
98
100
|
def back_find(line_number, file_path)
|
99
|
-
|
101
|
+
command = `head -n 1 #{file_path} && tail -n #{last_line - line_number} #{file_path} | head -n 1`
|
102
|
+
CSV.new(command, file_options).first
|
100
103
|
end
|
101
104
|
|
102
105
|
end
|
@@ -7,6 +7,11 @@ describe CsvClassMaker do
|
|
7
7
|
CsvClassMaker::generate_class 'People', 'spec/support/demo.csv'
|
8
8
|
Object.constants.include? :People
|
9
9
|
end
|
10
|
+
|
11
|
+
it 'takes a file options' do
|
12
|
+
people = CsvClassMaker::generate_class 'Other', 'spec/support/delimeter.csv', col_sep: "\t"
|
13
|
+
people.first.should respond_to(:nickname)
|
14
|
+
end
|
10
15
|
end
|
11
16
|
end
|
12
17
|
end
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_class_maker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Mark Platt
|
@@ -13,51 +14,54 @@ dependencies:
|
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rspec
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
|
-
- -
|
19
|
+
- - ~>
|
18
20
|
- !ruby/object:Gem::Version
|
19
21
|
version: '0'
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
|
-
- -
|
27
|
+
- - ~>
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '0'
|
27
|
-
description:
|
28
|
-
provide some methods for finding data in the CSV'
|
30
|
+
description: ! '''csv_class_maker will create a class out of your csv''s headers,
|
31
|
+
and provide some methods for finding data in the CSV'''
|
29
32
|
email: mplatt@mrkplt.com
|
30
33
|
executables: []
|
31
34
|
extensions: []
|
32
35
|
extra_rdoc_files: []
|
33
36
|
files:
|
34
|
-
- lib/csv_class_maker.rb
|
35
37
|
- lib/csv_class_maker/csv_class_maker.rb
|
36
38
|
- lib/csv_class_maker/csv_find.rb
|
39
|
+
- lib/csv_class_maker.rb
|
37
40
|
- spec/csv_class_maker_spec.rb
|
38
41
|
homepage: http://mrkplt.com
|
39
42
|
licenses:
|
40
43
|
- MIT
|
41
|
-
metadata: {}
|
42
44
|
post_install_message:
|
43
45
|
rdoc_options: []
|
44
46
|
require_paths:
|
45
47
|
- lib
|
46
48
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
47
50
|
requirements:
|
48
|
-
- -
|
51
|
+
- - ! '>='
|
49
52
|
- !ruby/object:Gem::Version
|
50
53
|
version: 1.9.3
|
51
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
52
56
|
requirements:
|
53
|
-
- -
|
57
|
+
- - ! '>='
|
54
58
|
- !ruby/object:Gem::Version
|
55
59
|
version: '0'
|
56
60
|
requirements: []
|
57
61
|
rubyforge_project:
|
58
|
-
rubygems_version:
|
62
|
+
rubygems_version: 1.8.23
|
59
63
|
signing_key:
|
60
|
-
specification_version:
|
61
|
-
summary:
|
64
|
+
specification_version: 3
|
65
|
+
summary: ! '''Object oriented CSV reading'''
|
62
66
|
test_files:
|
63
67
|
- spec/csv_class_maker_spec.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 1b8b85b814a7b26331ffd7677f375a0eb8f9c5bb
|
4
|
-
data.tar.gz: 90ee267cefb5291d54b77c6753f2423091126aa3
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: a5b71f57eb29c3a6eb1cbef8b5308e717f73f07766d491f4facaa65603d1111c5d31c36e48716250f88605d7747c4acb58fb34b4742ae7252bf00a65719204dc
|
7
|
-
data.tar.gz: ae2ec65f7486a935e00102b6437044c9e972f59b69021907282d549082aa03b82cf03f496949ad419c6a9829529c51ca57869e5617260261f207b7e1fe989307
|