foraneus 0.0.11 → 0.0.12
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 +4 -4
- data/README.md +67 -0
- data/lib/foraneus.rb +36 -29
- data/spec/lib/foraneus_spec.rb +45 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dd151153010e57f4108ce2e054238b10b2d0b46
|
4
|
+
data.tar.gz: b582478fbb77f95ef5f924b1e7ae2b5bb492c15b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a0c78292a5a51eb6d59dbb6000bed04e4669b95e0b3a78b09e84a9d4c302db1a426930a141166291cab1d2b34279a5837625932655f29270e25b7c82abef74f
|
7
|
+
data.tar.gz: c9385cc3522459024a4d68f148dbb08200411099b3d809e85f54d4c51d4ab99bc8a2965466f278b960d093d15e9d0fe929bd687dffd75e5c4e0f24d7a2a1135d
|
data/README.md
CHANGED
@@ -142,6 +142,73 @@ message is added to the error's `message` attribute.
|
|
142
142
|
Data coming from the inside is assumed to be valid, so `.raw` won't return an instance having
|
143
143
|
errors neither being invalid.
|
144
144
|
|
145
|
+
## Required fields
|
146
|
+
|
147
|
+
Fields can be declared as required.
|
148
|
+
|
149
|
+
``` ruby
|
150
|
+
class MyForm < Foraneus
|
151
|
+
integer :delay, :required => true
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
If an external value is not fed into a required field, an error with key 'KeyError' will be assigned.
|
156
|
+
|
157
|
+
``` ruby
|
158
|
+
form = MyForm.parse
|
159
|
+
|
160
|
+
form.valid? # => false
|
161
|
+
|
162
|
+
form[:errors][:delay].key # => 'KeyError'
|
163
|
+
form[:errors][:delay].message # => 'required parameter not found: "delay"'
|
164
|
+
```
|
165
|
+
|
166
|
+
## Blank values
|
167
|
+
|
168
|
+
By default, any blank value is treated as nil.
|
169
|
+
|
170
|
+
``` ruby
|
171
|
+
MyForm = Class.new(Foraneus) { string :name }
|
172
|
+
|
173
|
+
MyForm.parse(:name => '').name
|
174
|
+
# => nil
|
175
|
+
```
|
176
|
+
|
177
|
+
This behaviour can be modified by setting opt `blanks_as_nil` to false.
|
178
|
+
|
179
|
+
``` ruby
|
180
|
+
MyForm = Class.new(Foraneus) { string :name, :blanks_as_nil => false }
|
181
|
+
|
182
|
+
MyForm.parse(:name => '').name
|
183
|
+
# => ''
|
184
|
+
```
|
185
|
+
|
186
|
+
## Default values
|
187
|
+
|
188
|
+
Define fields with default values:
|
189
|
+
|
190
|
+
``` ruby
|
191
|
+
MyForm = Class.new(Foraneus) { string :name, :default => 'Alice' }
|
192
|
+
```
|
193
|
+
|
194
|
+
Parse data from the ouside:
|
195
|
+
|
196
|
+
``` ruby
|
197
|
+
form = MyForm.parse
|
198
|
+
|
199
|
+
form.name # => 'Alice'
|
200
|
+
form[:name] # => nil, data from the outside don't include any value
|
201
|
+
```
|
202
|
+
|
203
|
+
Convert values back from the inside:
|
204
|
+
|
205
|
+
``` ruby
|
206
|
+
form = MyForm.raw
|
207
|
+
|
208
|
+
form[:name] # => 'Alice'
|
209
|
+
form.name # => nil, data from the inside don't include any value
|
210
|
+
```
|
211
|
+
|
145
212
|
## Installation
|
146
213
|
|
147
214
|
- Install `foraneus` as a gem.
|
data/lib/foraneus.rb
CHANGED
@@ -25,8 +25,8 @@ class Foraneus
|
|
25
25
|
# Declares a boolean field.
|
26
26
|
#
|
27
27
|
# @param [Symbol] name The name of the field.
|
28
|
-
def self.boolean(name)
|
29
|
-
converter = Foraneus::Converters::Boolean.new
|
28
|
+
def self.boolean(name, *args)
|
29
|
+
converter = Foraneus::Converters::Boolean.new(*args)
|
30
30
|
field(name, converter)
|
31
31
|
end
|
32
32
|
|
@@ -69,16 +69,16 @@ class Foraneus
|
|
69
69
|
# Declares a noop field.
|
70
70
|
#
|
71
71
|
# @param [Symbol] name The name of the field.
|
72
|
-
def self.noop(name)
|
73
|
-
converter = Foraneus::Converters::Noop.new
|
72
|
+
def self.noop(name, *args)
|
73
|
+
converter = Foraneus::Converters::Noop.new(*args)
|
74
74
|
field(name, converter)
|
75
75
|
end
|
76
76
|
|
77
77
|
# Declares a string field.
|
78
78
|
#
|
79
79
|
# @param [Symbol] name The name of the field.
|
80
|
-
def self.string(name)
|
81
|
-
converter = Foraneus::Converters::String.new
|
80
|
+
def self.string(name, *args)
|
81
|
+
converter = Foraneus::Converters::String.new(*args)
|
82
82
|
field(name, converter)
|
83
83
|
end
|
84
84
|
|
@@ -104,28 +104,28 @@ class Foraneus
|
|
104
104
|
|
105
105
|
# Parses data coming from an external source.
|
106
106
|
#
|
107
|
-
# @param [Hash<Symbol, String>]
|
107
|
+
# @param [Hash<Symbol, String>] data External data.
|
108
108
|
#
|
109
109
|
# @return [Foraneus] An instance of a form.
|
110
|
-
def self.parse(
|
110
|
+
def self.parse(data = {})
|
111
111
|
instance = self.new
|
112
112
|
|
113
113
|
parsed_keys = []
|
114
114
|
|
115
|
-
|
116
|
-
given_key =
|
117
|
-
v =
|
118
|
-
given_key =
|
119
|
-
|
115
|
+
fields.each do |field, converter|
|
116
|
+
given_key = field
|
117
|
+
v = data.fetch(given_key) do
|
118
|
+
given_key = field.to_sym
|
119
|
+
data.fetch(given_key, nil)
|
120
120
|
end
|
121
121
|
|
122
122
|
parsed_keys << given_key
|
123
|
-
__parse_raw_datum(
|
123
|
+
__parse_raw_datum(given_key, v, instance, converter)
|
124
124
|
end
|
125
125
|
|
126
|
-
|
126
|
+
data.each do |k, v|
|
127
127
|
unless parsed_keys.include?(k)
|
128
|
-
|
128
|
+
instance[k] = v
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
@@ -140,10 +140,19 @@ class Foraneus
|
|
140
140
|
def self.raw(data = {})
|
141
141
|
instance = self.new
|
142
142
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
143
|
+
fields.each do |field, converter|
|
144
|
+
given_key = field
|
145
|
+
|
146
|
+
v = data.fetch(given_key) do
|
147
|
+
given_key = field.to_sym
|
148
|
+
data.fetch(given_key, nil)
|
149
|
+
end
|
150
|
+
|
151
|
+
instance.send("#{field}=", v)
|
152
|
+
|
153
|
+
if v.nil?
|
154
|
+
v = converter.opts[:default]
|
155
|
+
end
|
147
156
|
|
148
157
|
s = if v.nil?
|
149
158
|
nil
|
@@ -151,8 +160,8 @@ class Foraneus
|
|
151
160
|
converter.raw(v)
|
152
161
|
end
|
153
162
|
|
154
|
-
instance[
|
155
|
-
instance.data[
|
163
|
+
instance[given_key] = s
|
164
|
+
instance.data[given_key] = v
|
156
165
|
end
|
157
166
|
|
158
167
|
instance
|
@@ -190,21 +199,19 @@ class Foraneus
|
|
190
199
|
|
191
200
|
# @api private
|
192
201
|
#
|
193
|
-
# Parses a
|
202
|
+
# Parses a value and assigns it to the corresponding field.
|
194
203
|
#
|
195
204
|
# It also registers errors if the conversion fails.
|
196
205
|
#
|
197
|
-
# @param [Foraneus] foraneus
|
198
206
|
# @param [String, Symbol] k
|
199
207
|
# @param [String] v
|
200
|
-
|
208
|
+
# @param [Foraneus] foraneus
|
209
|
+
# @param [Converter] converter
|
210
|
+
def self.__parse_raw_datum(k, v, foraneus, converter)
|
201
211
|
field = k.to_s
|
202
|
-
converter = fields[field]
|
203
212
|
|
204
213
|
foraneus[k] = v
|
205
214
|
|
206
|
-
return unless converter
|
207
|
-
|
208
215
|
if v == '' && converter.opts.fetch(:blanks_as_nil, true)
|
209
216
|
v = nil
|
210
217
|
end
|
@@ -212,7 +219,7 @@ class Foraneus
|
|
212
219
|
if v.nil? && converter.opts[:required]
|
213
220
|
raise KeyError, "required parameter not found: #{field.inspect}"
|
214
221
|
elsif v.nil?
|
215
|
-
v =
|
222
|
+
v = converter.opts[:default]
|
216
223
|
else
|
217
224
|
v = converter.parse(v)
|
218
225
|
end
|
data/spec/lib/foraneus_spec.rb
CHANGED
@@ -101,7 +101,7 @@ describe Foraneus do
|
|
101
101
|
it { should be_valid }
|
102
102
|
end
|
103
103
|
|
104
|
-
context 'when a field is declared as
|
104
|
+
context 'when a field is declared as blanks_as_nil = true' do
|
105
105
|
let(:converter) { Foraneus::Converters::String.new(:blanks_as_nil => true) }
|
106
106
|
|
107
107
|
subject(:form) { form_spec.parse(:delay => '') }
|
@@ -178,6 +178,38 @@ describe Foraneus do
|
|
178
178
|
it { should_not be_valid }
|
179
179
|
|
180
180
|
its(:delay) { should be_nil }
|
181
|
+
|
182
|
+
its([:delay]) { should be_nil }
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context 'when default value' do
|
187
|
+
let(:converter) { Foraneus::Converters::Integer.new(:default => 1) }
|
188
|
+
|
189
|
+
subject(:form) { form_spec.parse }
|
190
|
+
|
191
|
+
it { should be_valid }
|
192
|
+
|
193
|
+
its(:delay) { should eq(1) }
|
194
|
+
|
195
|
+
its(:data) { should include(:delay => 1) }
|
196
|
+
|
197
|
+
its([:delay]) { should be_nil}
|
198
|
+
|
199
|
+
its([]) { should include(:delay => nil) }
|
200
|
+
|
201
|
+
its([:errors]) { should_not include(:delay) }
|
202
|
+
|
203
|
+
context 'when missing required field' do
|
204
|
+
let(:converter) { Foraneus::Converters::Integer.new(:default => 1, :required => true) }
|
205
|
+
|
206
|
+
subject(:form) { form_spec.parse }
|
207
|
+
|
208
|
+
it { should_not be_valid }
|
209
|
+
|
210
|
+
its(:delay) { should be_nil }
|
211
|
+
|
212
|
+
its([:delay]) { should be_nil }
|
181
213
|
end
|
182
214
|
end
|
183
215
|
end
|
@@ -220,5 +252,17 @@ describe Foraneus do
|
|
220
252
|
|
221
253
|
its([]) { should include('delay' => nil) }
|
222
254
|
end
|
255
|
+
|
256
|
+
context 'when default value' do
|
257
|
+
let(:converter) { Foraneus::Converters::Integer.new(:default => 1) }
|
258
|
+
|
259
|
+
subject(:form) { form_spec.raw }
|
260
|
+
|
261
|
+
its(:delay) { should be_nil }
|
262
|
+
|
263
|
+
its([:delay]) { should eq('1') }
|
264
|
+
|
265
|
+
its([]) { should include(:delay => '1') }
|
266
|
+
end
|
223
267
|
end
|
224
268
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foraneus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gianfranco Zas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|