foraneus 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +93 -10
- data/lib/foraneus.rb +8 -1
- data/spec/lib/foraneus/converters/decimal_converter_spec.rb +1 -1
- data/spec/lib/foraneus/converters/float_converter_spec.rb +1 -1
- data/spec/lib/foraneus/converters/integer_converter_spec.rb +1 -1
- data/spec/lib/foraneus_spec.rb +10 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MTdlYTI4OGY2Y2M4N2I3ODAxOTUyOThjYjYyYzA1MDlkNWMyZDliNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWM3NWRlYzIyZGEzMzYyNDVmMGEzMWZiYWIzMzY2YmI4NWY0MjhmMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MmQyNTdlNmFmNjNiNGM4MWIxYjcyMmExNWIxNDQxODA0MTdkNzFiMGFhMTVk
|
10
|
+
M2E3ZWVhNzRmM2Q5ZDBkOGU3OTEzZDVjMzU1YTk3NGRhZTEyYWRkNDU3Yzc3
|
11
|
+
Njc2N2Y0Mjk5NWYzNTBmM2UwNWUxNjgxMTVhMjk0YzNjNWZiZWU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YmUyYjQyZDNjZWI1NzllOTNhZTVkZjZmMjBiZDkyMjFhOTQ5OGE1MmU4NTYx
|
14
|
+
MTYzYjMwMWY4OTAwY2YzODQzNzg1YTJhMDE2MTYxNmFmYTQ1ZjYxNzBlZGM3
|
15
|
+
ZjU3ZjMzMGJjNWQ2Y2IwNTY1YmJkZDU5ZGU4N2NlYjBhNzFhMmE=
|
data/README.md
CHANGED
@@ -1,8 +1,17 @@
|
|
1
1
|
# Foraneus
|
2
2
|
|
3
|
-
|
3
|
+
Foraneus allows to:
|
4
|
+
- parse data coming from external sources (like an HTTP request).
|
5
|
+
- convert data back to a raw representation suitable for being used at the outside, like an HTML
|
6
|
+
form.
|
7
|
+
|
8
|
+
No matter which source of data is fed into Foraneus (external or internal), any instance can return
|
9
|
+
raw and parsed data.
|
10
|
+
|
11
|
+
## Basic usage
|
4
12
|
|
5
13
|
- Declaration:
|
14
|
+
|
6
15
|
``` ruby
|
7
16
|
class MyForm < Foraneus
|
8
17
|
integer :delay
|
@@ -26,12 +35,23 @@
|
|
26
35
|
form[] # => { :delay => '5', :duration => '2.14' }
|
27
36
|
```
|
28
37
|
|
38
|
+
- From the inside:
|
39
|
+
|
29
40
|
``` ruby
|
30
|
-
form.
|
31
|
-
form.errors # => {}
|
41
|
+
form = MyForm.raw(:delay => 5, :duration => 2.14)
|
32
42
|
```
|
33
43
|
|
34
|
-
|
44
|
+
``` ruby
|
45
|
+
form.delay # => 5
|
46
|
+
form[:delay] # => '5'
|
47
|
+
```
|
48
|
+
|
49
|
+
``` ruby
|
50
|
+
form.data # => { :delay => 5, :duration => 2.14 }
|
51
|
+
form[] # => { :delay => '5', :duration => '2.14' }
|
52
|
+
```
|
53
|
+
|
54
|
+
- When no data:
|
35
55
|
|
36
56
|
``` ruby
|
37
57
|
form = MyForm.new
|
@@ -42,22 +62,85 @@
|
|
42
62
|
form[:delay] # => nil
|
43
63
|
```
|
44
64
|
|
45
|
-
|
65
|
+
## Declaration
|
66
|
+
|
67
|
+
Declare source classes by inheriting from `Foraneus` base class.
|
46
68
|
|
47
69
|
``` ruby
|
48
|
-
|
70
|
+
class MyForm < Foraneus
|
71
|
+
field :delay, SomeCustomConverter.new
|
72
|
+
float :duration
|
73
|
+
end
|
49
74
|
```
|
50
75
|
|
76
|
+
Fields are declared in two ways:
|
77
|
+
|
78
|
+
- calling `.field`
|
79
|
+
- calling a shortcut method, like `.float`
|
80
|
+
|
81
|
+
|
82
|
+
There are handy methods for any of the built-in converters: boolean, date, decimal, float, integer,
|
83
|
+
noop, and string.
|
84
|
+
|
85
|
+
When no converter is passed to `.field`, Foraneus::Converters::Noop is assigned to the declared
|
86
|
+
field.
|
87
|
+
|
88
|
+
## Converters
|
89
|
+
|
90
|
+
Converters have two interrelated responsibilities:
|
91
|
+
|
92
|
+
- Parse data, like the string `"3,000"`, into an object, `like 3_000`.
|
93
|
+
- Serialize data, like integer `3_000`, into string `"3,000"`
|
94
|
+
|
95
|
+
A converter is simply an object that responds to `#parse(s)` and `#raw(v)` methods.
|
96
|
+
|
97
|
+
When `#parse(s)` raises a StandardError exception, or any of its descendants, the exception is
|
98
|
+
rescued and a Foraneus::Error instance is added to `Foraneus#errors` map.
|
99
|
+
|
100
|
+
Built-in converters:
|
101
|
+
|
102
|
+
- Boolean
|
103
|
+
- Date
|
104
|
+
- Decimal
|
105
|
+
- Float
|
106
|
+
- Integer
|
107
|
+
- Noop
|
108
|
+
- String
|
109
|
+
|
110
|
+
## Validations
|
111
|
+
|
112
|
+
Foraneus only validates that external data can be converted to the specified types. Smart
|
113
|
+
validations, like date range inclusion, are out of the scope of this gem.
|
114
|
+
|
115
|
+
`#valid?` and `#errors` are handy methods that tell whether a Foraneus instance is valid or not.
|
116
|
+
|
117
|
+
Valid instance:
|
118
|
+
|
51
119
|
``` ruby
|
52
|
-
form.
|
53
|
-
form[:
|
120
|
+
form.valid? # => true
|
121
|
+
form[:errors] # => {}
|
54
122
|
```
|
55
123
|
|
124
|
+
Invalid one:
|
56
125
|
``` ruby
|
57
|
-
form
|
58
|
-
|
126
|
+
form = MyForm.parse(:delay => 'INVALID')
|
127
|
+
|
128
|
+
form.valid? # => false
|
129
|
+
|
130
|
+
form[:errors][:delay].key # => 'ArgumentError'
|
131
|
+
form[:errors][:delay].message # => 'invalid value for Integer(): "INVALID"'
|
59
132
|
```
|
60
133
|
|
134
|
+
`#errors` is a map in which keys correspond to field names, and values are instances of
|
135
|
+
`Foraneus::Error`.
|
136
|
+
|
137
|
+
The name of the exception raised by `#parse` is to the error's `key` attribute, and the exception's
|
138
|
+
message is added to the error's `message` attribute.
|
139
|
+
|
140
|
+
|
141
|
+
Data coming from the inside is assumed to be valid, so `.raw` won't return an instance having
|
142
|
+
errors neither being invalid.
|
143
|
+
|
61
144
|
## Installation
|
62
145
|
|
63
146
|
- Install `foraneus` as a gem.
|
data/lib/foraneus.rb
CHANGED
@@ -15,11 +15,13 @@ class Foraneus
|
|
15
15
|
|
16
16
|
attr_accessor :data
|
17
17
|
|
18
|
-
def initialize
|
18
|
+
def initialize(data = {})
|
19
19
|
@data = {}
|
20
20
|
@raw_data = {}
|
21
21
|
|
22
22
|
@errors = {}
|
23
|
+
|
24
|
+
self.class.send(:__raw, self, data)
|
23
25
|
end
|
24
26
|
|
25
27
|
def self.boolean(name)
|
@@ -107,6 +109,10 @@ class Foraneus
|
|
107
109
|
def self.raw(data)
|
108
110
|
instance = self.new
|
109
111
|
|
112
|
+
__raw(instance, data)
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.__raw(instance, data)
|
110
116
|
data.each do |k, v|
|
111
117
|
next unless fields.has_key?(k.to_s)
|
112
118
|
instance.send("#{k}=", v)
|
@@ -124,6 +130,7 @@ class Foraneus
|
|
124
130
|
|
125
131
|
instance
|
126
132
|
end
|
133
|
+
private_class_method :__raw
|
127
134
|
|
128
135
|
def [](m = nil)
|
129
136
|
if m == :errors
|
data/spec/lib/foraneus_spec.rb
CHANGED
@@ -20,6 +20,16 @@ describe Foraneus do
|
|
20
20
|
its(:data) { should be_empty }
|
21
21
|
|
22
22
|
its([]) { should be_empty }
|
23
|
+
|
24
|
+
context 'when initial data' do
|
25
|
+
subject(:form) { form_spec.new(:delay => 5) }
|
26
|
+
|
27
|
+
its(:delay) { should eq(5) }
|
28
|
+
its([:delay]) { should eq('5') }
|
29
|
+
|
30
|
+
its(:data) { should include(:delay => 5) }
|
31
|
+
its([]) { should include(:delay => '5') }
|
32
|
+
end
|
23
33
|
end
|
24
34
|
|
25
35
|
describe '.parse' do
|
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.6
|
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-05-
|
11
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
description: Provides
|
27
|
+
description: Provides two way transformation mechanisms to external data.
|
28
28
|
email: snmgian@gmail.com
|
29
29
|
executables: []
|
30
30
|
extensions: []
|
@@ -74,7 +74,7 @@ rubyforge_project:
|
|
74
74
|
rubygems_version: 2.1.5
|
75
75
|
signing_key:
|
76
76
|
specification_version: 4
|
77
|
-
summary:
|
77
|
+
summary: Transforms external data.
|
78
78
|
test_files:
|
79
79
|
- spec/lib/foraneus_spec.rb
|
80
80
|
- spec/lib/foraneus/converters/noop_converter_spec.rb
|