csvh 0.5.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5528c84de3fd89a37fc7cb37c4ef7c26988aa9a3
4
+ data.tar.gz: b5f9b26106d34f09565700704fe34ef9484ded97
5
+ SHA512:
6
+ metadata.gz: 93a1a100cf34d6d20c4acc048830550110a43bc59eb3e5702fa1d39169fbf47a0cf77533ced3757c8c91874b80be6b2d4274acf8fb0cefb97b83e41d0cb06db6
7
+ data.tar.gz: ff2397bde3839c9c10b27417dfe3b56f14f31301734d907073ad981aec895118bf91f3e9e776d78163f46ab431cb6e0f4a3760b9ea245996938fcc013b80fe4e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csvh.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Steve Jorgensen
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,77 @@
1
+ # CSVH
2
+
3
+ The csvh gem provides convenient augmentations to handling of CSV
4
+ files with header rows in Ruby.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'csvh'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install csvh
21
+
22
+ ## Usage
23
+
24
+ This gem provides a `CSVH::Reader` class that lazily reads from
25
+ CSV-formatted data that has a header row. Allows accessing
26
+ headers before reading any subsequent data rows and/or when no
27
+ additional data rows are present in the data.
28
+
29
+ The `CSVH::Reader` class is primarily intended to be used in one
30
+ of the following ways:
31
+
32
+ # Read from a file, and close the file automatically.
33
+ CSVH::Reader.from_file 'the-path-to/my-data.csv' do |reader|
34
+ # reader.headers is an array of header strings.
35
+ puts "Headers: " + reader.headers.inspect
36
+
37
+ reader.each do |row|
38
+ # row is a standard Ruby CSV::Row object.
39
+ puts row.to_h.inspect
40
+ end
41
+ end
42
+
43
+ # Read from an IO stream.
44
+ reader = CSVH::Reader.from_string_or_io(an_io_stream)
45
+
46
+ # reader.headers is an array of header strings.
47
+ puts "Headers: " + reader.headers.inspect
48
+
49
+ reader.each do |row|
50
+ # row is a standard Ruby CSV::Row object.
51
+ puts row.to_h.inspect
52
+ end
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install
57
+ dependencies. Then, run `rake spec` to run the tests. You can
58
+ also run `bin/console` for an interactive prompt that will allow
59
+ you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec
62
+ rake install`. To release a new version, update the version
63
+ number in `version.rb`, and then run `bundle exec rake release`,
64
+ which will create a git tag for the version, push git commits and
65
+ tags, and push the `.gem` file to
66
+ [rubygems.org](https://rubygems.org).
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at
71
+ https://github.com/[USERNAME]/csvh.
72
+
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the
77
+ [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "csvh"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/csvh.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csvh/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csvh"
8
+ spec.version = CSVH::VERSION
9
+ spec.authors = ["Steve Jorgensen"]
10
+ spec.email = ["stevej@stevej.name"]
11
+
12
+ spec.summary = "Convenient augmentations to handling of CSV files with header rows in Ruby"
13
+ spec.homepage = "https://github.com/stevecj/csvh"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,146 @@
1
+ require 'delegate'
2
+
3
+ module CSVH
4
+
5
+ # Sequantially and lazily reads from CSV-formatted data that
6
+ # has a header row. Allows accessing headers before reading
7
+ # any subsequent data rows and/or when no additional data rows
8
+ # are present in the data.
9
+ class Reader
10
+ extend Forwardable
11
+
12
+ DEFAULT_CSV_OPTS = {
13
+ headers: :first_row,
14
+ return_headers: true
15
+ }.freeze
16
+
17
+ class << self
18
+
19
+ # When called without a block argument, returns an open
20
+ # reader for data from the file at the given file_path.
21
+ #
22
+ # When called with a block argument, passes an open reader
23
+ # for data from the file to the given block, closes the
24
+ # reader (and its underlying file IO channel) before
25
+ # returning, and then returns the value that was returned
26
+ # by the block.
27
+ #
28
+ # By default, the underlying CSV object is initialized
29
+ # with default options for data with a header row and to
30
+ # return the header row. Any oadditional options you supply
31
+ # will be added to those defaults or override them.
32
+ #
33
+ # @param file_path [String] the path of the file to read.
34
+ # @param opts options for `CSV.new`.
35
+ # @yieldparam [Reader] the new reader.
36
+ # @return [Reader,object]
37
+ # the new reader or the value returned from the given
38
+ # block.
39
+ def from_file(file_path, **opts)
40
+ opts = default_csv_opts.merge(opts)
41
+ io = File.open(file_path, 'r')
42
+ csv = CSV.new(io, **opts)
43
+ instance = new(csv)
44
+
45
+ if block_given?
46
+ begin
47
+ yield instance
48
+ ensure
49
+ instance.close unless instance.closed?
50
+ end
51
+ else
52
+ instance
53
+ end
54
+ end
55
+
56
+ # Returns an open reader for data from given string or
57
+ # readable IO stream.
58
+ #
59
+ # @param data [String, IO] the source of the data to read.
60
+ # @param opts options for `CSV.new`.
61
+ # @return [Reader] the new reader.
62
+ def from_string_or_io(data, **opts)
63
+ opts = default_csv_opts.merge(opts)
64
+ csv = CSV.new(data, **opts)
65
+ new(csv)
66
+ end
67
+
68
+ alias foreach from_file
69
+ alias parse from_string_or_io
70
+
71
+ private
72
+
73
+ def default_csv_opts
74
+ DEFAULT_CSV_OPTS
75
+ end
76
+ end
77
+
78
+ # Returns a new reader based on the given CSV object. The CSV
79
+ # object must be configured to return a header row (a
80
+ # `CSV::ROW` that returns true from its `#header?` method
81
+ # as its first item. The header item must also not have been
82
+ # read yet.
83
+ # @param csv [CSV] A Ruby `::CSV` object.
84
+ def initialize(csv)
85
+ unless csv.return_headers?
86
+ raise \
87
+ InappropreateCsvInstanceError,
88
+ "%{self.class} requires a CSV instance that returns headers." \
89
+ " It needs to have been initialized with non-false/nil values" \
90
+ " for :headers and :return_headers options."
91
+ end
92
+ @csv = csv
93
+ end
94
+
95
+ # @return [Reader] the target of the method call.
96
+ def to_csvh_reader
97
+ self
98
+ end
99
+
100
+ # Returns the list of column header values from the CSV data.
101
+ #
102
+ # If any rows have already been read, then the result is
103
+ # immediately returned, having been recorded when the header
104
+ # row was initially encountered.
105
+ #
106
+ # If no rows have been read yet, then the first row is read
107
+ # from the data in order to return the result.
108
+ #
109
+ # @return [Array<String>] the column header names.
110
+ def headers
111
+ @headers ||= begin
112
+ row = @csv.readline
113
+ unless row.header_row?
114
+ raise \
115
+ CsvPrematurelyShiftedError,
116
+ "the header row was prematurely read from the underlying CSV object."
117
+ end
118
+ row.headers
119
+ end
120
+ end
121
+
122
+ def_delegators \
123
+ :@csv,
124
+ :close,
125
+ :closed?
126
+
127
+ # When given a block, yields each data row of the data source
128
+ # in turn as `CSV::Row` instances. When called without a
129
+ # block, returns an Enumerator over those rows.
130
+ #
131
+ # Does not yield the header row, however, the headers are
132
+ # available via the #headers method of the reader or the row.
133
+ #
134
+ # @yieldparam [CSV::Row]
135
+ def each
136
+ headers
137
+ if block_given?
138
+ @csv.each { |row| yield row }
139
+ else
140
+ @csv.each
141
+ end
142
+ end
143
+
144
+ end
145
+
146
+ end
@@ -0,0 +1,3 @@
1
+ module CSVH
2
+ VERSION = "0.5.0"
3
+ end
data/lib/csvh.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'csv'
2
+
3
+ require "csvh/version"
4
+ require "csvh/reader"
5
+
6
+ module CSVH
7
+ class InappropreateCsvInstanceError < ArgumentError ; end
8
+ class CsvPrematurelyShiftedError < StandardError ; end
9
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csvh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Jorgensen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-03 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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
+ description:
56
+ email:
57
+ - stevej@stevej.name
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - csvh.gemspec
72
+ - lib/csvh.rb
73
+ - lib/csvh/reader.rb
74
+ - lib/csvh/version.rb
75
+ homepage: https://github.com/stevecj/csvh
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Convenient augmentations to handling of CSV files with header rows in Ruby
99
+ test_files: []