yamload 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +7 -8
- data/lib/yamload/loader.rb +12 -11
- data/lib/yamload/version.rb +1 -1
- data/spec/loader_spec.rb +45 -27
- data/yamload.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 650a0838572afc492f7766cfff57ad8ca16d1e52
|
4
|
+
data.tar.gz: 214852d50263f5f60a4163cc12b45e2d6b327a43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 432872d1ddca948ffdf9c8681379997932a04267a6599ec716ddb62a6fecaaa3420f1f716af06e32f97584a7a026fc3d36e323bf32d8708fbd88b14bc23b90f0
|
7
|
+
data.tar.gz: 13ab50e74db4a5a4f856dad456942fbc3459188db3ff98110f93e2ab631d096ff35a54dc14a9a0f303ac7fcc7e6330ecc929cca1ab197a7d0beacf8a9f43696a
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Yamload
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/yamload.
|
4
|
-
[![Build Status](https://travis-ci.org/sealink/yamload.
|
5
|
-
[![Coverage Status](https://coveralls.io/repos/sealink/yamload/badge.
|
6
|
-
[![Dependency Status](https://gemnasium.com/sealink/yamload.
|
7
|
-
[![Code Climate](https://codeclimate.com/github/sealink/yamload.
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/yamload.svg)](http://badge.fury.io/rb/yamload)
|
4
|
+
[![Build Status](https://travis-ci.org/sealink/yamload.svg?branch=master)](https://travis-ci.org/sealink/yamload)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/sealink/yamload/badge.svg)](https://coveralls.io/r/sealink/yamload)
|
6
|
+
[![Dependency Status](https://gemnasium.com/sealink/yamload.svg)](https://gemnasium.com/sealink/yamload)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/sealink/yamload/badges/gpa.svg)](https://codeclimate.com/github/sealink/yamload)
|
8
8
|
|
9
9
|
* YAML files loading
|
10
10
|
* Recursive conversion to immutable objects
|
@@ -54,9 +54,7 @@ Define a schema for the configuration
|
|
54
54
|
```ruby
|
55
55
|
# Load config/test.yml
|
56
56
|
loader = Yamload::Loader.new(:test)
|
57
|
-
loader.
|
58
|
-
schema.string 'test'
|
59
|
-
end
|
57
|
+
loader.schema = { 'test' => String }
|
60
58
|
loader.valid?
|
61
59
|
# => true
|
62
60
|
loader.validate!
|
@@ -64,6 +62,7 @@ loader.validate!
|
|
64
62
|
loader.error
|
65
63
|
# => nil
|
66
64
|
```
|
65
|
+
See [Classy Hash](https://github.com/deseretbook/classy_hash) for documentation on schema definitions
|
67
66
|
|
68
67
|
Define defaults
|
69
68
|
```ruby
|
data/lib/yamload/loader.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'yaml'
|
2
|
-
require '
|
2
|
+
require 'classy_hash'
|
3
3
|
require 'facets/hash/deep_merge'
|
4
4
|
|
5
5
|
module Yamload
|
@@ -22,11 +22,7 @@ module Yamload
|
|
22
22
|
loaded_hash
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
@schema = Respect::HashSchema.define do |schema|
|
27
|
-
yield schema
|
28
|
-
end
|
29
|
-
end
|
25
|
+
attr_accessor :schema
|
30
26
|
|
31
27
|
attr_writer :defaults
|
32
28
|
|
@@ -35,18 +31,23 @@ module Yamload
|
|
35
31
|
end
|
36
32
|
|
37
33
|
def valid?
|
38
|
-
|
39
|
-
|
34
|
+
validate!
|
35
|
+
true
|
36
|
+
rescue SchemaError
|
37
|
+
false
|
40
38
|
end
|
41
39
|
|
42
40
|
def validate!
|
43
|
-
|
44
|
-
|
41
|
+
@error = nil
|
42
|
+
ClassyHash.validate(loaded_hash, @schema)
|
43
|
+
rescue RuntimeError => e
|
44
|
+
@error = e.message
|
45
|
+
raise SchemaError, @error
|
45
46
|
end
|
46
47
|
|
47
48
|
def error
|
48
49
|
return nil if valid?
|
49
|
-
@
|
50
|
+
@error
|
50
51
|
end
|
51
52
|
|
52
53
|
private
|
data/lib/yamload/version.rb
CHANGED
data/spec/loader_spec.rb
CHANGED
@@ -85,43 +85,52 @@ describe Yamload do
|
|
85
85
|
end
|
86
86
|
|
87
87
|
context 'when a schema is defined' do
|
88
|
-
let(:
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
88
|
+
let(:schema) {
|
89
|
+
{
|
90
|
+
'test' => TrueClass,
|
91
|
+
'users' => [
|
92
|
+
[
|
93
|
+
{
|
94
|
+
'first_name' => String,
|
95
|
+
'last_name' => String,
|
96
|
+
'address' => {
|
97
|
+
'address_line_1' => String,
|
98
|
+
'address_line_2' => [:optional, String, NilClass],
|
99
|
+
'city' => String,
|
100
|
+
'state' => String,
|
101
|
+
'post_code' => Integer,
|
102
|
+
'country' => String
|
103
|
+
},
|
104
|
+
'email' => String
|
105
|
+
}
|
106
|
+
]
|
107
|
+
],
|
108
|
+
'settings' => {
|
109
|
+
'remote_access' => TrueClass
|
110
|
+
}
|
111
|
+
}
|
105
112
|
}
|
106
113
|
|
107
|
-
before {
|
114
|
+
before { loader.schema = schema }
|
108
115
|
|
109
116
|
specify { expect(loader).to be_valid }
|
110
117
|
specify { expect(loader.error).to be_nil }
|
111
118
|
specify { expect { loader.validate! }.not_to raise_error }
|
112
119
|
|
113
120
|
context 'when the schema is not matched' do
|
114
|
-
let(:
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
121
|
+
let(:schema) {
|
122
|
+
{
|
123
|
+
'users' => [
|
124
|
+
[
|
125
|
+
{
|
126
|
+
'expected_attribute' => String
|
127
|
+
}
|
128
|
+
]
|
129
|
+
]
|
130
|
+
}
|
122
131
|
}
|
123
132
|
|
124
|
-
let(:expected_error) { "
|
133
|
+
let(:expected_error) { '"users"[0]["expected_attribute"] is not present' }
|
125
134
|
specify { expect(loader).not_to be_valid }
|
126
135
|
specify { expect(loader.error).to eq expected_error }
|
127
136
|
specify { expect { loader.validate! }.to raise_error Yamload::SchemaError, expected_error }
|
@@ -145,4 +154,13 @@ describe Yamload do
|
|
145
154
|
specify { expect(config_obj.settings.remember_user).to eq false }
|
146
155
|
specify { expect(config_obj.settings.remote_access).to eq true }
|
147
156
|
end
|
157
|
+
|
158
|
+
context 'when reloading' do
|
159
|
+
let(:original_hash) { loader.loaded_hash }
|
160
|
+
before do
|
161
|
+
original_hash
|
162
|
+
loader.reload
|
163
|
+
end
|
164
|
+
specify { expect(loader.loaded_hash).not_to be original_hash }
|
165
|
+
end
|
148
166
|
end
|
data/yamload.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
|
23
23
|
spec.add_dependency 'anima', '~> 0.2'
|
24
24
|
spec.add_dependency 'facets', '~> 3.0'
|
25
|
-
spec.add_dependency '
|
25
|
+
spec.add_dependency 'classy_hash', '~> 0.1'
|
26
26
|
|
27
27
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
28
28
|
spec.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yamload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessandro Berardi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: anima
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: classy_hash
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|