compel 0.3.7 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd80b5474b8218a24a20053cbf9a52645965acd3
4
- data.tar.gz: c793b3161901f6aa6872b0763a7fb9570c9864d9
3
+ metadata.gz: 032ba6894d54a63ee956025716cb973156d601b0
4
+ data.tar.gz: 0121184a327f472d7750180db68411253f91d465
5
5
  SHA512:
6
- metadata.gz: 78f27f5b101c0255540f8a99018d750247ddae79eca4847b1a656fbbd527e4af1445ce12567f5049b201fc1309bdd1b594af225876937b450689fc269f2b92c6
7
- data.tar.gz: 6c1d7772d9cd20a21f6e36fe0f1c717bebb358a4a98ed2c3fb6831f986770895d4040901bf4a8ccc25e3b0ccd16cd72ae34ed0b142e9c02330bb83a4946c4ebf
6
+ metadata.gz: 4fb359e62420fc5a8b780297bfc05a9ffcc986034d362e4cace62626a4f8e67c0e75a38013335f8fe1eb48c93a560ba2bbc41b9ecdfd49eeaa591443fcfb1f71
7
+ data.tar.gz: f933cdda27071b04278ff0812f91c7956a4a92e478c08c6912cef70878de9bddb88860e5b7f074bedcdcd3620c002c919c4b13a69ae9ce71abe2f58371d0480e
data/README.md CHANGED
@@ -169,6 +169,8 @@ Methods `length`, `min_length` and `max_length` turn the object to validate into
169
169
  - `min(``value``)`
170
170
  - `max(``value``)`
171
171
  - `format(``regexp``)`
172
+ - `email`
173
+ - `url`
172
174
 
173
175
  ==========================
174
176
 
data/compel.gemspec CHANGED
@@ -18,7 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ['lib']
20
20
 
21
- gem.add_runtime_dependency 'hashie', '~> 3.4'
22
21
  gem.add_development_dependency 'rspec', '~> 3.2'
23
22
  gem.add_development_dependency 'rake', '~> 0'
24
23
  gem.add_development_dependency 'pry', '~> 0'
data/lib/compel.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'time'
2
- require 'hashie'
3
2
 
4
3
  require 'compel/exceptions/type_error'
5
4
  require 'compel/exceptions/invalid_object_error'
@@ -36,7 +36,7 @@ module Compel
36
36
  protected
37
37
 
38
38
  def default_options
39
- Hashie::Mash.new(required: false)
39
+ { required: false }
40
40
  end
41
41
 
42
42
  end
@@ -4,9 +4,20 @@ module Compel
4
4
  class Hash < Type
5
5
 
6
6
  def coerce_value
7
- Hashie::Mash.new(value).to_hash rescue nil
7
+ if ::Hash.try_convert(value)
8
+ symbolyze_keys(value)
9
+ end
8
10
  end
9
11
 
12
+ private
13
+
14
+ def symbolyze_keys(hash)
15
+ {}.tap do |symbolyzed_hash|
16
+ hash.each do |key, value|
17
+ symbolyzed_hash[key.to_sym] = value
18
+ end
19
+ end
20
+ end
10
21
  end
11
22
 
12
23
  end
data/lib/compel/errors.rb CHANGED
@@ -3,7 +3,7 @@ module Compel
3
3
  class Errors
4
4
 
5
5
  def initialize
6
- @errors = Hashie::Mash.new
6
+ @errors = {}
7
7
  end
8
8
 
9
9
  def add(key, error)
@@ -20,8 +20,6 @@ module Compel
20
20
  return self
21
21
  end
22
22
 
23
- @input = Hashie::Mash.new(input)
24
-
25
23
  keys_validator = \
26
24
  HashKeysValidator.validate(input, keys_schemas)
27
25
 
@@ -32,7 +30,7 @@ module Compel
32
30
  end
33
31
 
34
32
  def serialize
35
- coerced = output.is_a?(Hash) ? input.merge(output) : Hashie::Mash.new
33
+ coerced = output.is_a?(Hash) ? input.merge(output) : {}
36
34
 
37
35
  coerced.tap do |hash|
38
36
  if !errors.empty?
@@ -1,3 +1,3 @@
1
1
  module Compel
2
- VERSION = '0.3.7'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -4,11 +4,11 @@ describe Compel::Builder do
4
4
 
