csv_class_maker 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 948e6040124f37290122b5405595b00889cfb579
4
+ data.tar.gz: 48ed1b40e8b145f0d58f5d32857e3ffb7b40ee92
5
+ SHA512:
6
+ metadata.gz: 109692828cc8410b9d10d3fd7f7f28b2a8e7972267e5cb987d7b43522aa7ce711254bcfa734a8639460cfd96188f3fbb23a7d3a213850ddf9eaec6c3363f31d2
7
+ data.tar.gz: 82c30a2788e9c90af06da059f006d82c0c0a75f9b3e86c4dc84adf04c9fe6e16389a7aeeaa692882534008c6e59a6220ff7fa9634cd20a5d47c036ac93a0bb55
@@ -48,12 +48,10 @@ module CsvClassMaker
48
48
  private
49
49
 
50
50
  def self.extract_headers(file_name, options)
51
- @csv_headers = []
52
51
  csv_file = File.open(file_name,'r')
53
52
 
54
- CSV.new(csv_file, options).first.each do |headers, values|
55
- @csv_headers << headers
53
+ @csv_headers = CSV.new(csv_file, options).first.map do |headers, values|
54
+ headers
56
55
  end
57
- @csv_headers
58
56
  end
59
57
  end
@@ -1,10 +1,7 @@
1
1
  module CsvClassMaker::CsvFind
2
-
3
2
  def all
4
3
  rewind
5
- object_array = []
6
- file.each {|row| object_array << build_instance(row, file.lineno) }
7
- object_array
4
+ file.map { |row| build_instance(row, file.lineno) }
8
5
  end
9
6
 
10
7
  def find_by(key_val_pair)
@@ -13,40 +10,34 @@ module CsvClassMaker::CsvFind
13
10
  end
14
11
 
15
12
  def find_all_by(key_val_pair)
16
- rows = search(key_val_pair)
17
- output_objects = []
18
- rows.each { |row|
19
- output_objects << build_instance(row, row[:line_number])
20
- }
21
- output_objects
13
+ search(key_val_pair).map { |row| build_instance(row, row[:line_number]) }
22
14
  end
23
15
 
24
16
  def find(line_number)
25
- if (first_line..last_line).include? line_number
26
- row = front_find(line_number, file.path)
17
+ row = if (first_line..last_line).include? line_number
18
+ front_find(line_number, file.path)
27
19
  elsif (middle_line..last_line).include? line_number
28
- row = back_find(line_number, file.path)
29
- else
30
- row = nil
20
+ back_find(line_number, file.path)
31
21
  end
32
- row.nil? ? nil : build_instance(row, line_number)
22
+
23
+ row.nil? ? row : build_instance(row, line_number)
33
24
  end
34
25
 
35
26
  def first
36
27
  rewind
37
- build_instance file.first, first_line
28
+ build_instance(file.first, first_line)
38
29
  end
39
30
 
40
31
  def last
41
32
  command = `head -n 1 #{file.path} && tail -n 1 #{file.path}`
42
33
  last_row = CSV.new(command, file_options).first
43
- build_instance last_row, last_line
34
+ build_instance(last_row, last_line)
44
35
  end
45
36
 
46
37
  def each
47
38
  rewind
48
39
  (first_line..last_line).each do |line_number|
49
- yield find line_number
40
+ yield find(line_number)
50
41
  end
51
42
  end
52
43
 
@@ -54,7 +45,7 @@ module CsvClassMaker::CsvFind
54
45
 
55
46
  def build_instance(row, line)
56
47
  new_instance = self.new
57
- row.each { |key, value| new_instance.send "#{key}=".to_sym, value }
48
+ row.each { |key, value| new_instance.send("#{key}=".to_sym, value) }
58
49
  new_instance.line_number = line
59
50
  return new_instance
60
51
  end
@@ -68,29 +59,17 @@ module CsvClassMaker::CsvFind
68
59
  @results = file
69
60
  @pairs = key_val_pair
70
61
 
71
- @pairs.each { |pair|
72
- @results = dig(pair, @results)
73
- }
62
+ @pairs.each { |pair| @results = dig(pair, @results) }
74
63
 
75
64
  @results
76
65
  end
77
66
 
78
67
  def dig(hash_pair, rows)
79
- @key = hash_pair.first
80
- @value = hash_pair.last
81
- @accumulator = []
82
-
83
- rows.each { |row|
84
- if row[@key] == @value
85
- if $. != last_line
86
- @accumulator << row.push(line_number: $.) if $. != last_line
87
- else
88
- @accumulator << row
89
- end
68
+ rows.map do |row|
69
+ if row[hash_pair.first] == hash_pair.last
70
+ $. != last_line ? row.push(line_number: $.) : row
90
71
  end
91
- }
92
-
93
- @accumulator
72
+ end.reject(&:nil?)
94
73
  end
95
74
 
96
75
  def front_find(line_number, file_path)
@@ -101,5 +80,4 @@ module CsvClassMaker::CsvFind
101
80
  command = `head -n 1 #{file_path} && tail -n #{last_line - line_number} #{file_path} | head -n 1`
102
81
  CSV.new(command, file_options).first
103
82
  end
104
-
105
83
  end
@@ -62,7 +62,7 @@ describe Object do
62
62
  end
63
63
  it ".each line yields" do
64
64
  @output = []
65
- People.each{|person| @output << person.first}
65
+ People.each{ |person| @output << person.first }
66
66
  expect(@output).to eq ['Mark', 'Longinus', 'Johnny', 'Charlie']
67
67
  end
68
68
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_class_maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
5
- prerelease:
4
+ version: 0.3.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mark Platt
@@ -14,7 +13,6 @@ dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,13 +20,12 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
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'''
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'''
32
29
  email: mplatt@mrkplt.com
33
30
  executables: []
34
31
  extensions: []
@@ -41,27 +38,26 @@ files:
41
38
  homepage: http://mrkplt.com
42
39
  licenses:
43
40
  - MIT
41
+ metadata: {}
44
42
  post_install_message:
45
43
  rdoc_options: []
46
44
  require_paths:
47
45
  - lib
48
46
  required_ruby_version: !ruby/object:Gem::Requirement
49
- none: false
50
47
  requirements:
51
- - - ! '>='
48
+ - - '>='
52
49
  - !ruby/object:Gem::Version
53
50
  version: 1.9.3
54
51
  required_rubygems_version: !ruby/object:Gem::Requirement
55
- none: false
56
52
  requirements:
57
- - - ! '>='
53
+ - - '>='
58
54
  - !ruby/object:Gem::Version
59
55
  version: '0'
60
56
  requirements: []
61
57
  rubyforge_project:
62
- rubygems_version: 1.8.23
58
+ rubygems_version: 2.0.14
63
59
  signing_key:
64
- specification_version: 3
65
- summary: ! '''Object oriented CSV reading'''
60
+ specification_version: 4
61
+ summary: '''Object oriented CSV reading'''
66
62
  test_files:
67
63
  - spec/csv_class_maker_spec.rb