csv_class_maker 1.0.3 → 1.1.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 +4 -4
- data/lib/csv_class_maker.rb +1 -0
- data/lib/csv_class_maker/csv_class_maker.rb +6 -8
- data/spec/csv_class_maker_spec.rb +22 -4
- metadata +17 -4
- data/lib/csv_class_maker/csv_find.rb +0 -142
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a5b445825a19aad125c8fb3d531aad9001e2f74
|
4
|
+
data.tar.gz: 859a008a90053f81a0f5954c5c57df10afcfb360
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e25215d990e0b030b77ef755dfd1b486e1c918de3f30abc86d6dd934311f5f494721d48c1ac8dc9756cf5d27385a652f832b43810c12c51c86b9938af17b572f
|
7
|
+
data.tar.gz: d7690eaec949f6308ac85dca716d9106b891091f4e00f5d5b55baf49352900a4bc671ea95559a2324424c9b1c696e581190ea08a9939ca04a41b2481dac7bd0e
|
data/lib/csv_class_maker.rb
CHANGED
@@ -6,17 +6,15 @@
|
|
6
6
|
module CsvClassMaker
|
7
7
|
require 'csv'
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
)
|
9
|
+
VERSION = {
|
10
|
+
major: '1',
|
11
|
+
minor: '1',
|
12
|
+
patch: '0'
|
13
|
+
}.values.join('.')
|
15
14
|
|
15
|
+
def self.generate_class(class_name, file_name, options = {})
|
16
16
|
Object.const_set(class_name, Class.new do
|
17
|
-
require 'csv_class_maker/csv_find'
|
18
17
|
include CsvFind
|
19
|
-
|
20
18
|
csv_file(file_name, options)
|
21
19
|
end
|
22
20
|
)
|
@@ -52,20 +52,38 @@ describe Object do
|
|
52
52
|
expect(People.find(5)).to eq @person4
|
53
53
|
end
|
54
54
|
|
55
|
-
it "responds to .find_by" do
|
55
|
+
it "[DEPRECATED] responds to .find_by" do
|
56
56
|
(People).should respond_to(:find_by)
|
57
57
|
end
|
58
58
|
|
59
|
-
it ".find_by returns correctly" do
|
59
|
+
it "[DEPRECATED] .find_by returns correctly" do
|
60
60
|
expect(People.find_by(nickname: 'Pebbles')).to eq @person3
|
61
61
|
expect(People.find_by(nickname: 'Pebbles', last: 'Smith')).to eq @person2
|
62
62
|
end
|
63
63
|
|
64
|
-
it "
|
64
|
+
it ".find_by returns an nil if there are no results" do
|
65
|
+
expect(People.find_by(nickname: 'Beastmode')).to eq nil
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
it "responds to .where" do
|
70
|
+
(People).should respond_to(:where)
|
71
|
+
end
|
72
|
+
|
73
|
+
it ".where returns correctly" do
|
74
|
+
expect(People.where(nickname: 'Pebbles')).to eq [@person2, @person3]
|
75
|
+
expect(People.where(nickname: 'Pebbles', last: 'Radiation')).to eq [@person3]
|
76
|
+
end
|
77
|
+
|
78
|
+
it ".where returns an empty array if there are no results" do
|
79
|
+
expect(People.where(nickname: 'Beastmode')).to eq []
|
80
|
+
end
|
81
|
+
|
82
|
+
it "[DEPRECATED] responds to .find_all_by" do
|
65
83
|
(People).should respond_to(:find_all_by)
|
66
84
|
end
|
67
85
|
|
68
|
-
it ".find_all_by returns correctly" do
|
86
|
+
it "[DEPRECATED] .find_all_by returns correctly" do
|
69
87
|
expect(People.find_all_by(nickname: 'Pebbles')).to eq [@person2, @person3]
|
70
88
|
expect(People.find_all_by(nickname: 'Pebbles', last: 'Radiation')).to eq [@person3]
|
71
89
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
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.1.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: 2015-05-
|
11
|
+
date: 2015-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: csv_find
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.1
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -33,7 +47,6 @@ extra_rdoc_files: []
|
|
33
47
|
files:
|
34
48
|
- lib/csv_class_maker.rb
|
35
49
|
- lib/csv_class_maker/csv_class_maker.rb
|
36
|
-
- lib/csv_class_maker/csv_find.rb
|
37
50
|
- spec/csv_class_maker_spec.rb
|
38
51
|
homepage: http://mrkplt.com
|
39
52
|
licenses:
|
@@ -55,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
68
|
version: '0'
|
56
69
|
requirements: []
|
57
70
|
rubyforge_project:
|
58
|
-
rubygems_version: 2.
|
71
|
+
rubygems_version: 2.2.2
|
59
72
|
signing_key:
|
60
73
|
specification_version: 4
|
61
74
|
summary: "'Object oriented CSV reading'"
|
@@ -1,142 +0,0 @@
|
|
1
|
-
module CsvClassMaker::CsvFind
|
2
|
-
def self.included(base)
|
3
|
-
base.send :extend, ClassMethods
|
4
|
-
base.send :prepend, InstanceMethods
|
5
|
-
end
|
6
|
-
|
7
|
-
module InstanceMethods
|
8
|
-
attr_accessor :line_number
|
9
|
-
|
10
|
-
def initialize(hash = nil)
|
11
|
-
if hash
|
12
|
-
hash.each { |k,v| send("#{k}=".to_sym, v) }
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def ==(other_instance)
|
17
|
-
instance_variables.map do |instance_variable|
|
18
|
-
instance_variable_get(instance_variable) ==
|
19
|
-
other_instance.instance_variable_get(instance_variable)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
attr_reader :hash
|
26
|
-
end
|
27
|
-
|
28
|
-
module ClassMethods
|
29
|
-
|
30
|
-
attr_reader :headers, :file, :file_options,
|
31
|
-
:first_line, :middle_line, :last_line
|
32
|
-
|
33
|
-
def csv_file(file_name, options = {})
|
34
|
-
@file_options = options
|
35
|
-
@file = CSV.new(File.open(file_name, 'r'), @file_options)
|
36
|
-
@first_line = 2
|
37
|
-
@last_line = `wc -l #{file_name}`.split(' ').first.to_i
|
38
|
-
@middle_line = (@last_line/2) + 1
|
39
|
-
@line_number = nil
|
40
|
-
extract_headers(file_name, options)
|
41
|
-
define_accessors
|
42
|
-
end
|
43
|
-
|
44
|
-
def define_accessors
|
45
|
-
headers.each do |header|
|
46
|
-
self.send(:attr_accessor, header)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def all
|
51
|
-
rewind
|
52
|
-
file.map { |row| build_instance(row, file.lineno) }
|
53
|
-
end
|
54
|
-
|
55
|
-
def find_by(key_val_pair)
|
56
|
-
row = search(key_val_pair).last
|
57
|
-
build_instance(row, row[:line_number])
|
58
|
-
end
|
59
|
-
|
60
|
-
def find_all_by(key_val_pair)
|
61
|
-
search(key_val_pair).map { |row| build_instance(row, row[:line_number]) }
|
62
|
-
end
|
63
|
-
|
64
|
-
def find(line_number)
|
65
|
-
row = if (first_line..middle_line).include?(line_number)
|
66
|
-
front_find(line_number, file.path)
|
67
|
-
else
|
68
|
-
back_find(line_number, file.path)
|
69
|
-
end
|
70
|
-
|
71
|
-
row.nil? ? row : build_instance(row, line_number)
|
72
|
-
end
|
73
|
-
|
74
|
-
def first
|
75
|
-
rewind
|
76
|
-
build_instance(file.first, first_line)
|
77
|
-
end
|
78
|
-
|
79
|
-
def last
|
80
|
-
command = `head -n 1 #{file.path} && tail -n 1 #{file.path}`
|
81
|
-
last_row = CSV.new(command, file_options).first
|
82
|
-
build_instance(last_row, last_line)
|
83
|
-
end
|
84
|
-
|
85
|
-
def each
|
86
|
-
rewind
|
87
|
-
(first_line..last_line).each do |line_number|
|
88
|
-
yield find(line_number) if block_given?
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
private
|
93
|
-
|
94
|
-
def extract_headers(file_name, options)
|
95
|
-
csv_file = File.open(file_name,'r')
|
96
|
-
|
97
|
-
@headers ||= CSV.new(csv_file, options).first.map do |headers, values|
|
98
|
-
headers
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def build_instance(row, line)
|
103
|
-
new_instance = new
|
104
|
-
row.each { |key, value| new_instance.send("#{key}=".to_sym, value) }
|
105
|
-
new_instance.line_number = line
|
106
|
-
|
107
|
-
new_instance
|
108
|
-
end
|
109
|
-
|
110
|
-
def rewind
|
111
|
-
file.rewind
|
112
|
-
end
|
113
|
-
|
114
|
-
def search(key_val_pair)
|
115
|
-
rewind
|
116
|
-
@results = file
|
117
|
-
@pairs = key_val_pair
|
118
|
-
|
119
|
-
@pairs.each { |pair| @results = dig(pair, @results) }
|
120
|
-
|
121
|
-
@results
|
122
|
-
end
|
123
|
-
|
124
|
-
def dig(hash_pair, rows)
|
125
|
-
rows.select do |row|
|
126
|
-
if row[hash_pair.first] == hash_pair.last
|
127
|
-
$. != last_line ? row.push(line_number: $.) : row
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def front_find(line_number, file_path)
|
133
|
-
command = `head -n 1 #{file_path} && head -n #{line_number} #{file_path} | tail -n 1`
|
134
|
-
CSV.new(command, file_options).first
|
135
|
-
end
|
136
|
-
|
137
|
-
def back_find(line_number, file_path)
|
138
|
-
command = `head -n 1 #{file_path} && tail -n #{(last_line + 1) - line_number} #{file_path} | head -n 1`
|
139
|
-
CSV.new(command, file_options).first
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|