5
5
  context 'Build' do
6
6
 
7
- it 'should build new Schema for givin type' do
7
+ it 'should build new Schema for given type' do
8
8
  builder = Compel.string
9
9
 
10
10
  expect(builder.type).to be(Compel::Coercion::String)
11
- expect(builder.options.keys).to include('required')
11
+ expect(builder.options.keys).to include(:required)
12
12
  expect(builder.required?).to be false
13
13
  expect(builder.default_value).to be nil
14
14
  end
@@ -298,14 +298,14 @@ describe Compel::Builder do
298
298
 
299
299
  keys_schemas = schema.options[:keys]
300
300
 
301
- expect(keys_schemas.a.type).to be Compel::Coercion::Float
302
- expect(keys_schemas.b.type).to be Compel::Coercion::String
303
- expect(keys_schemas.c.type).to be Compel::Coercion::Hash
304
- expect(keys_schemas.d.type).to be Compel::Coercion::JSON
305
- expect(keys_schemas.e.type).to be Compel::Coercion::Time
306
- expect(keys_schemas.f.type).to be Compel::Coercion::DateTime
307
- expect(keys_schemas.g.type).to be Compel::Coercion::Date
308
- expect(keys_schemas.h.type).to be Compel::Coercion::Integer
301
+ expect(keys_schemas[:a].type).to be Compel::Coercion::Float
302
+ expect(keys_schemas[:b].type).to be Compel::Coercion::String
303
+ expect(keys_schemas[:c].type).to be Compel::Coercion::Hash
304
+ expect(keys_schemas[:d].type).to be Compel::Coercion::JSON
305
+ expect(keys_schemas[:e].type).to be Compel::Coercion::Time
306
+ expect(keys_schemas[:f].type).to be Compel::Coercion::DateTime
307
+ expect(keys_schemas[:g].type).to be Compel::Coercion::Date
308
+ expect(keys_schemas[:h].type).to be Compel::Coercion::Integer
309
309
  end
310
310
 
311
311
  it 'should raise error for invalid #keys' do
@@ -557,19 +557,19 @@ describe Compel::Builder do
557
557
 
558
558
  expect(result.value).to \
559
559
  eq({
560
- "first_name" => "Joaquim",
561
- "birth_date" => "1989-0",
562
- "address" => {
563
- "line_one" => "Lisboa",
564
- "post_code" => "1100",
565
- "country_code" => "PT",
566
- "line_two" => "-"
560
+ first_name: 'Joaquim',
561
+ birth_date: '1989-0',
562
+ address: {
563
+ line_one: 'Lisboa',
564
+ post_code: '1100',
565
+ country_code: 'PT',
566
+ line_two: '-'
567
567
  },
568
- "errors" => {
569
- "last_name" => ["is required"],
570
- "birth_date" => ["'1989-0' is not a parsable date with format: %Y-%m-%d"],
571
- "address" => {
572
- "post_code" => ["must match format ^\\d{4}-\\d{3}$"]
568
+ errors: {
569
+ last_name: ['is required'],
570
+ birth_date: ["'1989-0' is not a parsable date with format: %Y-%m-%d"],
571
+ address: {
572
+ post_code: ["must match format ^\\d{4}-\\d{3}$"]
573
573
  }
574
574
  }
575
575
  })
@@ -580,7 +580,7 @@ describe Compel::Builder do
580
580
  a: Compel.float.required
581
581
  })
582
582
 
583
- expect(schema.validate({ a: nil }).errors.a).to \
583
+ expect(schema.validate({ a: nil }).errors[:a]).to \
584
584
  include('is required')
585
585
  end
586
586
 
@@ -613,7 +613,7 @@ describe Compel::Builder do
613
613
  result = schema.validate({ a: 1, b: 2, c: 3 })
614
614
 
615
615
  expect(result.errors[:base]).to \
616
- include("must be {\"a\"=>1, \"b\"=>2, \"c\"=>{\"d\"=>3, \"e\"=>4}}")
616
+ include("must be #{value.to_hash}")
617
617
  end
618
618
 
619
619
  it 'should validate without errors' do
@@ -763,9 +763,9 @@ describe Compel::Builder do
763
763
  expect(result.valid?).to be true
764
764
  expect(result.value).to eq \
765
765
  [
766
- Hashie::Mash.new({ a: 'A', b: 1 }),
767
- Hashie::Mash.new({ a: 'B' }),
768
- Hashie::Mash.new({ a: 'C', b: 3 })
766
+ { a: 'A', b: 1 },
767
+ { a: 'B' },
768
+ { a: 'C', b: 3 }
769
769
  ]
770
770
  end
771
771
 
@@ -149,8 +149,8 @@ describe Compel::Coercion do
149
149
  }, Compel::Coercion::Hash)
