simple_csv_reader 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85db951fc28b9b5dce4660386480f9f2c2bcf445031559c5240d10bde37baec0
4
- data.tar.gz: f192937261995849810277db919abfd15698723bf8cf5e51be66c1dadfc4ba75
3
+ metadata.gz: 5f9db42a81223ede030684309a036e82938f8870d9be4a1fb1797ee5dcf04fd1
4
+ data.tar.gz: fd7a39b8eed7006fc6136d47953f7921bd037bf2d7e2c98ad1714964a3b1e7e4
5
5
  SHA512:
6
- metadata.gz: 93ead315b6c1848242c057717007fa7082914c7267f80bdd0b734b3fbf7b5b19be7e915acfe8b084271a85dd031a55dab99330638dd406b5acc9868c1bf7a395
7
- data.tar.gz: c31551dd5a57e944d49b93716c6a2a06b0b206fad6b8f49cfdb55be9fd8e810b28bd9d870e749281171e7ae375440cedcf6fda0a4c6c0200ab1d1bcea86cffbb
6
+ metadata.gz: b21b87edc91175bd8083cdd0d159f474477b50101668b76c012f2de3059c3796db491bcd37113df02675f40f0e12326d5f82ed47fef5e1495df87e5c91180f2a
7
+ data.tar.gz: 3b1c17f10f7916ba33caf253cd4c393b2bf28ba0b0d44318e351ec4c5bded1cada38e6329fe66861c7e51a12e7071e76b834ae7935c03bf12feb08244333db76
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_csv_reader (0.3.0)
4
+ simple_csv_reader (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -5,26 +5,54 @@ require 'nkf'
5
5
 
6
6
  module SimpleCsvReader
7
7
  class Reader
8
- def initialize(path)
8
+ def initialize(path, headers, **options)
9
9
  @path = path
10
+ @headers = headers
11
+ @options = options
10
12
  end
11
13
 
12
- def read(headers, **options, &block)
14
+ def each(&block)
15
+ return enum_for(:each) unless block_given?
16
+
13
17
  # NOTE: The first line is a header
14
- csv(headers, **options.slice(:converters)).each.with_index(2) do |row, row_number|
18
+ csv.each.with_index(2) do |row, row_number|
15
19
  hash = row.to_h
16
20
  next if hash.compact.empty? # NOTE: blank row
17
21
 
18
- filtered_hash = options[:include_unspecified_headers] ? hash : hash.slice(*headers.keys)
22
+ filtered_hash = @options[:include_unspecified_headers] ? hash : hash.slice(*@headers.keys)
19
23
  yield(filtered_hash, row_number: row_number)
20
24
  end
21
25
  end
22
26
 
27
+ def self.default_converters
28
+ instance_variable_defined?(:@default_converters) ? @default_converters : :numeric
29
+ end
30
+
31
+ def self.default_converters=(converters)
32
+ @default_converters = converters
33
+ end
34
+
35
+ def self.default_nil_value
36
+ instance_variable_defined?(:@default_nil_value) ? @default_nil_value : nil
37
+ end
38
+
39
+ def self.default_nil_value=(value)
40
+ @default_nil_value = value
41
+ end
42
+
23
43
  private
24
44
 
25
- def csv(headers, converters: :numeric)
26
- header_converter = -> { headers.invert.fetch(_1, _1) }
27
- CSV.parse(content, headers: :first_row, header_converters: header_converter, converters: converters)
45
+ def csv
46
+ header_converter = -> { @headers.invert.fetch(_1, _1) }
47
+ CSV.parse(content, headers: :first_row, header_converters: header_converter, converters: converters, nil_value: nil_value)
48
+ end
49
+
50
+ def converters
51
+ @options.fetch(:converters) { self.class.default_converters }
52
+ end
53
+
54
+ def nil_value
55
+ @options.fetch(:nil_value) { self.class.default_nil_value }
28
56
  end
29
57
 
30
58
  def content
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleCsvReader
4
- VERSION = '0.3.0'
4
+ VERSION = '0.5.0'
5
5
  end
@@ -5,7 +5,7 @@ require_relative "simple_csv_reader/version"
5
5
 
6
6
  module SimpleCsvReader
7
7
  def read(path, headers, **options, &block)
8
- Reader.new(path).read(headers, **options, &block)
8
+ Reader.new(path, headers, **options).each(&block)
9
9
  end
10
10
 
11
11
  module_function :read
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_csv_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aki77
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-26 00:00:00.000000000 Z
11
+ date: 2022-12-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple CSV reader.
14
14
  email:
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubygems_version: 3.3.7
59
+ rubygems_version: 3.3.26
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: A simple CSV reader.