compel 0.1.1 → 0.1.2

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: 20943c4ee705f920a1d316a9028b0ddc7e26cfc7
4
- data.tar.gz: e9071ee624b81b8efe30b1747eacc3d00426339b
3
+ metadata.gz: 68ba3b85759b0fddf7579d70d30d62f311ce365e
4
+ data.tar.gz: a250017bbdf6de848efcbdae5cd4664bd6261501
5
5
  SHA512:
6
- metadata.gz: 638f760fe8ea31a6545101cf48923ed2d835cf4f449d4f29bbf64610945eb78013d4aaedb5ea0724946846235b490741712b12d7dbd8cc09ac2c60127734aa0f
7
- data.tar.gz: cf70bca72fc84dbc3e4c3434a16bb49569a659188e1be6049e20d058027ac33be2f0361b12f240a9d1b5e0a3b6572a7b5ae295735445cf29628f09f28558d1b9
6
+ metadata.gz: 5ce4e60243142f7f0b6312c4e80ff5e09068b029c54e989fc8149f84740d62383ed952a1db685e1a97f62a2842efa221c14562d67d8692260ca76fc97f545eb8
7
+ data.tar.gz: 728d7340542f63acea00f5e40da20c90ff93b591c07f132a2a18ff8c85bca7176df4527a2d83944455d1b603bbe742f8cfacf399e8014e4bdbc8bfa61bf287a4
data/README.md CHANGED
@@ -11,16 +11,6 @@ The motivation was to create an integration for [RestMyCase](https://github.com/
11
11
 
12
12
  Based on the same principle from [Grape](https://github.com/ruby-grape/grape) framework and [sinatra-param](https://github.com/mattt/sinatra-param) gem to validate request params.
13
13
 
14
- ###Installation
15
-
16
- Add this line to your application's Gemfile:
17
-
18
- gem 'compel'
19
-
20
- And then execute:
21
-
22
- $ bundle
23
-
24
14
  ### Example
25
15
 
26
16
  ```ruby
@@ -68,15 +58,13 @@ Will return an [Hashie::Mash](https://github.com/intridea/hashie) object:
68
58
  }
69
59
  ```
70
60
 
71
- There are 3 ways run validations:
61
+ There are 3 ways to run validations:
72
62
 
73
- - `#run`
74
- - Validates and returns an Hash with coerced params plus a `:errors` key with a _Rails like_ Hash of errors if any.
75
- - `#run!`
76
- - Validates and raises `Compel::InvalidParamsError` exception with the coerced params and errors.
77
- - `#run?`
78
- - Validates and returns true or false.
79
-
63
+ Method | Behaviour
64
+ ------------- | -------------
65
+ `#run` | Validates and returns an Hash with coerced params plus a `:errors` key with a _Rails like_ Hash of errors if any.
66
+ `#run!` | Validates and raises `Compel::InvalidParamsError` exception with the coerced params and errors.
67
+ `#run?` | Validates and returns true or false.
80
68
 
81
69
  ### Types
82
70
 
@@ -103,7 +91,7 @@ class App < Sinatra::Base
103
91
  ...
104
92
 
105
93
  def compel(&block)
106
- Compel::run!(params, &block)
94
+ params.merge! Compel::run!(params, &block)
107
95
  end
108
96
 
109
97
  error Compel::InvalidParamsError do |exception|
@@ -116,9 +104,32 @@ class App < Sinatra::Base
116
104
  set :raise_errors, true
117
105
  end
118
106
 
107
+ ...
108
+
109
+ post '/api/posts' do
110
+ compel do
111
+ param :post, Hash, required: true do
112
+ param :title, String, required: true
113
+ end
114
+ end
115
+
116
+ puts params[:post]
117
+ end
118
+
119
+ ...
120
+
119
121
  end
120
122
  ```
123
+ ###Installation
121
124
 
125
+ Add this line to your application's Gemfile:
126
+
127
+ gem 'compel'
128
+
129
+ And then execute:
130
+
131
+ $ bundle
132
+
122
133
  ### TODO
123
134
 
124
135
  - Write more Documentation (check specs for now ;)
@@ -4,7 +4,6 @@ module Compel
4
4
 
5
5
  attr_reader :errors,
6
6
  :conditions,
7
- :coerced_params,
8
7
  :serialized_errors
9
8
 
10
9
  def initialize(params, &block)
@@ -44,7 +43,7 @@ module Compel
44
43
  # raise exception to avoid validation
45
44
 
46
45
  # If the param value has already been coerced from digging into child Hash
47
- # use that value instead, so we don't loose the previous coerced values
46
+ # use that value instead, so we don't lose the previous coerced values
48
47
 
49
48
  coerced_value = Coercion.coerce! \
50
49
  (@coerced_params[param.name].nil? ? param.value : @coerced_params[param.name]), param.type, param.options
@@ -67,7 +66,12 @@ module Compel
67
66
 
68
67
  def param(name, type, options = {}, &block)
69
68
  @conditions[name] = \
70
- Param.new(name, type, @params[name], options, &block)
69
+ Param.new(name, type, @params.delete(name), options, &block)
70
+ end
71
+
72
+ def coerced_params
73
+ # @params has all params that are not affected by the validation
74
+ @params.merge(@coerced_params)
71
75
  end
72
76
 
73
77
  def serialize
@@ -94,6 +98,8 @@ module Compel
94
98
 
95
99
  raise exception, 'params are invalid'
96
100
  end
101
+
102
+ coerced_params
97
103
  end
98
104
 
99
105
  end
@@ -1,3 +1,3 @@
1
1
  module Compel
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -19,7 +19,7 @@ describe Compel do
19
19
  raise_error Compel::InvalidParamsError, 'params are invalid'
20
20
  end
21
21
 
22
- it 'shoudl raise InvalidParamsError exception with errors' do
22
+ it 'should raise InvalidParamsError exception with errors' do
23
23
  params = {
24
24
  first_name: 'Joaquim'
25
25
  }
@@ -61,38 +61,66 @@ describe Compel do
61
61
 
62
62
  def make_the_call(method, params)
63
63
  Compel.send(method, params) do
64
- param :first_name, String, required: true
65
- param :last_name, String, required: true
66
- param :birth_date, DateTime
67
- param :age, Integer
68
- param :admin, Compel::Boolean
69
- param :blog_role, Hash do
70
- param :admin, Compel::Boolean, required: true
64
+ param :user, Hash, required: true do
65
+ param :first_name, String, required: true
66
+ param :last_name, String, required: true
67
+ param :birth_date, DateTime
68
+ param :age, Integer
69
+ param :admin, Compel::Boolean
70
+ param :blog_role, Hash do
71
+ param :admin, Compel::Boolean, required: true
72
+ end
71
73
  end
72
74
  end
73
75
  end
74
76
 
75
77
  it 'should compel returning coerced values' do
76
78
  params = {
77
- first_name: 'Joaquim',
78
- last_name: 'Adráz',
79
- birth_date: '1989-08-06T09:00:00',
80
- age: '26',
81
- admin: 'f',
82
- blog_role: {
83
- admin: '0'
79
+ user: {
80
+ first_name: 'Joaquim',
81
+ last_name: 'Adráz',
82
+ birth_date: '1989-08-06T09:00:00',
83
+ age: '26',
84
+ admin: 'f',
85
+ blog_role: {
86
+ admin: '0'
87
+ }
84
88
  }
85
89
  }
86
90
 
87
91
  expect(make_the_call(:run, params)).to eq \
88
92
  Hashie::Mash.new({
89
- first_name: 'Joaquim',
90
- last_name: 'Adráz',
91
- birth_date: DateTime.parse('1989-08-06T09:00:00'),
92
- age: 26,
93
- admin: false,
94
- blog_role: {
95
- admin: false
93
+ user: {
94
+ first_name: 'Joaquim',
95
+ last_name: 'Adráz',
96
+ birth_date: DateTime.parse('1989-08-06T09:00:00'),
97
+ age: 26,
98
+ admin: false,
99
+ blog_role: {
100
+ admin: false
101
+ }
102
+ }
103
+ })
104
+ end
105
+
106
+ it 'should not compel and leave other params untouched' do
107
+ params = {
108
+ other_param: 1,
109
+ user: {
110
+ first_name: 'Joaquim'
111
+ }
112
+ }
113
+
114
+ expect(make_the_call(:run, params)).to eq \
115
+ Hashie::Mash.new({
116
+ other_param: 1,
117
+ user: {
118
+ first_name: 'Joaquim',
119
+ },
120
+ errors: {
121
+ user: {
122
+ last_name: ['is required']
123
+ }
96
124
  }
97
125
  })
98
126
  end
@@ -109,14 +137,20 @@ describe Compel do
109
137
 
110
138
  it 'should not compel' do
111
139
  params = {
112
- first_name: 'Joaquim'
140
+ user: {
141
+ first_name: 'Joaquim'
142
+ }
113
143
  }
114
144
 
115
145
  expect(make_the_call(:run, params)).to eq \
116
146
  Hashie::Mash.new({
117
- first_name: 'Joaquim',
147
+ user:{
148
+ first_name: 'Joaquim',
149
+ },
118
150
  errors: {
119
- last_name: ['is required']
151
+ user: {
152
+ last_name: ['is required']
153
+ }
120
154
  }
121
155
  })
122
156
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joaquim Adráz