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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6be6f6616ab52beb4071293efae46f89ace03f8
4
- data.tar.gz: f69f085e8311b5f37b58f2f62f2aa0720284e96d
3
+ metadata.gz: 650a0838572afc492f7766cfff57ad8ca16d1e52
4
+ data.tar.gz: 214852d50263f5f60a4163cc12b45e2d6b327a43
5
5
  SHA512:
6
- metadata.gz: 243f48a7eae25f29cf995c3de52af317414b78c2a9c9d03921272b3f26890dd698b1884c99ba1bc0d14d3fd80bcd47df1e5ee21093755efa1b83d7a5016e129b
7
- data.tar.gz: f37efa4c9ca19518c591b3b9357c15146604fdad7d9192d48759b27117ab76b6eadbba672bb0c6538a00114839d13caaff2e3793b95156dc9ac1cc799f26243e
6
+ metadata.gz: 432872d1ddca948ffdf9c8681379997932a04267a6599ec716ddb62a6fecaaa3420f1f716af06e32f97584a7a026fc3d36e323bf32d8708fbd88b14bc23b90f0
7
+ data.tar.gz: 13ab50e74db4a5a4f856dad456942fbc3459188db3ff98110f93e2ab631d096ff35a54dc14a9a0f303ac7fcc7e6330ecc929cca1ab197a7d0beacf8a9f43696a
data/.gitignore CHANGED
@@ -14,3 +14,4 @@
14
14
  *.o
15
15
  *.a
16
16
  mkmf.log
17
+ .DS_Store
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Yamload
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/yamload.png)](http://badge.fury.io/rb/yamload)
4
- [![Build Status](https://travis-ci.org/sealink/yamload.png?branch=master)](https://travis-ci.org/sealink/yamload)
5
- [![Coverage Status](https://coveralls.io/repos/sealink/yamload/badge.png)](https://coveralls.io/r/sealink/yamload)
6
- [![Dependency Status](https://gemnasium.com/sealink/yamload.png)](https://gemnasium.com/sealink/yamload)
7
- [![Code Climate](https://codeclimate.com/github/sealink/yamload.png)](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.define_schema do |schema|
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
@@ -1,5 +1,5 @@
1
1
  require 'yaml'
2
- require 'respect'
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
- def define_schema
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
- return true if @schema.nil?
39
- @schema.validate? loaded_hash
34
+ validate!
35
+ true
36
+ rescue SchemaError
37
+ false
40
38
  end
41
39
 
42
40
  def validate!
43
- return if valid?
44
- fail SchemaError, error
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
- @schema.last_error.message
50
+ @error
50
51
  end
51
52
 
52
53
  private
@@ -1,3 +1,3 @@
1
1
  module Yamload
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
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(:define_schema) {
89
- loader.define_schema do |schema|
90
- schema.array 'users' do |users_array|
91
- users_array.hash do |user_hash|
92
- user_hash.string 'first_name'
93
- user_hash.string 'last_name'
94
- user_hash.hash 'address' do |address_hash|
95
- address_hash.string 'address_line_1'
96
- address_hash.string 'city'
97
- address_hash.string 'state'
98
- address_hash.integer 'post_code'
99
- address_hash.string 'country'
100
- end
101
- user_hash.string 'email', format: :email
102
- end
103
- end
104
- end
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 { define_schema }
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(:define_schema) {
115
- loader.define_schema do |schema|
116
- schema.array 'users' do |users_array|
117
- users_array.hash do |user_hash|
118
- user_hash.string 'expected_attribute'
119
- end
120
- end
121
- end
121
+ let(:schema) {
122
+ {
123
+ 'users' => [
124
+ [
125
+ {
126
+ 'expected_attribute' => String
127
+ }
128
+ ]
129
+ ]
130
+ }
122
131
  }
123
132
 
124
- let(:expected_error) { "missing key `expected_attribute'" }
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 'respect', '~> 0.1'
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.2
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-03 00:00:00.000000000 Z
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: respect
42
+ name: classy_hash
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"