recite_csv 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d78da3c0debc0e3962fa02b0344c7df3980d2275
4
- data.tar.gz: a7373574d333c472c031f040749bf9089f1eeed0
2
+ SHA256:
3
+ metadata.gz: 7a4537328a0288a5ab2128f56327322eee068031d5afaab03706734bb179041e
4
+ data.tar.gz: 0b5a4fce48b66f8e93225952c042d21aba89e90fcad4144ffc1e466c7e1c352c
5
5
  SHA512:
6
- metadata.gz: 5f6a549d6c8f69a0c086bf3faae9f8943a6046ba85b81eecfb904ea30abdd0819fdddf4b4992ad25d330f287cb4ee25cc71d572cfcc048fdc1fca43598b142f7
7
- data.tar.gz: d0de9224da6de39c8ce014a558acb281627f3bed7df122e34727f541d8487caf2a09c20411f9869fea3906374e0a94ccd3292c4e8e7781dba605a4a44d419b8a
6
+ metadata.gz: 2b7f88122616980eacfc4a859aeb6fff0c3e801b96594318992653142b630e597fa41eed2926650898b5020432173219e721c2e10cbbba71a8816e65f4d98924
7
+ data.tar.gz: a85827717185419d59cb08a23fa22f847a8ec9e0a3049b410e0972d22beb1deaa67133dd24d32675e304ef49ecbf45e9dc889e43a3d62d03f1e68f0245c089da
data/.gitignore CHANGED
File without changes
data/.rspec CHANGED
File without changes
File without changes
@@ -1,10 +1,10 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.2.8
5
- - 2.3.5
6
- - 2.4.2
4
+ - 2.2.9
5
+ - 2.3.6
6
+ - 2.4.3
7
+ - 2.5.0
7
8
  before_install: gem install bundler -v 1.14.6
8
9
  script:
9
- - bundle install
10
10
  - bundle exec rake spec
@@ -1,5 +1,19 @@
1
1
  ## Unreleased
2
2
 
3
+
4
+ ## 0.1.2 (2018-02-23)
5
+
6
+ ### Changes
7
+
8
+ * Change `Row::Base` to an abstract class
9
+ * Add support Ruby v2.5
10
+ * Add file options to `Reader::Core#each`
11
+
12
+ ### Misc
13
+
14
+ * Change to use `Hash#each_pair` instead of `Hash#each`
15
+
16
+
3
17
  ## 0.1.1 (2017-12-03)
4
18
 
5
19
  ### Changes
