servi 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/servi.rb +111 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 11896204fcc9e4b7211577129399d9863d657bed
4
+ data.tar.gz: 2131f13d63197edde1f10c97fe45105982518f94
5
+ SHA512:
6
+ metadata.gz: 7fe1da5405b9ddafe4dd34516850cab60d6930ee91d0360223958960e3346b806989a3e3e99d749b22444d80c44edb0b231c56b560d2a9f0e8b3766767e1b5f0
7
+ data.tar.gz: baa30cf484fd306b3a2d3343d072b368645f70e030e7296c5554cff7d4b8e405f893b3d5009cbb612d819e267c61076fb30188bdbba29b12f77e77597b780ecb
data/lib/servi.rb ADDED
@@ -0,0 +1,111 @@
1
+ require "scrivener"
2
+
3
+ class Servi
4
+ attr :input
5
+
6
+ def self.call(user_input, trusted_input)
7
+ service = new(user_input)
8
+
9
+ trusted_input.each do |key, value|
10
+ service.send("#{key}=", value)
11
+ end
12
+
13
+ service.commit
14
+ end
15
+
16
+ def self.empty_result
17
+ Servi::Result.new(:empty, {}, {}, Servi::Errors.new)
18
+ end
19
+
20
+ def initialize(input = {})
21
+ @input = input
22
+ end
23
+
24
+ def commit
25
+ form = self.class.const_get(:Input).new(input, self.validation_context)
26
+
27
+ if form.valid?
28
+ build(form.attributes)
29
+ else
30
+ error(form.errors)
31
+ end
32
+ end
33
+
34
+ # Things you may need when validating the input
35
+ def validation_context
36
+ {}
37
+ end
38
+ protected :validation_context
39
+
40
+ def error(errors)
41
+ Result.new(:error, @input, {}, Errors.new(errors))
42
+ end
43
+ protected :error
44
+
45
+ def success(output = {})
46
+ Result.new(:success, @input, output)
47
+ end
48
+ protected :success
49
+
50
+ class Result
51
+ attr :input
52
+ attr :output
53
+ attr :errors
54
+
55
+ def initialize(status, input, output, errors = Hash.new { |hash, key| hash[key] = [] })
56
+ @status = status
57
+ @input = input
58
+ @output = output
59
+ @errors = errors
60
+ end
61
+
62
+ def ok?
63
+ @status == :success
64
+ end
65
+
66
+ def [](key)
67
+ @output.fetch(key)
68
+ end
69
+ 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
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: servi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Hanna
8
+ - Damian Janowski
9
+ - Lucas Tolchinsky
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-05-23 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description:
16
+ email:
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/servi.rb
22
+ homepage: http://github.com/kevinjhanna/servi
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Form, errors and results hanlder
46
+ test_files: []