csv_class_maker 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/csv_class_maker/csv_class_maker.rb +0 -4
- data/lib/csv_class_maker/csv_find.rb +15 -16
- data/spec/csv_class_maker_spec.rb +3 -1
- metadata +11 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b032060813fda21e2eb3fde8b38f226e0e84cda9
|
4
|
+
data.tar.gz: 8a29ae5280459bb8bd4ebee4152ea05778659c38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f1b4b41e13cd8bf08448b18a95ad6841cfa6c8b90a7d291ccb2a334a7f1fd47a1ff841fae6b55a1096e3dcefab25294a3020b9fc5ed8d27b0f2e388ca231d82
|
7
|
+
data.tar.gz: 37d0051e32be666203322d76d57e886add86f3fbc84e0897343e7bcbe8fe053a9164543126ae5db92f44d212ac755328f469fb4b43b7cc2e12ad4975106c2f20
|
@@ -14,10 +14,6 @@ module CsvClassMaker
|
|
14
14
|
)
|
15
15
|
|
16
16
|
Object.const_set(class_name, Class.new do
|
17
|
-
# remove building of the struct here. completely self contain method
|
18
|
-
# generation into the csv_find module so that you can pull extract headers
|
19
|
-
# method out.
|
20
|
-
|
21
17
|
require 'csv_class_maker/csv_find'
|
22
18
|
include CsvFind
|
23
19
|
|
@@ -27,29 +27,27 @@ module CsvClassMaker::CsvFind
|
|
27
27
|
|
28
28
|
module ClassMethods
|
29
29
|
def csv_file(file_name, options = {})
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
@file_options = options
|
31
|
+
@file = CSV.new(File.open(file_name, 'r'), @file_options)
|
32
|
+
@first_line = 2
|
33
|
+
@last_line = `wc -l #{file_name}`.split(' ').first.to_i
|
34
|
+
@middle_line = (@last_line/2)+1
|
35
35
|
@line_number = nil
|
36
36
|
extract_headers(file_name, options)
|
37
37
|
define_accessors
|
38
38
|
end
|
39
39
|
|
40
|
-
def headers; return
|
41
|
-
def file; return
|
42
|
-
def first_line; return
|
43
|
-
def middle_line; return
|
44
|
-
def last_line; return
|
45
|
-
def file_options; return
|
40
|
+
def headers; return @headers; end
|
41
|
+
def file; return @file; end
|
42
|
+
def first_line; return @first_line; end
|
43
|
+
def middle_line; return @middle_line; end
|
44
|
+
def last_line; return @last_line; end
|
45
|
+
def file_options; return @file_options; end
|
46
46
|
|
47
47
|
def define_accessors
|
48
48
|
headers.each do |header|
|
49
49
|
self.send(:attr_accessor, header)
|
50
50
|
end
|
51
|
-
|
52
|
-
# self.send(:attr_accessor, :line_number)
|
53
51
|
end
|
54
52
|
|
55
53
|
def all
|
@@ -67,7 +65,7 @@ module CsvClassMaker::CsvFind
|
|
67
65
|
end
|
68
66
|
|
69
67
|
def find(line_number)
|
70
|
-
row = if (first_line..
|
68
|
+
row = if (first_line..middle_line).include? line_number
|
71
69
|
front_find(line_number, file.path)
|
72
70
|
elsif (middle_line..last_line).include? line_number
|
73
71
|
back_find(line_number, file.path)
|
@@ -99,7 +97,7 @@ module CsvClassMaker::CsvFind
|
|
99
97
|
def extract_headers(file_name, options)
|
100
98
|
csv_file = File.open(file_name,'r')
|
101
99
|
|
102
|
-
|
100
|
+
@headers ||= CSV.new(csv_file, options).first.map do |headers, values|
|
103
101
|
headers
|
104
102
|
end
|
105
103
|
end
|
@@ -137,8 +135,9 @@ module CsvClassMaker::CsvFind
|
|
137
135
|
command = `head -n 1 #{file_path} && head -n #{line_number} #{file_path} | tail -n 1`
|
138
136
|
CSV.new(command, file_options).first
|
139
137
|
end
|
138
|
+
|
140
139
|
def back_find(line_number, file_path)
|
141
|
-
command = `head -n 1 #{file_path} && tail -n #{last_line - line_number} #{file_path} | head -n 1`
|
140
|
+
command = `head -n 1 #{file_path} && tail -n #{(last_line + 1) - line_number} #{file_path} | head -n 1`
|
142
141
|
CSV.new(command, file_options).first
|
143
142
|
end
|
144
143
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require './lib/csv_class_maker'
|
2
|
-
require 'byebug'
|
3
2
|
|
4
3
|
describe CsvClassMaker do
|
5
4
|
context 'class methods' do
|
@@ -47,7 +46,10 @@ describe Object do
|
|
47
46
|
end
|
48
47
|
|
49
48
|
it ".find returns correctly" do
|
49
|
+
expect(People.find(2)).to eq @person1
|
50
50
|
expect(People.find(3)).to eq @person2
|
51
|
+
expect(People.find(4)).to eq @person3
|
52
|
+
expect(People.find(5)).to eq @person4
|
51
53
|
end
|
52
54
|
|
53
55
|
it "responds to .find_by" do
|
metadata
CHANGED
@@ -1,53 +1,39 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_class_maker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Platt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 2.14.1
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 2.14.1
|
27
|
-
|
28
|
-
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 3.5.1
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ~>
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 3.5.1
|
41
|
-
description: '''csv_class_maker will create a class out of your csv''s headers, and
|
42
|
-
provide some methods for finding data in the CSV'''
|
27
|
+
description: "'csv_class_maker will create a class out of your csv's headers, and
|
28
|
+
provide some methods for finding data in the CSV'"
|
43
29
|
email: mplatt@mrkplt.com
|
44
30
|
executables: []
|
45
31
|
extensions: []
|
46
32
|
extra_rdoc_files: []
|
47
33
|
files:
|
34
|
+
- lib/csv_class_maker.rb
|
48
35
|
- lib/csv_class_maker/csv_class_maker.rb
|
49
36
|
- lib/csv_class_maker/csv_find.rb
|
50
|
-
- lib/csv_class_maker.rb
|
51
37
|
- spec/csv_class_maker_spec.rb
|
52
38
|
homepage: http://mrkplt.com
|
53
39
|
licenses:
|
@@ -59,19 +45,19 @@ require_paths:
|
|
59
45
|
- lib
|
60
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
47
|
requirements:
|
62
|
-
- -
|
48
|
+
- - ">="
|
63
49
|
- !ruby/object:Gem::Version
|
64
50
|
version: 2.0.0
|
65
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
52
|
requirements:
|
67
|
-
- -
|
53
|
+
- - ">="
|
68
54
|
- !ruby/object:Gem::Version
|
69
55
|
version: '0'
|
70
56
|
requirements: []
|
71
57
|
rubyforge_project:
|
72
|
-
rubygems_version: 2.
|
58
|
+
rubygems_version: 2.4.5
|
73
59
|
signing_key:
|
74
60
|
specification_version: 4
|
75
|
-
summary: '
|
61
|
+
summary: "'Object oriented CSV reading'"
|
76
62
|
test_files:
|
77
63
|
- spec/csv_class_maker_spec.rb
|