simple_csv_reader 0.2.0 → 0.4.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: 464241d327186f58aa17c95eaff8b93b0ca53d73bdbf42b97802109548942d39
4
- data.tar.gz: 94144bdfcff87e98a177c4a6563a562c9a11cac0d37b1ba0e700beebe74db631
3
+ metadata.gz: 1257ac24bb535dd8a802d2fdb507b68adccc66bcb7c042e9b9d6e30252685883
4
+ data.tar.gz: e4c36b6597ccd5293a0a244fe8c4f07f65ccce638de14355b649c9e2aeda5190
5
5
  SHA512:
6
- metadata.gz: e78ff8c6d8e0c21417461a83b4238850f4ab9ffffc953563f9afb75066374d453d6e9b5b7e15a0a55563a35dbf8d90fa23a5ac678e68099588f6888c77f09fb6
7
- data.tar.gz: da60653a2af7b3b6ce3d131ebabbe2059b69b8f7b39e592a4697f4099ae528a38d776da1239b5354c8f8d9068b132997f47b9c085c1e82289e041021a7c5bce0
6
+ metadata.gz: ac55d646bad8ddd96d697778bbb6da9273e2be5af2cf1d820f1ca0d31f0f4ff2287956500a106de453ac2e18d97732143fe757883af86518d8d16856c41437ea
7
+ data.tar.gz: cfc44981d4bf0c91cdd9c0bbfed8e31d19c99793c11ab669f3315250abb8fcbdee57f12bc1f8a36428568fbb853a8737713e66951d0d7a2fc19da7f96e1c817c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_csv_reader (0.2.0)
4
+ simple_csv_reader (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -5,26 +5,42 @@ 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).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
+
23
35
  private
24
36
 
25
- def csv(headers)
26
- header_converter = -> { headers.invert.fetch(_1, _1) }
27
- CSV.parse(content, headers: :first_row, header_converters: header_converter, converters: :integer)
37
+ def csv
38
+ header_converter = -> { @headers.invert.fetch(_1, _1) }
39
+ CSV.parse(content, headers: :first_row, header_converters: header_converter, converters: converters)
40
+ end
41
+
42
+ def converters
43
+ @options.fetch(:converters) { self.class.default_converters }
28
44
  end
29
45
 
30
46
  def content
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleCsvReader
4
- VERSION = '0.2.0'
4
+ VERSION = '0.4.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.2.0
4
+ version: 0.4.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-07-19 00:00:00.000000000 Z
11
+ date: 2022-12-04 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.