data/Gemfile CHANGED
File without changes
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 Yuji Hanamura
3
+ Copyright (c) 2017-2018 Yuji Hanamura
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -4,6 +4,7 @@ ReciteCSV helps to build a class for CSV reader.
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/recite_csv.svg)](https://badge.fury.io/rb/recite_csv)
6
6
  [![Build Status](https://travis-ci.org/yujideveloper/recite_csv.svg?branch=master)](https://travis-ci.org/yujideveloper/recite_csv)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/eb04cab6d55b0c7a1f7f/maintainability)](https://codeclimate.com/github/yujideveloper/recite_csv/maintainability)
7
8
 
8
9
  ## Installation
9
10
 
@@ -88,7 +89,7 @@ end
88
89
  Define custom methods of row object using `row_methods`.
89
90
 
90
91
  ``` ruby
91
- class Quz
92
+ class Qux
92
93
  include ReciteCSV::Reader::Builder.new(col1: "COL1", col2: "COL2")
93
94
 
94
95
  row_methods do
@@ -102,14 +103,39 @@ class Quz
102
103
  end
103
104
  end
104
105
 
105
- Quz.new("./example.csv").each do |row|
106
- row.class # => Quz::Row
106
+ Qux.new("./example.csv").each do |row|
107
+ row.class # => Qux::Row
107
108
  row.col1
108
109
  row.col2
109
110
  row.custom_method
110
111
  end
111
112
  ```
112
113
 
114
+ Specify file mode and encoding.
115
+
116
+ ``` ruby
117
+ class Quux
118
+ include ReciteCSV::Reader::Builder.new(col1: "COL1", col2: "COL2")
119
+ end
120
+
121
+ Quux.new("./example.csv", file_options: "rb:UTF-8").each do |row|
122
+ # do something
123
+ end
124
+ ```
125
+
126
+ Convert encoding.
127
+
128
+ ``` ruby
129
+ class Corge
130
+ include ReciteCSV::Reader::Builder.new(col1: "COL1", col2: "COL2")
131
+ end
132
+
133
+ Corge.new("./example.csv", file_options: ["rb:Shift_JIS:UTF-8", invalid: :replace, undef: :replace]).each do |row|
134
+ # do something
135
+ end
136
+ ```
137
+
138
+
113
139
  ## Development
114
140
 
115
141
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
File without changes
@@ -3,13 +3,6 @@
3
3
 
4
4
  require "bundler/setup"
5
5
  require "recite_csv"
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
6
  require "pry"
7
+
15
8
  Pry.start
data/bin/setup CHANGED
File without changes
File without changes
File without changes
@@ -20,7 +20,8 @@ module ReciteCSV
20
20
 
21
21
  def self.new(*)
22
22
  if self == Base
23
- raise ::NotImplementedError, "#{self} is an abstract class and cannot be instantiated."
23
+ raise ::NotImplementedError,
24
+ "#{self} is an abstract class and cannot be instantiated."
24
25
  end
25
26
  super
26
27
  end
@@ -31,7 +32,7 @@ module ReciteCSV
31
32
 
32
33
  class ColumnMethodsBuilder < ::Module
33
34
  def initialize(raw_definition)
34
- raw_definition.each do |method_name, header_name|
35
+ raw_definition.each_pair do |method_name, header_name|
35
36
  define_method method_name do
36
37
  self[header_name]
37
38
  end
@@ -65,8 +66,12 @@ module ReciteCSV
65
66
  module_function
66
67
 
67
68
  def dispatch(header_definition)
68
- _, definition_class = DEFINITIONS.find { |klass, _| header_definition.is_a? klass }
69
- raise ::ArgumentError, "Unexpected header definition type" unless definition_class
69
+ _, definition_class = DEFINITIONS.find do |klass, _|
70
+ header_definition.is_a? klass
71
+ end
72
+ unless definition_class
73
+ raise ::ArgumentError, "Unexpected header definition type"
74
+ end
70
75
  definition_class.new(header_definition)
71
76
  end
72
77
  end
File without changes
File without changes
@@ -20,7 +20,8 @@ module ReciteCSV
20
20
  def included(class_or_module)
21
21
  class_or_module.include Core
22
22
  class_or_module.const_set(:Row, @row_class)
23
- class_or_module.const_set(:DEFAULT_CSV_OPTIONS, @definition.default_csv_options)
23
+ class_or_module.const_set(:DEFAULT_CSV_OPTIONS,
24
+ @definition.default_csv_options)
24
25
  class_or_module.extend DSL
25
26
  end
26
27
  end
@@ -7,22 +7,43 @@ module ReciteCSV
7
7
  module Core
8
8
  include Enumerable
9
9
 
10
- attr_reader :file, :csv_options
10
+ attr_reader :file, :file_options, :csv_options
11
11
 
12
- def initialize(file, csv_options = {})
12
+ def initialize(file, options = {})
13
13
  @file = file
14
- @csv_options = (csv_options || {}).merge(self.class::DEFAULT_CSV_OPTIONS)
14
+ @file_options = options.delete(:file_options) || {}
15
+ @csv_options =
16
+ (options || {}).merge(self.class::DEFAULT_CSV_OPTIONS)
15
17
  end
16
18
 
17
- def each
19
+ def each(&block)
18
20
  if block_given?
19
- ::CSV.foreach(self.file, self.csv_options) do |raw_row|
20
- yield self.class::Row.new(raw_row)
21
- end
21
+ _each(&block)
22
22
  else
23
23
  self.to_enum
24
24
  end
25
25
  end
26
+
27
+ private
28
+
29
+ def _each(&block)
30
+ f = if self.file.is_a?(::String)
31
+ ::File.open(self.file, *Array(self.file_options))
32
+ else
33
+ self.file
34
+ end
35
+ begin
36
+ _foreach(f, &block)
37
+ ensure
38
+ f.close if self.file.is_a?(::String)
39
+ end
40
+ end
41
+
42
+ def _foreach(f)
43
+ ::CSV.new(f, self.csv_options).each do |raw_row|
44
+ yield self.class::Row.new(raw_row)
45
+ end
46
+ end
26
47
  end
27
48
  end
28
49
  end
File without changes
File without changes
@@ -12,6 +12,14 @@ module ReciteCSV
12
12
  def [](key)
13
13
  self._raw_data[key]
14
14
  end
15
+
16
+ def self.new(*)
17
+ if self == Base
18
+ raise ::NotImplementedError,
19
+ "#{self} is an abstract class and cannot be instantiated."
20
+ end
21
+ super
22
+ end
15
23
  end
16
24
  end
17
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReciteCSV
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: recite_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Hanamura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-03 00:00:00.000000000 Z
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  version: '0'
131
131
  requirements: []
132
132
  rubyforge_project:
133
- rubygems_version: 2.6.13
133
+ rubygems_version: 2.7.3
134
134
  signing_key:
135
135
  specification_version: 4
136
136
  summary: ReciteCSV helps to build a class for CSV reader.