recite_csv 0.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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +23 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +126 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/recite_csv/header/definition.rb +74 -0
- data/lib/recite_csv/header.rb +8 -0
- data/lib/recite_csv/module.rb +4 -0
- data/lib/recite_csv/reader/builder.rb +28 -0
- data/lib/recite_csv/reader/core.rb +28 -0
- data/lib/recite_csv/reader/dsl.rb +11 -0
- data/lib/recite_csv/reader.rb +8 -0
- data/lib/recite_csv/row/base.rb +17 -0
- data/lib/recite_csv/row.rb +8 -0
- data/lib/recite_csv/version.rb +5 -0
- data/lib/recite_csv.rb +5 -0
- data/recite_csv.gemspec +29 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8492257e8882c0950b3bd4fe923537cbb0b94822
|
4
|
+
data.tar.gz: d5d2eca503ee999109e44dedb9e0296fcaa9c91d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 17a59c273f7487aac65ad7a0e121e50fb9b830fe7653b434d54bf84e4422411b900dc5a3dfc2b90b8dacdd20667a35ee299e0cc2ca80b359b785797548681f3f
|
7
|
+
data.tar.gz: 3e3dd79a098ece2b2fe80fdd96d399b41551b226923aae8edba873db7c76d91c4b2d08f46b4e17d4c0cb030d3d9bc56c66b375789b0793243d535c04c02d4177
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.4
|
3
|
+
|
4
|
+
Style/AsciiComments:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Style/Documentation:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Style/RedundantSelf:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/StringLiterals:
|
14
|
+
Enabled: true
|
15
|
+
EnforcedStyle: double_quotes
|
16
|
+
|
17
|
+
Layout/EndOfLine:
|
18
|
+
Enabled: true
|
19
|
+
EnforcedStyle: lf
|
20
|
+
|
21
|
+
Metrics/BlockLength:
|
22
|
+
Exclude:
|
23
|
+
- spec/**/*
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Yuji Hanamura
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# ReciteCSV
|
2
|
+
|
3
|
+
ReciteCSV helps to build a class for CSV reader.
|
4
|
+
|
5
|
+
[](https://travis-ci.org/yujideveloper/recite_csv)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'recite_csv'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install recite_csv
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
The following is a example csv file.
|
26
|
+
|
27
|
+
``` csv
|
28
|
+
COL1,COL2
|
29
|
+
VALUE1,VALUE2
|
30
|
+
VALUE3,VALUE4
|
31
|
+
```
|
32
|
+
|
33
|
+
Specify header definition using hash object.
|
34
|
+
|
35
|
+
``` ruby
|
36
|
+
class Foo
|
37
|
+
include ReciteCSV::Reader::Builder.new(col1: "COL1", col2: "COL2")
|
38
|
+
end
|
39
|
+
|
40
|
+
Foo.new("./example.csv").each do |row|
|
41
|
+
row.class # => Foo::Row
|
42
|
+
row.col1
|
43
|
+
row.col2
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Specify header definition using array object.
|
48
|
+
|
49
|
+
``` ruby
|
50
|
+
class Bar
|
51
|
+
include ReciteCSV::Reader::Builder.new(%w[col1 col2])
|
52
|
+
end
|
53
|
+
|
54
|
+
Bar.new("./example.csv").each do |row|
|
55
|
+
row.class # => Bar::Row
|
56
|
+
row.col1
|
57
|
+
row.col2
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
Define custom methods of row object.
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
class Baz
|
65
|
+
include(
|
66
|
+
ReciteCSV::Reader::Builder.new(col1: "COL1", col2: "COL2") do
|
67
|
+
# define methods of Row class
|
68
|
+
def col1
|
69
|
+
"override #{super}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def custom_method
|
73
|
+
# do somethings..
|
74
|
+
end
|
75
|
+
end
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
Baz.new("./example.csv").each do |row|
|
80
|
+
row.class # => Baz::Row
|
81
|
+
row.col1
|
82
|
+
row.col2
|
83
|
+
row.custom_method
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Define custom methods of row object using `row_methods`.
|
88
|
+
|
89
|
+
``` ruby
|
90
|
+
class Quz
|
91
|
+
include ReciteCSV::Reader::Builder.new(col1: "COL1", col2: "COL2")
|
92
|
+
|
93
|
+
row_methods do
|
94
|
+
def col1
|
95
|
+
"override #{super}"
|
96
|
+
end
|
97
|
+
|
98
|
+
def custom_method
|
99
|
+
# do somethings..
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
Quz.new("./example.csv").each do |row|
|
105
|
+
row.class # => Quz::Row
|
106
|
+
row.col1
|
107
|
+
row.col2
|
108
|
+
row.custom_method
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
## Development
|
113
|
+
|
114
|
+
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.
|
115
|
+
|
116
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yujideveloper/recite_csv.
|
121
|
+
|
122
|
+
|
123
|
+
## License
|
124
|
+
|
125
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
126
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
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
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ReciteCSV
|
4
|
+
module Header
|
5
|
+
module Definition
|
6
|
+
class Base
|
7
|
+
attr_reader :raw_definition
|
8
|
+
|
9
|
+
def initialize(raw_definition)
|
10
|
+
@raw_definition = raw_definition
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_column_methods_module
|
14
|
+
self.class::ColumnMethodsBuilder.new(self.raw_definition)
|
15
|
+
end
|
16
|
+
|
17
|
+
def default_csv_options
|
18
|
+
{}.freeze
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.new(*)
|
22
|
+
if self == Base
|
23
|
+
raise ::NotImplementedError, "#{self} is an abstract class and cannot be instantiated."
|
24
|
+
end
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Hash < Base
|
30
|
+
DEFAULT_CSV_OPIONS = { headers: :first_row }.freeze
|
31
|
+
|
32
|
+
class ColumnMethodsBuilder < ::Module
|
33
|
+
def initialize(raw_definition)
|
34
|
+
raw_definition.each do |method_name, header_name|
|
35
|
+
define_method method_name do
|
36
|
+
self[header_name]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def default_csv_options
|
43
|
+
DEFAULT_CSV_OPIONS
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Array < Base
|
48
|
+
class ColumnMethodsBuilder < ::Module
|
49
|
+
def initialize(raw_definition)
|
50
|
+
raw_definition.each.with_index do |name, idx|
|
51
|
+
next if name.nil? || name.empty?
|
52
|
+
define_method name do
|
53
|
+
self[idx]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
DEFINITIONS = {
|
61
|
+
::Hash => Hash,
|
62
|
+
::Array => Array
|
63
|
+
}.freeze
|
64
|
+
|
65
|
+
module_function
|
66
|
+
|
67
|
+
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
|
70
|
+
definition_class.new(header_definition)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "recite_csv/header"
|
4
|
+
require "recite_csv/row"
|
5
|
+
require "recite_csv/reader/core"
|
6
|
+
require "recite_csv/reader/dsl"
|
7
|
+
|
8
|
+
module ReciteCSV
|
9
|
+
module Reader
|
10
|
+
class Builder < ::Module
|
11
|
+
def initialize(header_definition, &block)
|
12
|
+
definition = Header::Definition.dispatch(header_definition)
|
13
|
+
@definition = definition
|
14
|
+
@row_class = ::Class.new(Row::Base) do |klass|
|
15
|
+
klass.include definition.build_column_methods_module
|
16
|
+
klass.class_exec(&block) if block_given?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def included(class_or_module)
|
21
|
+
class_or_module.include Core
|
22
|
+
class_or_module.const_set(:Row, @row_class)
|
23
|
+
class_or_module.const_set(:DEFAULT_CSV_OPIONS, @definition.default_csv_options)
|
24
|
+
class_or_module.extend DSL
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "csv"
|
4
|
+
|
5
|
+
module ReciteCSV
|
6
|
+
module Reader
|
7
|
+
module Core
|
8
|
+
include Enumerable
|
9
|
+
|
10
|
+
attr_reader :file, :csv_options
|
11
|
+
|
12
|
+
def initialize(file, csv_options = {})
|
13
|
+
@file = file
|
14
|
+
@csv_options = (csv_options || {}).merge(self.class::DEFAULT_CSV_OPIONS)
|
15
|
+
end
|
16
|
+
|
17
|
+
def each
|
18
|
+
if block_given?
|
19
|
+
::CSV.foreach(self.file, self.csv_options) do |raw_row|
|
20
|
+
yield self.class::Row.new(raw_row)
|
21
|
+
end
|
22
|
+
else
|
23
|
+
self.to_enum
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/recite_csv.rb
ADDED
data/recite_csv.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "recite_csv/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "recite_csv"
|
9
|
+
spec.version = ReciteCSV::VERSION
|
10
|
+
spec.authors = ["Yuji Hanamura"]
|
11
|
+
spec.email = ["yuji.developer@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = "ReciteCSV helps to build a class for CSV reader."
|
14
|
+
spec.description = "ReciteCSV helps to build a class for CSV reader."
|
15
|
+
spec.homepage = "https://github.com/yujideveloper/recite_csv"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "rubocop"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: recite_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuji Hanamura
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ReciteCSV helps to build a class for CSV reader.
|
70
|
+
email:
|
71
|
+
- yuji.developer@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/recite_csv.rb
|
88
|
+
- lib/recite_csv/header.rb
|
89
|
+
- lib/recite_csv/header/definition.rb
|
90
|
+
- lib/recite_csv/module.rb
|
91
|
+
- lib/recite_csv/reader.rb
|
92
|
+
- lib/recite_csv/reader/builder.rb
|
93
|
+
- lib/recite_csv/reader/core.rb
|
94
|
+
- lib/recite_csv/reader/dsl.rb
|
95
|
+
- lib/recite_csv/row.rb
|
96
|
+
- lib/recite_csv/row/base.rb
|
97
|
+
- lib/recite_csv/version.rb
|
98
|
+
- recite_csv.gemspec
|
99
|
+
homepage: https://github.com/yujideveloper/recite_csv
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.6.13
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: ReciteCSV helps to build a class for CSV reader.
|
123
|
+
test_files: []
|