keka 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +3 -0
- data/Gemfile.lock +19 -3
- data/README.md +35 -0
- data/keka.gemspec +2 -2
- data/lib/keka/result.rb +26 -4
- data/lib/keka/version.rb +1 -1
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9aa8d1755bf0535f2daef94a23feca6c60b0ce322ac18b61207b820ea693633
|
4
|
+
data.tar.gz: b1f9fa5f84161a955e550564906deefc249709a7b2e1032d246413f53cc9d067
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f97929bfb8fbfc6a154b4e80991183fbdf043a86ccd2b78bd800fc1c7c68cbdd8389e8840186007d8c3b5b89a8f2516f8b6c75bd8408ef3084edabdb4281b9c
|
7
|
+
data.tar.gz: 887aa03c1d48f8d304bcf599cb5352e4462e54b647f53697b7a1860d38c21f0a38abef0f164eeff159498845d36f38518c309fe0eea2cc07b0ddab866369ac5e
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.6.6
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,18 +1,30 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
keka (0.
|
4
|
+
keka (0.4.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
activemodel (6.1.0)
|
10
|
+
activesupport (= 6.1.0)
|
11
|
+
activesupport (6.1.0)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 1.6, < 2)
|
14
|
+
minitest (>= 5.1)
|
15
|
+
tzinfo (~> 2.0)
|
16
|
+
zeitwerk (~> 2.3)
|
9
17
|
coderay (1.1.2)
|
18
|
+
concurrent-ruby (1.1.7)
|
10
19
|
diff-lcs (1.3)
|
20
|
+
i18n (1.8.5)
|
21
|
+
concurrent-ruby (~> 1.0)
|
11
22
|
method_source (0.9.2)
|
23
|
+
minitest (5.14.2)
|
12
24
|
pry (0.12.2)
|
13
25
|
coderay (~> 1.1.0)
|
14
26
|
method_source (~> 0.9.0)
|
15
|
-
rake (
|
27
|
+
rake (13.0.1)
|
16
28
|
rspec (3.9.0)
|
17
29
|
rspec-core (~> 3.9.0)
|
18
30
|
rspec-expectations (~> 3.9.0)
|
@@ -26,15 +38,19 @@ GEM
|
|
26
38
|
diff-lcs (>= 1.2.0, < 2.0)
|
27
39
|
rspec-support (~> 3.9.0)
|
28
40
|
rspec-support (3.9.2)
|
41
|
+
tzinfo (2.0.4)
|
42
|
+
concurrent-ruby (~> 1.0)
|
43
|
+
zeitwerk (2.4.2)
|
29
44
|
|
30
45
|
PLATFORMS
|
31
46
|
ruby
|
32
47
|
|
33
48
|
DEPENDENCIES
|
49
|
+
activemodel
|
34
50
|
bundler (~> 1.16)
|
35
51
|
keka!
|
36
52
|
pry
|
37
|
-
rake (~>
|
53
|
+
rake (~> 13.0)
|
38
54
|
rspec (~> 3.7)
|
39
55
|
|
40
56
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -120,6 +120,41 @@ def invalid_example
|
|
120
120
|
end
|
121
121
|
```
|
122
122
|
|
123
|
+
### ActiveRecord (ActiveModel) support
|
124
|
+
|
125
|
+
One of the most common boundary conditions is validation.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
class User
|
129
|
+
validates :name, :city, presence: true
|
130
|
+
end
|
131
|
+
|
132
|
+
if User.new(name: nil, city: nil).valid?
|
133
|
+
# do something
|
134
|
+
else
|
135
|
+
# return errors
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
139
|
+
Keka supports `ActiveModel::Errors` as `msg` argument to return full message and errors per fields
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
user = User.new(name: nil, city: nil)
|
143
|
+
|
144
|
+
result = Keka.run do
|
145
|
+
Keka.err_unless! user.valid?, user.errors
|
146
|
+
end
|
147
|
+
|
148
|
+
puts result.ok?
|
149
|
+
# => false
|
150
|
+
|
151
|
+
puts result.msg
|
152
|
+
# => "Name can't be blank, City can't be blank"
|
153
|
+
|
154
|
+
puts result.errors
|
155
|
+
# => {name: ["can't be blank"], city: ["can't be blank"]}
|
156
|
+
```
|
157
|
+
|
123
158
|
## Development
|
124
159
|
|
125
160
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/keka.gemspec
CHANGED
@@ -33,8 +33,8 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.require_paths = ["lib"]
|
34
34
|
|
35
35
|
spec.add_development_dependency "bundler", "~> 1.16"
|
36
|
-
spec.add_development_dependency "rake", "~>
|
36
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
37
37
|
spec.add_development_dependency "rspec", "~> 3.7"
|
38
38
|
spec.add_development_dependency 'pry'
|
39
|
-
|
39
|
+
spec.add_development_dependency 'activemodel'
|
40
40
|
end
|
data/lib/keka/result.rb
CHANGED
@@ -1,17 +1,39 @@
|
|
1
1
|
module Keka
|
2
2
|
class Result
|
3
|
-
|
4
|
-
|
5
|
-
def initialize(is_success, msg)
|
3
|
+
def initialize(is_success, msg_or_errors)
|
6
4
|
@is_success = is_success
|
7
|
-
@
|
5
|
+
@msg_or_errors = msg_or_errors
|
8
6
|
end
|
9
7
|
|
10
8
|
def ok?
|
11
9
|
is_success
|
12
10
|
end
|
13
11
|
|
12
|
+
def msg=(msg_or_errors)
|
13
|
+
@msg_or_errors = msg_or_errors
|
14
|
+
end
|
15
|
+
|
16
|
+
def msg
|
17
|
+
return @msg_or_errors if @msg_or_errors.is_a?(String)
|
18
|
+
return @msg_or_errors.full_messages.join(', ') if active_model_error?
|
19
|
+
|
20
|
+
@msg_or_errors
|
21
|
+
end
|
22
|
+
|
23
|
+
def errors
|
24
|
+
return {} if ok?
|
25
|
+
return {base: [@msg_or_errors]} if @msg_or_errors.is_a?(String)
|
26
|
+
return @msg_or_errors.messages if active_model_error?
|
27
|
+
|
28
|
+
{}
|
29
|
+
end
|
30
|
+
|
14
31
|
private
|
15
32
|
attr_reader :is_success
|
33
|
+
|
34
|
+
def active_model_error?
|
35
|
+
Object.const_defined?("ActiveModel") &&
|
36
|
+
@msg_or_errors.is_a?(ActiveModel::Errors)
|
37
|
+
end
|
16
38
|
end
|
17
39
|
end
|
data/lib/keka/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: keka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zino
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '13.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '13.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activemodel
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: No more ad hoc passage of boolean results and error messages.
|
70
84
|
email:
|
71
85
|
- zinocodes@gmail.com
|
@@ -112,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
126
|
- !ruby/object:Gem::Version
|
113
127
|
version: '0'
|
114
128
|
requirements: []
|
115
|
-
rubygems_version: 3.0.
|
129
|
+
rubygems_version: 3.0.3
|
116
130
|
signing_key:
|
117
131
|
specification_version: 4
|
118
132
|
summary: Better handle short-circuit logic, result state, and return message
|