150
150
 
151
151
  expect(value).to eq({
152
- 'first_name' => 'Joaquim',
153
- 'last_name' => 'Adráz'
152
+ first_name: 'Joaquim',
153
+ last_name: 'Adráz'
154
154
  })
155
155
  end
156
156
 
@@ -161,20 +161,20 @@ describe Compel::Coercion do
161
161
  }, Compel::Coercion::Hash)
162
162
 
163
163
  expect(value).to eq({
164
- 'first_name' => 'Joaquim',
165
- 'last_name' => 'Adráz'
164
+ first_name: 'Joaquim',
165
+ last_name: 'Adráz'
166
166
  })
167
167
  end
168
168
 
169
169
  it 'should coerce 2' do
170
- value = Compel::Coercion.coerce!(Hashie::Mash.new({
170
+ value = Compel::Coercion.coerce!({
171
171
  first_name: 'Joaquim',
172
172
  last_name: 'Adráz'
173
- }), Compel::Coercion::Hash)
173
+ }, Compel::Coercion::Hash)
174
174
 
175
175
  expect(value).to eq({
176
- 'first_name' => 'Joaquim',
177
- 'last_name' => 'Adráz'
176
+ first_name: 'Joaquim',
177
+ last_name: 'Adráz'
178
178
  })
179
179
  end
180
180
 
@@ -39,18 +39,16 @@ describe Compel do
39
39
 
40
40
  expect(result.valid?).to be true
41
41
  expect(result.value).to eq \
