csv2hash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +79 -0
- data/Guardfile +8 -0
- data/LICENSE +7 -0
- data/README.md +73 -0
- data/Rakefile +29 -0
- data/bin/load_rvm +1 -0
- data/csv2hash.gemspec +24 -0
- data/lib/csv2hash/definition/mapping.rb +7 -0
- data/lib/csv2hash/definition.rb +44 -0
- data/lib/csv2hash/parser.rb +24 -0
- data/lib/csv2hash/validator.rb +34 -0
- data/lib/csv2hash/version.rb +3 -0
- data/lib/csv2hash.rb +24 -0
- data/spec/csv2hash/definition_spec.rb +79 -0
- data/spec/csv2hash/parser_spec.rb +39 -0
- data/spec/csv2hash/validator_spec.rb +47 -0
- data/spec/csv2hash_spec.rb +3 -0
- data/spec/spec_helper.rb +10 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f8f8219aff79abbf8d98c623d02d6e95929af47
|
4
|
+
data.tar.gz: 428d41f0b1a1c8490bb19aa78d0dbf4dbe61f487
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1b68da6acde6bace6121a0db5dfb4470827d6129f17f776a83157870c0f8f5977ad72bbead178782694d4ceeb0ae54d369a8152b2a6b11482bc4935e96c0db45
|
7
|
+
data.tar.gz: d492de227d7970cbbed83e984ae531d7fa4e5761a345320bbde6c58e93daa4aede871af4a5e7cb7bb0e27618c7d4286fe28b660eb5feb77410757a31c434c14c
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --fail-fast
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
csv2hash (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
celluloid (0.15.2)
|
10
|
+
timers (~> 1.1.0)
|
11
|
+
coderay (1.1.0)
|
12
|
+
coveralls (0.7.0)
|
13
|
+
multi_json (~> 1.3)
|
14
|
+
rest-client
|
15
|
+
simplecov (>= 0.7)
|
16
|
+
term-ansicolor
|
17
|
+
thor
|
18
|
+
diff-lcs (1.2.5)
|
19
|
+
docile (1.1.1)
|
20
|
+
ffi (1.9.3)
|
21
|
+
formatador (0.2.4)
|
22
|
+
guard (2.2.4)
|
23
|
+
formatador (>= 0.2.4)
|
24
|
+
listen (~> 2.1)
|
25
|
+
lumberjack (~> 1.0)
|
26
|
+
pry (>= 0.9.12)
|
27
|
+
thor (>= 0.18.1)
|
28
|
+
guard-rspec (4.0.4)
|
29
|
+
guard (>= 2.1.1)
|
30
|
+
rspec (~> 2.14)
|
31
|
+
listen (2.2.0)
|
32
|
+
celluloid (>= 0.15.2)
|
33
|
+
rb-fsevent (>= 0.9.3)
|
34
|
+
rb-inotify (>= 0.9)
|
35
|
+
lumberjack (1.0.4)
|
36
|
+
method_source (0.8.2)
|
37
|
+
mime-types (2.0)
|
38
|
+
multi_json (1.8.2)
|
39
|
+
pry (0.9.12.4)
|
40
|
+
coderay (~> 1.0)
|
41
|
+
method_source (~> 0.8)
|
42
|
+
slop (~> 3.4)
|
43
|
+
rake (10.1.0)
|
44
|
+
rb-fsevent (0.9.3)
|
45
|
+
rb-inotify (0.9.2)
|
46
|
+
ffi (>= 0.5.0)
|
47
|
+
rest-client (1.6.7)
|
48
|
+
mime-types (>= 1.16)
|
49
|
+
rspec (2.14.1)
|
50
|
+
rspec-core (~> 2.14.0)
|
51
|
+
rspec-expectations (~> 2.14.0)
|
52
|
+
rspec-mocks (~> 2.14.0)
|
53
|
+
rspec-core (2.14.7)
|
54
|
+
rspec-expectations (2.14.4)
|
55
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
56
|
+
rspec-mocks (2.14.4)
|
57
|
+
simplecov (0.8.2)
|
58
|
+
docile (~> 1.1.0)
|
59
|
+
multi_json
|
60
|
+
simplecov-html (~> 0.8.0)
|
61
|
+
simplecov-html (0.8.0)
|
62
|
+
slop (3.4.7)
|
63
|
+
term-ansicolor (1.2.2)
|
64
|
+
tins (~> 0.8)
|
65
|
+
thor (0.18.1)
|
66
|
+
timers (1.1.0)
|
67
|
+
tins (0.13.1)
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
ruby
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
bundler (~> 1.3)
|
74
|
+
coveralls
|
75
|
+
csv2hash!
|
76
|
+
guard
|
77
|
+
guard-rspec
|
78
|
+
rake
|
79
|
+
rspec
|
data/Guardfile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec, cmd: 'rspec --color --fail-fast' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2013 Joël AZÉMAR https://github.com/joel, Joël AZÉMAR joel.azemar@gmail.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Csv2Hash
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/joel/csv2hash.png)](https://codeclimate.com/github/joel/csv2hash)
|
4
|
+
|
5
|
+
[![Dependency Status](https://gemnasium.com/joel/csv2hash.png)](https://gemnasium.com/joel/csv2hash)
|
6
|
+
|
7
|
+
[![Build Status](https://travis-ci.org/joel/csv2hash.png?branch=master)](https://travis-ci.org/joel/csv2hash) (Travis CI)
|
8
|
+
|
9
|
+
[![Coverage Status](https://coveralls.io/repos/joel/csv2hash/badge.png)](https://coveralls.io/r/joel/csv2hash)
|
10
|
+
|
11
|
+
|
12
|
+
It's DSL for valided and map CSV to Ruby Hash
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'csv2hash'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install csv2hash
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
You should be declare an definition for you CSV, for each cells you should define what you expect.
|
31
|
+
|
32
|
+
Example :
|
33
|
+
|
34
|
+
You want first cell parsed should be string with values are 'yes' or 'no' you must fill follow rule :
|
35
|
+
|
36
|
+
{ name: 'aswering', type: 'string', values: ['yes', 'no'], position: [0,0] }
|
37
|
+
|
38
|
+
All keys as default value, so you can just define this rule :
|
39
|
+
|
40
|
+
{ name: 'aswering', values: ['yes', 'no'], position: [0,0] }
|
41
|
+
|
42
|
+
You can define message, default is 'undefined :key on :position'
|
43
|
+
|
44
|
+
{ name: 'aswering', values: ['yes', 'no'], position: [0,0], message: 'this value is not supported' }
|
45
|
+
|
46
|
+
if you insert key on you message they will be substituted
|
47
|
+
|
48
|
+
{ ..., message: 'value of :name is not supported, please you one of :values' }
|
49
|
+
|
50
|
+
produce ':
|
51
|
+
value of aswering is not supported, please you one of [yes, no]'
|
52
|
+
|
53
|
+
#### Default values
|
54
|
+
|
55
|
+
* message: 'undefined :key on :position'
|
56
|
+
* mappable: true
|
57
|
+
* type: 'string'
|
58
|
+
* values: nil
|
59
|
+
* nested: nil
|
60
|
+
* allow_blank: false
|
61
|
+
* position: nil
|
62
|
+
* maptype: 'cell'
|
63
|
+
|
64
|
+
### Limitations
|
65
|
+
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
rescue Exception => e
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rake'
|
8
|
+
require 'rspec'
|
9
|
+
require 'rspec/core/rake_task'
|
10
|
+
require 'csv2hash'
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
13
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
14
|
+
end
|
15
|
+
|
16
|
+
RSpec::Core::RakeTask.new('spec:progress') do |spec|
|
17
|
+
spec.rspec_opts = %w(--format progress)
|
18
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rdoc/task'
|
22
|
+
Rake::RDocTask.new do |rdoc|
|
23
|
+
rdoc.rdoc_dir = 'rdoc'
|
24
|
+
rdoc.title = 'csv2hash #{csv2hash::VERSION}'
|
25
|
+
rdoc.rdoc_files.include('README*')
|
26
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
27
|
+
end
|
28
|
+
|
29
|
+
task default: :spec
|
data/bin/load_rvm
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use `cat .ruby-version`@`cat .ruby-gemset` --create
|
data/csv2hash.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
|
3
|
+
spec.name = 'csv2hash'
|
4
|
+
spec.version = '0.0.1'
|
5
|
+
spec.date = '2013-11-26'
|
6
|
+
spec.summary = %q{Mapping CSV to Ruby Hash}
|
7
|
+
spec.description = %q{DSL for CSV Ruby Hash mapping}
|
8
|
+
spec.authors = ['Joel AZEMAR']
|
9
|
+
spec.email = 'joel.azemar@gmail.com'
|
10
|
+
spec.files = ['lib/csv2hash.rb']
|
11
|
+
spec.homepage = 'https://github.com/joel/csv2hash'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
20
|
+
spec.add_development_dependency 'rake'
|
21
|
+
spec.add_development_dependency 'rspec'
|
22
|
+
|
23
|
+
spec.required_ruby_version = '~> 2.0'
|
24
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class Definition
|
2
|
+
|
3
|
+
MAPPING = 'mapping'
|
4
|
+
COLLECTION = 'collection'
|
5
|
+
|
6
|
+
TYPES = [Definition::MAPPING, Definition::COLLECTION]
|
7
|
+
|
8
|
+
attr_accessor :type, :rules
|
9
|
+
|
10
|
+
def validate!
|
11
|
+
unless TYPES.include?(type)
|
12
|
+
raise "not suitable type, please use '#{Definition::MAPPING}' or '#{Definition::COLLECTION}'"
|
13
|
+
end
|
14
|
+
raise 'rules must be an Array of rules' unless rules.class == Array
|
15
|
+
end
|
16
|
+
|
17
|
+
def default!
|
18
|
+
rules.each do |rule|
|
19
|
+
default_position rule
|
20
|
+
rule.merge! message: 'undefined :key on :position' unless rule.has_key? :message
|
21
|
+
rule.merge! mappable: true unless rule.has_key? :mappable
|
22
|
+
rule.merge! type: 'string' unless rule.has_key? :type
|
23
|
+
rule.merge! values: nil unless rule.has_key? :values
|
24
|
+
rule.merge! nested: nil unless rule.has_key? :nested
|
25
|
+
rule.merge! allow_blank: false unless rule.has_key? :allow_blank
|
26
|
+
rule.merge! position: nil unless rule.has_key? :position
|
27
|
+
rule.merge! maptype: 'cell' unless rule.has_key? :maptype
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def default_position rule
|
34
|
+
case type
|
35
|
+
when Definition::MAPPING
|
36
|
+
x, y = rule.fetch(:position, ['undefined', 'undefined'])
|
37
|
+
rule.merge! key: "key_#{x}_#{y}" unless rule.has_key? :key
|
38
|
+
when Definition::COLLECTION
|
39
|
+
y = rule.fetch :position
|
40
|
+
rule.merge! key: "key_#{y}" unless rule.has_key? :key
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Parser
|
2
|
+
|
3
|
+
def fill!
|
4
|
+
@data = {}.tap do |data_computed|
|
5
|
+
data_computed[:data] ||= []
|
6
|
+
data_computed[:data] << {}.tap do |data_parsed|
|
7
|
+
|
8
|
+
definition.rules.each do |rule|
|
9
|
+
if rule.fetch :mappable
|
10
|
+
x, y = rule.fetch :position
|
11
|
+
if (nested = rule.fetch :nested)
|
12
|
+
data_parsed[nested] ||= {}
|
13
|
+
data_parsed[nested][rule.fetch(:key)] = data_source[x][y]
|
14
|
+
else
|
15
|
+
data_parsed[rule.fetch(:key)] = data_source[x][y]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Validator
|
2
|
+
|
3
|
+
def validate_data!
|
4
|
+
definition.rules.each do |rule|
|
5
|
+
if (rule.fetch(:maptype) == 'cell')
|
6
|
+
x, y = rule.fetch :position
|
7
|
+
validate_rule x, y, rule
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def validate_rule x, y, rule
|
15
|
+
begin
|
16
|
+
raise unless data_source[x]
|
17
|
+
unless rule.fetch :allow_blank
|
18
|
+
raise unless data_source[x][y]
|
19
|
+
end
|
20
|
+
if (values = rule.fetch :values)
|
21
|
+
raise unless values.include?(data_source[x][y])
|
22
|
+
end
|
23
|
+
rescue => e
|
24
|
+
raise message(rule)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def message rule
|
29
|
+
rule.fetch(:message).tap do |msg|
|
30
|
+
rule.each { |key, value| msg.gsub! ":#{key.to_s}", value.to_s }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/csv2hash.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'csv2hash/version'
|
2
|
+
require 'csv2hash/definition'
|
3
|
+
require 'csv2hash/definition/mapping'
|
4
|
+
require 'csv2hash/validator'
|
5
|
+
require 'csv2hash/parser'
|
6
|
+
|
7
|
+
class Csv2hash
|
8
|
+
include Validator
|
9
|
+
include Parser
|
10
|
+
|
11
|
+
attr_accessor :definition, :data_source, :data
|
12
|
+
|
13
|
+
def initialize definition, data_source
|
14
|
+
@definition, @data_source = definition, data_source
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse
|
18
|
+
definition.validate!
|
19
|
+
definition.default!
|
20
|
+
validate_data!
|
21
|
+
fill!
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Definition do
|
4
|
+
|
5
|
+
context 'regular context' do
|
6
|
+
subject do
|
7
|
+
Definition.new.tap do |definition|
|
8
|
+
definition.type = Definition::MAPPING
|
9
|
+
definition.rules = begin
|
10
|
+
[
|
11
|
+
{ position: [0,0], key: 'name' }
|
12
|
+
]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'variable should be assigned' do
|
18
|
+
subject.type.should eql Definition::MAPPING
|
19
|
+
subject.rules.should eql [ { position: [0,0], key: 'name' } ]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#validate!' do
|
24
|
+
context 'rules failling validation' do
|
25
|
+
subject do
|
26
|
+
Definition.new.tap do |definition|
|
27
|
+
definition.type = 'unsuitable_type'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
it 'should throw exception' do
|
31
|
+
expect {
|
32
|
+
subject.validate!
|
33
|
+
}.to raise_error "not suitable type, please use '#{Definition::MAPPING}' or '#{Definition::COLLECTION}'"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
context 'rules failling validation' do
|
37
|
+
subject do
|
38
|
+
Definition.new.tap do |definition|
|
39
|
+
definition.type = Definition::MAPPING
|
40
|
+
definition.rules = 'rules'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
it 'should throw exception' do
|
44
|
+
expect { subject.validate! }.to raise_error 'rules must be an Array of rules'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#default!' do
|
50
|
+
subject do
|
51
|
+
Definition.new.tap do |definition|
|
52
|
+
definition.type = Definition::MAPPING
|
53
|
+
definition.rules = begin
|
54
|
+
[
|
55
|
+
{ position: [0,0], key: 'name' }
|
56
|
+
]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
before { subject.default! }
|
62
|
+
|
63
|
+
it 'missing key must be filled' do
|
64
|
+
subject.rules.should eql([{ position: [0, 0],
|
65
|
+
key: 'name',
|
66
|
+
message: 'undefined :key on :position',
|
67
|
+
mappable: true,
|
68
|
+
type: 'string',
|
69
|
+
values: nil,
|
70
|
+
nested: nil,
|
71
|
+
allow_blank: false,
|
72
|
+
maptype: 'cell' }])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Parser do
|
4
|
+
|
5
|
+
let(:definition) do
|
6
|
+
Definition.new.tap do |definition|
|
7
|
+
definition.type = Definition::MAPPING
|
8
|
+
definition.rules = [ { position: [0,0], key: 'name' } ]
|
9
|
+
definition.validate!
|
10
|
+
definition.default!
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:data_source) { [ [ 'John Doe' ] ] }
|
15
|
+
|
16
|
+
subject { Csv2hash.new definition, data_source }
|
17
|
+
|
18
|
+
context 'regular way' do
|
19
|
+
it { expect { subject.parse }.to_not raise_error }
|
20
|
+
it {
|
21
|
+
subject.tap do |csv2hash|
|
22
|
+
csv2hash.parse
|
23
|
+
end.data.should eql({ data: [ { 'name' => 'John Doe' } ] })
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with nested' do
|
28
|
+
let(:data_source) { [ [ 'John Doe', 22 ] ] }
|
29
|
+
before do
|
30
|
+
definition.rules << { position: [0,1], key: 'age', nested: 'infos' }
|
31
|
+
end
|
32
|
+
it {
|
33
|
+
subject.tap { |c| c.parse }.data.should eql(
|
34
|
+
{ data: [ { 'name' => 'John Doe', 'infos' => { 'age' => 22 } } ] }
|
35
|
+
)
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Validator do
|
4
|
+
|
5
|
+
let(:definition) do
|
6
|
+
Definition.new.tap do |definition|
|
7
|
+
definition.type = Definition::MAPPING
|
8
|
+
definition.rules = [ { position: [0,0], key: 'name' } ]
|
9
|
+
definition.validate!
|
10
|
+
definition.default!
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject do
|
15
|
+
Csv2hash.new definition, data_source
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with valid data' do
|
19
|
+
let(:data_source) { [ [ 'John Doe' ] ]}
|
20
|
+
it { expect { subject.validate_data! }.to_not raise_error }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'with invalid data' do
|
24
|
+
let(:data_source) { [ [ ] ]}
|
25
|
+
it { expect { subject.validate_data! }.to raise_error('undefined name on [0, 0]') }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#message' do
|
29
|
+
subject { Csv2hash.new nil, nil }
|
30
|
+
|
31
|
+
context 'string value' do
|
32
|
+
let(:rule) { { foo: 'bar', message: ':foo are value of foo key' } }
|
33
|
+
|
34
|
+
it 'substitue value of key' do
|
35
|
+
subject.send(:message, rule).should eql 'bar are value of foo key'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'array value' do
|
40
|
+
let(:rule) { { foo: ['bar', 'zone'], message: ':foo are values of foo key' } }
|
41
|
+
|
42
|
+
it 'substitue value of key' do
|
43
|
+
subject.send(:message, rule).should eql '["bar", "zone"] are values of foo key'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv2hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel AZEMAR
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-26 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: DSL for CSV Ruby Hash mapping
|
56
|
+
email: joel.azemar@gmail.com
|
57
|
+
executables:
|
58
|
+
- load_rvm
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .coveralls.yml
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
|
+
- .travis.yml
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- Guardfile
|
69
|
+
- LICENSE
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/load_rvm
|
73
|
+
- csv2hash.gemspec
|
74
|
+
- lib/csv2hash.rb
|
75
|
+
- lib/csv2hash/definition.rb
|
76
|
+
- lib/csv2hash/definition/mapping.rb
|
77
|
+
- lib/csv2hash/parser.rb
|
78
|
+
- lib/csv2hash/validator.rb
|
79
|
+
- lib/csv2hash/version.rb
|
80
|
+
- spec/csv2hash/definition_spec.rb
|
81
|
+
- spec/csv2hash/parser_spec.rb
|
82
|
+
- spec/csv2hash/validator_spec.rb
|
83
|
+
- spec/csv2hash_spec.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
homepage: https://github.com/joel/csv2hash
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ~>
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '2.0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.1.11
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Mapping CSV to Ruby Hash
|
109
|
+
test_files:
|
110
|
+
- spec/csv2hash/definition_spec.rb
|
111
|
+
- spec/csv2hash/parser_spec.rb
|
112
|
+
- spec/csv2hash/validator_spec.rb
|
113
|
+
- spec/csv2hash_spec.rb
|
114
|
+
- spec/spec_helper.rb
|