servi 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/servi.rb +25 -73
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11896204fcc9e4b7211577129399d9863d657bed
4
- data.tar.gz: 2131f13d63197edde1f10c97fe45105982518f94
3
+ metadata.gz: 6811acf7e53f4885c8132c0d83eded742bf588a7
4
+ data.tar.gz: 9821e8dca4ce04e82cc3c491c5cdc7a2802416b0
5
5
  SHA512:
6
- metadata.gz: 7fe1da5405b9ddafe4dd34516850cab60d6930ee91d0360223958960e3346b806989a3e3e99d749b22444d80c44edb0b231c56b560d2a9f0e8b3766767e1b5f0
7
- data.tar.gz: baa30cf484fd306b3a2d3343d072b368645f70e030e7296c5554cff7d4b8e405f893b3d5009cbb612d819e267c61076fb30188bdbba29b12f77e77597b780ecb
6
+ metadata.gz: 8ec854851433cf72b0d9036f93fbbef5b88616f25baefdfa4e709ae28a43bc1ef88a5e5587e047634d0e253bf8277bd0308597ceb34d82967290c25031bbf035
7
+ data.tar.gz: 35432e25d98b66cf15dcfa21a24d1332083723a85660fc76774e680a48f662590580b08345ba25ef9cc148065f2d97a73358efcffe4c018e30359d4607154ca0
data/lib/servi.rb CHANGED
@@ -1,60 +1,53 @@
1
- require "scrivener"
1
+ require_relative "servi/validations"
2
2
 
3
3
  class Servi
4
- attr :input
4
+ include Servi::Validations
5
5
 
6
- def self.call(user_input, trusted_input)
7
- service = new(user_input)
6
+ attr_reader :errors
8
7
 
9
- trusted_input.each do |key, value|
10
- service.send("#{key}=", value)
11
- end
12
-
13
- service.commit
14
- end
8
+ def self.call(params, trusted = {})
9
+ merged = params.dup
15
10
 
16
- def self.empty_result
17
- Servi::Result.new(:empty, {}, {}, Servi::Errors.new)
18
- end
11
+ trusted.each do |key, value|
12
+ merged[key.to_s] = value
13
+ end
19
14
 
20
- def initialize(input = {})
21
- @input = input
22
- end
15
+ service = new(merged)
23
16
 
24
- def commit
25
- form = self.class.const_get(:Input).new(input, self.validation_context)
17
+ service.validate
26
18
 
27
- if form.valid?
28
- build(form.attributes)
19
+ if service.errors.empty?
20
+ service.build(service.clean)
29
21
  else
30
- error(form.errors)
22
+ service.error(service.errors)
31
23
  end
32
24
  end
33
25
 
34
- # Things you may need when validating the input
35
- def validation_context
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, @input, {}, Errors.new(errors))
36
+ Result.new(:error, @params, {}, errors)
42
37
  end
43
- protected :error
44
38
 
45
39
  def success(output = {})
46
- Result.new(:success, @input, output)
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, input, output, errors = Hash.new { |hash, key| hash[key] = [] })
48
+ def initialize(status, params, output, errors = Hash.new { |hash, key| hash[key] = [] })
56
49
  @status = status
57
- @input = input
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Hanna