42
- Hashie::Mash.new({
43
- user: {
44
- first_name: 'Joaquim',
45
- last_name: 'Adráz',
46
- birth_date: DateTime.parse('1989-08-06T09:00:00'),
47
- age: 26,
48
- admin: false,
49
- blog_role: {
50
- admin: false
51
- }
42
+ user: {
43
+ first_name: 'Joaquim',
44
+ last_name: 'Adráz',
45
+ birth_date: DateTime.parse('1989-08-06T09:00:00'),
46
+ age: 26,
47
+ admin: false,
48
+ blog_role: {
49
+ admin: false
52
50
  }
53
- })
51
+ }
54
52
  end
55
53
 
56
54
  end
@@ -69,17 +67,15 @@ describe Compel do
69
67
 
70
68
  expect(result.valid?).to be false
71
69
  expect(result.value).to eq \
72
- Hashie::Mash.new({
73
- other_param: 1,
70
+ other_param: 1,
71
+ user: {
72
+ first_name: 'Joaquim',
73
+ },
74
+ errors: {
74
75
  user: {
75
- first_name: 'Joaquim',
76
- },
77
- errors: {
78
- user: {
79
- last_name: ['is required']
80
- }
76
+ last_name: ['is required']
81
77
  }
82
- })
78
+ }
83
79
  end
84
80
 
85
81
  it 'should not compel for invalid hash' do
@@ -114,16 +110,14 @@ describe Compel do
114
110
 
115
111
  expect(result.valid?).to be false
116
112
  expect(result.value).to eq \
117
- Hashie::Mash.new({
118
- user:{
119
- first_name: 'Joaquim',
120
- },
121
- errors: {
122
- user: {
123
- last_name: ['is required']
124
- }
113
+ user:{
114
+ first_name: 'Joaquim',
115
+ },
116
+ errors: {
117
+ user: {
118
+ last_name: ['is required']
125
119
  }
126
- })
120
+ }
127
121
  end
128
122
 
129
123
  end
@@ -181,17 +175,15 @@ describe Compel do
181
175
 
182
176
  expect(result.valid?).to be false
183
177
  expect(result.value).to eq \
184
- Hashie::Mash.new({
178
+ address: {
179
+ line_two: 'Portugal'
180
+ },
181
+ errors: {
185
182
  address: {
186
- line_two: 'Portugal'
187
- },
188
- errors: {
189
- address: {
190
- line_one: ['is required'],
191
- post_code: ['is required']
192
- }
183
+ line_one: ['is required'],
184
+ post_code: ['is required']
193
185
  }
194
- })
186
+ }
195
187
  end
196
188
 
197
189
  it 'should not compel missing key and length invalid' do
@@ -211,25 +203,23 @@ describe Compel do
211
203
 
212
204
  expect(result.valid?).to be false
213
205
  expect(result.value).to eq \
214
- Hashie::Mash.new({
206
+ address: {
207
+ line_two: 'Portugal',
208
+ post_code: {
209
+ prefix: 1,
210
+ county: {
211
+ code: 'LX'
212
+ }
213
+ }
214
+ },
215
+ errors: {
215
216
  address: {
216
- line_two: 'Portugal',
217
+ line_one: ['is required'],
217
218
  post_code: {
218
- prefix: 1,
219
- county: {
220
- code: 'LX'
221
- }
222
- }
223
- },
224
- errors: {
225
- address: {
226
- line_one: ['is required'],
227
- post_code: {
228
- prefix: ['cannot have length different than 4']
229
- }
219
+ prefix: ['cannot have length different than 4']
230
220
  }
231
221
  }
232
- })
222
+ }
233
223
  end
234
224
 
235
225
  it 'should not compel for givin invalid optional value' do
@@ -248,25 +238,23 @@ describe Compel do
248
238
 
249
239
  expect(result.valid?).to be false
250
240
  expect(result.value).to eq \
251
- Hashie::Mash.new({
241
+ address: {
242
+ line_one: 'Line',
243
+ post_code: {
244
+ prefix: 1100,
245
+ suffix: 100,
246
+ county: {}
247
+ }
248
+ },
249
+ errors: {
252
250
  address: {
253
- line_one: 'Line',
254
251
  post_code: {
255
- prefix: 1100,
256
- suffix: 100,
257
- county: {}
258
- }
259
- },
260
- errors: {
261
- address: {
262
- post_code: {
263
- county: {
264
- code: ['is required']
265
- }
252
+ county: {
253
+ code: ['is required']
266
254
  }
267
255
  }
268
256
  }
269
- })
257
+ }
270
258
 
271
259
  end
272
260
 
@@ -279,12 +267,10 @@ describe Compel do
279
267
 
280
268
  expect(result.valid?).to be false
281
269
  expect(result.value).to eq \
282
- Hashie::Mash.new({
283
- address: nil,
284
- errors: {
285
- address: ['is required']
286
- }
287
- })
270
+ address: nil,
271
+ errors: {
272
+ address: ['is required']
273
+ }
288
274
  end
289
275
 
290
276
  it 'should not compel for empty object' do
@@ -292,11 +278,9 @@ describe Compel do
292
278
 
293
279
  expect(result.valid?).to be false
294
280
  expect(result.value).to eq \
295
- Hashie::Mash.new({
296
- errors: {
297
- address: ['is required']
298
- }
299
- })
281
+ errors: {
282
+ address: ['is required']
283
+ }
300
284
  end
301
285
 
302
286
  end
@@ -516,11 +500,9 @@ describe Compel do
516
500
 
517
501
  expect(make_the_call(:run!, hash)).to \
518
502
  eq \
519
- Hashie::Mash.new({
520
- first_name: 'Joaquim',
521
- last_name: 'Adráz',
522
- birth_date: DateTime.new(1988, 12, 24)
523
- })
503
+ first_name: 'Joaquim',
504
+ last_name: 'Adráz',
505
+ birth_date: DateTime.new(1988, 12, 24)
524
506
  end
525
507
 
526
508
  it 'should raise InvalidObjectError exception for missing required key' do
@@ -539,7 +521,10 @@ describe Compel do
539
521
 
540
522
  expect{ make_the_call(:run!, hash) }.to raise_error do |exception|
541
523
  expect(exception.object).to eq \
542
- Hashie::Mash.new(first_name: 'Joaquim', errors: { last_name: ['is required'] })
524
+ first_name: 'Joaquim',
525
+ errors: {
526
+ last_name: ['is required']
527
+ }
543
528
  end
544
529
  end
545
530
 
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joaquim Adráz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-15 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: hashie
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '3.4'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '3.4'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rspec
29
15
  requirement: !ruby/object:Gem::Requirement