darlingtonia 0.1.0 → 0.1.1
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 +4 -4
- data/CHANGELOG.md +14 -0
- data/lib/darlingtonia/parser.rb +6 -4
- data/lib/darlingtonia/validator.rb +13 -0
- data/lib/darlingtonia/validators/csv_format_validator.rb +17 -1
- data/lib/darlingtonia/version.rb +1 -1
- data/spec/darlingtonia/csv_format_validator_spec.rb +32 -1
- data/spec/darlingtonia/csv_parser_spec.rb +1 -0
- data/spec/darlingtonia/parser_spec.rb +7 -2
- data/spec/fixtures/bad_example.csv +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f8a94f2f8efae78fd25e95e947a1290759dc552
|
4
|
+
data.tar.gz: b2c3f7bdbb8f1047620b1d2a12fda977c76c5242
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c33bd4225faab6259e72e4cd74cbb24bca5a7c8592fe61c2ea290bfd03922f2a3b80cead1c3db1f89fc033fee04868130459f841e2a2669a174ecc28f76a532a
|
7
|
+
data.tar.gz: ad7b5de36a1dafdfde876647713fc582218a3f9e7eca49385375751572785e0200209ca502425769d3cb88adcff134e1c56a8876a77c44432dc13bceedadf4f9
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
0.1.1 - Fri Jan 12, 2018
|
2
|
+
------------------------
|
3
|
+
|
4
|
+
Bugfix for nested class inheritance.
|
5
|
+
|
6
|
+
- Fix CI by requiring manually 'tempfile' in specs that use it
|
7
|
+
- Add a CSV format validator at `CsvFormatValidator`
|
8
|
+
- Fix `Parser` subclass registration and reorder parser class `#match`
|
9
|
+
priority.
|
10
|
+
|
11
|
+
0.1.0 - Thu Jan 11, 2018
|
12
|
+
------------------------
|
13
|
+
|
14
|
+
Inital release.
|
data/lib/darlingtonia/parser.rb
CHANGED
@@ -9,8 +9,10 @@ module Darlingtonia
|
|
9
9
|
#
|
10
10
|
# Parser.for(file: file).records
|
11
11
|
#
|
12
|
+
# rubocop:disable Style/ClassVars
|
12
13
|
class Parser
|
13
|
-
|
14
|
+
DEFAULT_VALIDATORS = [].freeze
|
15
|
+
@@subclasses = [] # @private
|
14
16
|
|
15
17
|
##
|
16
18
|
# @!attribute [rw] file
|
@@ -42,7 +44,7 @@ module Darlingtonia
|
|
42
44
|
# @raise [NoParserError]
|
43
45
|
def for(file:)
|
44
46
|
klass =
|
45
|
-
|
47
|
+
@@subclasses.find { |k| k.match?(file: file) } ||
|
46
48
|
raise(NoParserError)
|
47
49
|
|
48
50
|
klass.new(file: file)
|
@@ -58,7 +60,7 @@ module Darlingtonia
|
|
58
60
|
##
|
59
61
|
# @private Register a new class when inherited
|
60
62
|
def inherited(subclass)
|
61
|
-
|
63
|
+
@@subclasses.unshift subclass
|
62
64
|
super
|
63
65
|
end
|
64
66
|
end
|
@@ -99,5 +101,5 @@ module Darlingtonia
|
|
99
101
|
|
100
102
|
class NoParserError < TypeError; end
|
101
103
|
class ValidationError < RuntimeError; end
|
102
|
-
end
|
104
|
+
end # rubocop:enable Style/ClassVars
|
103
105
|
end
|
@@ -3,6 +3,19 @@
|
|
3
3
|
module Darlingtonia
|
4
4
|
##
|
5
5
|
# @abstract A null validator; always returns an empty error collection
|
6
|
+
#
|
7
|
+
# @example validating a parser
|
8
|
+
# validator = MyValidator.new
|
9
|
+
# validator.validate(parser: myParser)
|
10
|
+
#
|
11
|
+
# @example validating an invalid parser
|
12
|
+
# validator = MyValidator.new
|
13
|
+
# validator.validate(parser: invalidParser)
|
14
|
+
# # => Error<#... validator: MyValidator,
|
15
|
+
# name: 'An Error Name',
|
16
|
+
# description: '...'
|
17
|
+
# lineno: 37>
|
18
|
+
#
|
6
19
|
class Validator
|
7
20
|
Error = Struct.new(:validator, :name, :description, :lineno)
|
8
21
|
|
@@ -2,7 +2,23 @@
|
|
2
2
|
|
3
3
|
module Darlingtonia
|
4
4
|
##
|
5
|
-
# A validator for correctly formatted CSV
|
5
|
+
# A validator for correctly formatted CSV.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# parser = Parser.new(file: File.open('path/to/my.csv'))
|
9
|
+
#
|
10
|
+
# CsvFormatValidator.new.validate(parser: parser)
|
11
|
+
#
|
12
|
+
# @see http://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV/MalformedCSVError.html
|
6
13
|
class CsvFormatValidator < Validator
|
14
|
+
##
|
15
|
+
# @see Validator#validate
|
16
|
+
def validate(parser:, **)
|
17
|
+
return [] if CSV.parse(parser.file.read)
|
18
|
+
rescue CSV::MalformedCSVError => e
|
19
|
+
[Error.new(self.class, e.class, e.message)]
|
20
|
+
ensure
|
21
|
+
parser.file.rewind
|
22
|
+
end
|
7
23
|
end
|
8
24
|
end
|
data/lib/darlingtonia/version.rb
CHANGED
@@ -3,5 +3,36 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Darlingtonia::CsvFormatValidator do
|
6
|
-
|
6
|
+
subject(:validator) { described_class.new }
|
7
|
+
let(:invalid_parser) { Darlingtonia::CsvParser.new(file: invalid_file) }
|
8
|
+
let(:invalid_file) { File.open('spec/fixtures/bad_example.csv') }
|
9
|
+
|
10
|
+
it_behaves_like 'a Darlingtonia::Validator' do
|
11
|
+
let(:valid_parser) { Darlingtonia::CsvParser.new(file: valid_file) }
|
12
|
+
let(:valid_file) { File.open('spec/fixtures/example.csv') }
|
13
|
+
end
|
14
|
+
|
15
|
+
define :a_validator_error do
|
16
|
+
match do |error|
|
17
|
+
return false unless error.respond_to?(:validator)
|
18
|
+
|
19
|
+
if fields
|
20
|
+
return false if fields[:validator] && error.validator != fields[:validator]
|
21
|
+
return false if fields[:name] && error.name != fields[:name]
|
22
|
+
end
|
23
|
+
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
chain :with, :fields
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#validate' do
|
31
|
+
it 'returns a Validator::Error' do
|
32
|
+
expect(validator.validate(parser: invalid_parser))
|
33
|
+
.to contain_exactly a_validator_error
|
34
|
+
.with(validator: validator.class,
|
35
|
+
name: CSV::MalformedCSVError)
|
36
|
+
end
|
37
|
+
end
|
7
38
|
end
|
@@ -24,12 +24,17 @@ describe Darlingtonia::Parser do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
27
|
+
|
28
|
+
class NestedParser < FakeParser; end
|
27
29
|
end
|
28
30
|
|
29
|
-
after(:context)
|
31
|
+
after(:context) do
|
32
|
+
Object.send(:remove_const, :FakeParser)
|
33
|
+
Object.send(:remove_const, :NestedParser)
|
34
|
+
end
|
30
35
|
|
31
36
|
it 'returns an importer instance' do
|
32
|
-
expect(described_class.for(file: file)).to be_a
|
37
|
+
expect(described_class.for(file: file)).to be_a NestedParser
|
33
38
|
end
|
34
39
|
end
|
35
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: darlingtonia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Johnson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-01-
|
12
|
+
date: 2018-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: active-fedora
|
@@ -138,6 +138,7 @@ files:
|
|
138
138
|
- ".gitignore"
|
139
139
|
- ".rubocop.yml"
|
140
140
|
- ".travis.yml"
|
141
|
+
- CHANGELOG.md
|
141
142
|
- Gemfile
|
142
143
|
- README.md
|
143
144
|
- Rakefile
|
@@ -161,6 +162,7 @@ files:
|
|
161
162
|
- spec/darlingtonia/parser_spec.rb
|
162
163
|
- spec/darlingtonia/validator_spec.rb
|
163
164
|
- spec/darlingtonia/version_spec.rb
|
165
|
+
- spec/fixtures/bad_example.csv
|
164
166
|
- spec/fixtures/example.csv
|
165
167
|
- spec/integration/import_csv_spec.rb
|
166
168
|
- spec/spec_helper.rb
|