csv_find 0.0.1 → 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.
- checksums.yaml +5 -5
- data/lib/csv_find/csv_find.rb +35 -31
- data/spec/csv_find_spec.rb +70 -41
- metadata +23 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2b108651a75a452e5eb1cd0baf8ea4b3203d1de7c5c1481f2cb8a5b6a872a775
|
4
|
+
data.tar.gz: 91b761d46c8d1d633db45e34e79b3d97258aa56c4664e7e6406d49015b64bf0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c394b06f9316d894e2b025265212efde56bcaa1a4a016b0fc0f296f96b0c05d925b43d209c7e305ccc8482e53de179295806c3f57643621105f56d0e5ae58d4
|
7
|
+
data.tar.gz: e85c5055eed62026a762f36cf5c1b956c774c06e54157d27530fa1b88c94e6809655522c7913f1cfd4baa86737c40a7fb49b830f800435253809116c02a0a099
|
data/lib/csv_find/csv_find.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'English'
|
4
|
+
|
5
|
+
module CsvFind #:nodoc:
|
2
6
|
require 'csv'
|
3
7
|
|
4
8
|
VERSION = {
|
5
|
-
major: '
|
9
|
+
major: '1',
|
6
10
|
minor: '0',
|
7
|
-
patch: '
|
11
|
+
patch: '0'
|
8
12
|
}.values.join('.')
|
9
13
|
|
10
14
|
def self.included(base)
|
@@ -12,17 +16,17 @@ module CsvFind
|
|
12
16
|
base.send :prepend, InstanceMethods
|
13
17
|
end
|
14
18
|
|
15
|
-
module InstanceMethods
|
19
|
+
module InstanceMethods #:nodoc:
|
16
20
|
attr_accessor :line_number
|
17
21
|
|
18
22
|
def initialize(hash = {})
|
19
|
-
hash.each { |k,v| send("#{k}=".to_sym, v) }
|
23
|
+
hash.each { |k, v| send("#{k}=".to_sym, v) }
|
20
24
|
end
|
21
25
|
|
22
|
-
def ==(
|
26
|
+
def ==(other)
|
23
27
|
instance_variables.map do |instance_variable|
|
24
28
|
instance_variable_get(instance_variable) ==
|
25
|
-
|
29
|
+
other.instance_variable_get(instance_variable)
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
@@ -31,7 +35,7 @@ module CsvFind
|
|
31
35
|
attr_reader :hash
|
32
36
|
end
|
33
37
|
|
34
|
-
module ClassMethods
|
38
|
+
module ClassMethods #:nodoc:
|
35
39
|
include Enumerable
|
36
40
|
|
37
41
|
attr_reader :headers, :file, :file_options,
|
@@ -39,10 +43,10 @@ module CsvFind
|
|
39
43
|
|
40
44
|
def csv_file(file_name, options = {})
|
41
45
|
@file_options = default_options.merge(options)
|
42
|
-
@file = CSV.new(File.open(file_name, 'r'),
|
46
|
+
@file = CSV.new(File.open(file_name, 'r'), **@file_options)
|
43
47
|
@first_line = 2
|
44
48
|
@last_line = `wc -l #{file_name}`.split(' ').first.to_i
|
45
|
-
@middle_line = (@last_line/2) + 1
|
49
|
+
@middle_line = (@last_line / 2) + 1
|
46
50
|
@line_number = nil
|
47
51
|
@headers = extract_headers(file_name, file_options)
|
48
52
|
|
@@ -65,22 +69,21 @@ module CsvFind
|
|
65
69
|
end
|
66
70
|
|
67
71
|
def find_by(key_val_pair)
|
68
|
-
warn DEPRECATION_MESSAGE
|
69
72
|
row = search(key_val_pair).last
|
70
73
|
row.nil? ? nil : build_instance(row, row[:line_number])
|
71
74
|
end
|
72
75
|
|
73
76
|
def find_all_by(key_val_pair)
|
74
|
-
warn DEPRECATION_MESSAGE
|
75
77
|
where(key_val_pair)
|
76
78
|
end
|
77
79
|
|
78
80
|
def find(line_number)
|
79
|
-
row =
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
row =
|
82
|
+
if (first_line..middle_line).include?(line_number)
|
83
|
+
front_find(line_number, file.path)
|
84
|
+
else
|
85
|
+
back_find(line_number, file.path)
|
86
|
+
end
|
84
87
|
|
85
88
|
row.nil? ? row : build_instance(row, line_number)
|
86
89
|
end
|
@@ -92,7 +95,7 @@ module CsvFind
|
|
92
95
|
|
93
96
|
def last
|
94
97
|
command = `head -n 1 #{file.path} && tail -n 1 #{file.path}`
|
95
|
-
last_row = CSV.new(command, file_options).first
|
98
|
+
last_row = CSV.new(command, **file_options).first
|
96
99
|
build_instance(last_row, last_line)
|
97
100
|
end
|
98
101
|
|
@@ -105,10 +108,6 @@ module CsvFind
|
|
105
108
|
|
106
109
|
private
|
107
110
|
|
108
|
-
DEPRECATION_MESSAGE =
|
109
|
-
'[DEPRECATION] This method is deprecated and will be removed in v2.' <<
|
110
|
-
'Please user #where.'
|
111
|
-
|
112
111
|
def default_options
|
113
112
|
{
|
114
113
|
headers: true,
|
@@ -118,8 +117,8 @@ module CsvFind
|
|
118
117
|
end
|
119
118
|
|
120
119
|
def extract_headers(file_name, options)
|
121
|
-
csv_file = File.open(file_name,'r')
|
122
|
-
CSV.new(csv_file, options).first.headers
|
120
|
+
csv_file = File.open(file_name, 'r')
|
121
|
+
CSV.new(csv_file, **options).first.headers
|
123
122
|
end
|
124
123
|
|
125
124
|
def build_instance(row, line)
|
@@ -147,19 +146,24 @@ module CsvFind
|
|
147
146
|
def dig(hash_pair, rows)
|
148
147
|
rows.select do |row|
|
149
148
|
if row[hash_pair.first] == hash_pair.last
|
150
|
-
|
149
|
+
$NR != last_line ? row.push(line_number: $NR) : row
|
151
150
|
end
|
152
151
|
end
|
153
152
|
end
|
154
153
|
|
155
|
-
def front_find(line_number,
|
156
|
-
command =
|
157
|
-
|
154
|
+
def front_find(line_number, path)
|
155
|
+
command =
|
156
|
+
`head -n 1 #{path} && head -n #{line_number} #{path} | tail -n 1`
|
157
|
+
|
158
|
+
CSV.new(command, **file_options).first
|
158
159
|
end
|
159
160
|
|
160
|
-
def back_find(line_number,
|
161
|
-
|
162
|
-
|
161
|
+
def back_find(line_number, path)
|
162
|
+
mid = (last_line + 1) - line_number
|
163
|
+
command =
|
164
|
+
`head -n 1 #{path} && tail -n #{mid} #{path} | head -n 1`
|
165
|
+
|
166
|
+
CSV.new(command, **file_options).first
|
163
167
|
end
|
164
168
|
end
|
165
169
|
end
|
data/spec/csv_find_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require './lib/csv_find'
|
2
4
|
|
3
5
|
class People
|
@@ -6,98 +8,125 @@ class People
|
|
6
8
|
end
|
7
9
|
|
8
10
|
describe People do
|
9
|
-
before(:all)
|
10
|
-
@person1 = People.new(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
before(:all) do
|
12
|
+
@person1 = People.new(
|
13
|
+
first: 'Mark',
|
14
|
+
last: 'Platt',
|
15
|
+
nickname: 'JCool',
|
16
|
+
line_number: 2
|
17
|
+
)
|
18
|
+
|
19
|
+
@person2 = People.new(
|
20
|
+
first: 'Longinus',
|
21
|
+
last: 'Smith',
|
22
|
+
nickname: 'Pebbles',
|
23
|
+
line_number: 3
|
24
|
+
)
|
25
|
+
|
26
|
+
@person3 = People.new(
|
27
|
+
first: 'Johnny',
|
28
|
+
last: 'Radiation',
|
29
|
+
nickname: 'Pebbles',
|
30
|
+
line_number: 4
|
31
|
+
)
|
32
|
+
|
33
|
+
@person4 = People.new(
|
34
|
+
first: 'Charlie',
|
35
|
+
last: 'Mansfield',
|
36
|
+
nickname: 'Sammykins',
|
37
|
+
line_number: 5
|
38
|
+
)
|
39
|
+
end
|
15
40
|
|
16
41
|
context 'class methods' do
|
17
|
-
it
|
18
|
-
(People).
|
42
|
+
it 'responds to .first' do
|
43
|
+
expect(People).to respond_to(:first)
|
19
44
|
end
|
20
45
|
|
21
|
-
it
|
46
|
+
it '.first returns correctly' do
|
22
47
|
expect(People.first).to eq @person1
|
23
48
|
end
|
24
49
|
|
25
|
-
it
|
26
|
-
(People).
|
50
|
+
it 'responds to .last' do
|
51
|
+
expect(People).to respond_to(:last)
|
27
52
|
end
|
28
53
|
|
29
|
-
it
|
54
|
+
it '.last returns correctly' do
|
30
55
|
expect(People.last).to eq @person4
|
31
56
|
end
|
32
57
|
|
33
|
-
it
|
34
|
-
(People).
|
58
|
+
it 'responds to .find' do
|
59
|
+
expect(People).to respond_to(:find)
|
35
60
|
end
|
36
61
|
|
37
|
-
it
|
62
|
+
it '.find returns correctly' do
|
38
63
|
expect(People.find(2)).to eq @person1
|
39
64
|
expect(People.find(3)).to eq @person2
|
40
65
|
expect(People.find(4)).to eq @person3
|
41
66
|
expect(People.find(5)).to eq @person4
|
42
67
|
end
|
43
68
|
|
44
|
-
it
|
45
|
-
(People).
|
69
|
+
it '[DEPRECATED] responds to .find_by' do
|
70
|
+
expect(People).to respond_to(:find_by)
|
46
71
|
end
|
47
72
|
|
48
|
-
it
|
73
|
+
it '[DEPRECATED] .find_by returns correctly' do
|
49
74
|
expect(People.find_by(nickname: 'Pebbles')).to eq @person3
|
50
75
|
expect(People.find_by(nickname: 'Pebbles', last: 'Smith')).to eq @person2
|
51
76
|
end
|
52
77
|
|
53
|
-
it
|
78
|
+
it '.find_by returns an nil if there are no results' do
|
54
79
|
expect(People.find_by(nickname: 'Beastmode')).to eq nil
|
55
80
|
end
|
56
81
|
|
57
|
-
|
58
|
-
|
59
|
-
(People).should respond_to(:where)
|
82
|
+
it 'responds to .where' do
|
83
|
+
expect(People).to respond_to(:where)
|
60
84
|
end
|
61
85
|
|
62
|
-
it
|
86
|
+
it '.where returns correctly' do
|
63
87
|
expect(People.where(nickname: 'Pebbles')).to eq [@person2, @person3]
|
64
|
-
expect(People.where(nickname: 'Pebbles', last: 'Radiation'))
|
88
|
+
expect(People.where(nickname: 'Pebbles', last: 'Radiation'))
|
89
|
+
.to eq [@person3]
|
65
90
|
end
|
66
91
|
|
67
|
-
it
|
92
|
+
it '.where returns an empty array if there are no results' do
|
68
93
|
expect(People.where(nickname: 'Beastmode')).to eq []
|
69
94
|
end
|
70
95
|
|
71
|
-
it
|
72
|
-
(People).
|
96
|
+
it '[DEPRECATED] responds to .find_all_by' do
|
97
|
+
expect(People).to respond_to(:find_all_by)
|
73
98
|
end
|
74
99
|
|
75
|
-
it
|
76
|
-
expect(People.find_all_by(nickname: 'Pebbles'))
|
77
|
-
|
100
|
+
it '[DEPRECATED] .find_all_by returns correctly' do
|
101
|
+
expect(People.find_all_by(nickname: 'Pebbles'))
|
102
|
+
.to eq [@person2, @person3]
|
103
|
+
|
104
|
+
expect(People.find_all_by(nickname: 'Pebbles', last: 'Radiation'))
|
105
|
+
.to eq [@person3]
|
78
106
|
end
|
79
107
|
|
80
|
-
it
|
81
|
-
(People).
|
108
|
+
it 'responds to .each' do
|
109
|
+
expect(People).to respond_to(:each)
|
82
110
|
end
|
83
111
|
|
84
|
-
it
|
112
|
+
it '.each line yields' do
|
85
113
|
@output = []
|
86
|
-
People.each{ |person| @output << person.first }
|
87
|
-
expect(@output).to eq [
|
114
|
+
People.each { |person| @output << person.first }
|
115
|
+
expect(@output).to eq %w[Mark Longinus Johnny Charlie]
|
88
116
|
end
|
89
117
|
end
|
118
|
+
|
90
119
|
context 'instance methods' do
|
91
|
-
it
|
92
|
-
(People.new).
|
120
|
+
it 'responds to .first as defined in the csv' do
|
121
|
+
expect(People.new).to respond_to(:first)
|
93
122
|
end
|
94
123
|
|
95
|
-
it
|
96
|
-
(People.new).
|
124
|
+
it 'responds to .last as defined in the csv' do
|
125
|
+
expect(People.new).to respond_to(:last)
|
97
126
|
end
|
98
127
|
|
99
|
-
it
|
100
|
-
(People.new).
|
128
|
+
it 'responds to .nickname as defined in the csv' do
|
129
|
+
expect(People.new).to respond_to(:nickname)
|
101
130
|
end
|
102
131
|
end
|
103
132
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_find
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Platt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -16,15 +16,29 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.9.0
|
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
|
-
version:
|
27
|
-
|
26
|
+
version: 3.9.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.82.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.82.0
|
41
|
+
description: csv_find quickly finds rows and creates an instance with that data
|
28
42
|
email: mplatt@mrkplt.com
|
29
43
|
executables: []
|
30
44
|
extensions: []
|
@@ -44,17 +58,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
58
|
requirements:
|
45
59
|
- - ">="
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: 2.
|
61
|
+
version: 2.5.0
|
48
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
63
|
requirements:
|
50
64
|
- - ">="
|
51
65
|
- !ruby/object:Gem::Version
|
52
66
|
version: '0'
|
53
67
|
requirements: []
|
54
|
-
|
55
|
-
rubygems_version: 2.2.2
|
68
|
+
rubygems_version: 3.1.2
|
56
69
|
signing_key:
|
57
70
|
specification_version: 4
|
58
|
-
summary:
|
71
|
+
summary: This uses some command line tools to take CSV's apart and put them together
|
72
|
+
to quickly find specific rows in a csv.
|
59
73
|
test_files:
|
60
74
|
- spec/csv_find_spec.rb
|