servi 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/servi.rb +25 -73
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6811acf7e53f4885c8132c0d83eded742bf588a7
|
4
|
+
data.tar.gz: 9821e8dca4ce04e82cc3c491c5cdc7a2802416b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ec854851433cf72b0d9036f93fbbef5b88616f25baefdfa4e709ae28a43bc1ef88a5e5587e047634d0e253bf8277bd0308597ceb34d82967290c25031bbf035
|
7
|
+
data.tar.gz: 35432e25d98b66cf15dcfa21a24d1332083723a85660fc76774e680a48f662590580b08345ba25ef9cc148065f2d97a73358efcffe4c018e30359d4607154ca0
|
data/lib/servi.rb
CHANGED
@@ -1,60 +1,53 @@
|
|
1
|
-
|
1
|
+
require_relative "servi/validations"
|
2
2
|
|
3
3
|
class Servi
|
4
|
-
|
4
|
+
include Servi::Validations
|
5
5
|
|
6
|
-
|
7
|
-
service = new(user_input)
|
6
|
+
attr_reader :errors
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
service.commit
|
14
|
-
end
|
8
|
+
def self.call(params, trusted = {})
|
9
|
+
merged = params.dup
|
15
10
|
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
trusted.each do |key, value|
|
12
|
+
merged[key.to_s] = value
|
13
|
+
end
|
19
14
|
|
20
|
-
|
21
|
-
@input = input
|
22
|
-
end
|
15
|
+
service = new(merged)
|
23
16
|
|
24
|
-
|
25
|
-
form = self.class.const_get(:Input).new(input, self.validation_context)
|
17
|
+
service.validate
|
26
18
|
|
27
|
-
if
|
28
|
-
build(
|
19
|
+
if service.errors.empty?
|
20
|
+
service.build(service.clean)
|
29
21
|
else
|
30
|
-
error(
|
22
|
+
service.error(service.errors)
|
31
23
|
end
|
32
24
|
end
|
33
25
|
|
34
|
-
|
35
|
-
|
36
|
-
{}
|
26
|
+
def initialize(params)
|
27
|
+
@params = params
|
28
|
+
@errors = Hash.new { |hash, key| hash[key] = [] }
|
29
|
+
end
|
30
|
+
|
31
|
+
def get(attr)
|
32
|
+
@params[attr.to_s]
|
37
33
|
end
|
38
|
-
protected :validation_context
|
39
34
|
|
40
35
|
def error(errors)
|
41
|
-
Result.new(:error, @
|
36
|
+
Result.new(:error, @params, {}, errors)
|
42
37
|
end
|
43
|
-
protected :error
|
44
38
|
|
45
39
|
def success(output = {})
|
46
|
-
Result.new(:success, @
|
40
|
+
Result.new(:success, @params, output)
|
47
41
|
end
|
48
|
-
protected :success
|
49
42
|
|
50
43
|
class Result
|
51
|
-
attr :input
|
52
44
|
attr :output
|
45
|
+
attr :params
|
53
46
|
attr :errors
|
54
47
|
|
55
|
-
def initialize(status,
|
48
|
+
def initialize(status, params, output, errors = Hash.new { |hash, key| hash[key] = [] })
|
56
49
|
@status = status
|
57
|
-
@
|
50
|
+
@params = params
|
58
51
|
@output = output
|
59
52
|
@errors = errors
|
60
53
|
end
|
@@ -67,45 +60,4 @@ class Servi
|
|
67
60
|
@output.fetch(key)
|
68
61
|
end
|
69
62
|
end
|
70
|
-
|
71
|
-
class Input < ::Scrivener
|
72
|
-
def initialize(atts, context)
|
73
|
-
@context = context
|
74
|
-
super(atts)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
class Errors
|
79
|
-
def initialize(errors={})
|
80
|
-
@errors = errors
|
81
|
-
end
|
82
|
-
|
83
|
-
def empty?
|
84
|
-
@errors.empty?
|
85
|
-
end
|
86
|
-
|
87
|
-
def on(att, name)
|
88
|
-
errors = lookup(att)
|
89
|
-
error = errors && errors.include?(name)
|
90
|
-
|
91
|
-
if block_given?
|
92
|
-
yield if error
|
93
|
-
else
|
94
|
-
error
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def any?(att)
|
99
|
-
errors = lookup(att)
|
100
|
-
errors && errors.any?
|
101
|
-
end
|
102
|
-
|
103
|
-
def lookup(atts)
|
104
|
-
Array(atts).inject(@errors) { |err, att| err && !err[att].empty? && err[att] }
|
105
|
-
end
|
106
|
-
|
107
|
-
def [](att)
|
108
|
-
@errors[att]
|
109
|
-
end
|
110
|
-
end
|
111
63
|
end
|