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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41f5f9c69cd88716d3d05c109a89f1988eecd25e581de70af22e4c5f349800bd
4
- data.tar.gz: a3f42179b0ac6eedf9bfe014bad4b24d91aa053b3e78d87927c0e1865031835b
3
+ metadata.gz: d9aa8d1755bf0535f2daef94a23feca6c60b0ce322ac18b61207b820ea693633
4
+ data.tar.gz: b1f9fa5f84161a955e550564906deefc249709a7b2e1032d246413f53cc9d067
5
5
  SHA512:
6
- metadata.gz: 3093f3e717c8f27db52f71b3002e26a46de8051c51e04c558db2d39d2224bbcc9aa3ccce9da0ad0fc772e6285d3b8cf55a8e8e8b2fa52c7552eed08a2d149982
7
- data.tar.gz: 3a74a02377cbda5a874634ff090ee93012750309fd455aa88f595b8b537178bd0d9aa72522899e0071a94832b7665731195d39090e232639c676bb43be3dffce
6
+ metadata.gz: 5f97929bfb8fbfc6a154b4e80991183fbdf043a86ccd2b78bd800fc1c7c68cbdd8389e8840186007d8c3b5b89a8f2516f8b6c75bd8408ef3084edabdb4281b9c
7
+ data.tar.gz: 887aa03c1d48f8d304bcf599cb5352e4462e54b647f53697b7a1860d38c21f0a38abef0f164eeff159498845d36f38518c309fe0eea2cc07b0ddab866369ac5e
@@ -1 +1 @@
1
- 2.5.3
1
+ 2.6.6
@@ -2,6 +2,9 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## Version 0.4.0
6
+ * Support ActiveModel::Errors as argument to error message.
7
+
5
8
  ## Version 0.3.0
6
9
  * Support `.err!` and `.ok!` public methods.
7
10
  * Rename `.ok` to `.ok_result`. [Breaking]
@@ -1,18 +1,30 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- keka (0.3.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 (10.5.0)
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 (~> 10.0)
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.
@@ -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", "~> 10.0"
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
@@ -1,17 +1,39 @@
1
1
  module Keka
2
2
  class Result
3
- attr_accessor :msg
4
-
5
- def initialize(is_success, msg)
3
+ def initialize(is_success, msg_or_errors)
6
4
  @is_success = is_success
7
- @msg = msg
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
@@ -1,3 +1,3 @@
1
1
  module Keka
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.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: 2020-05-08 00:00:00.000000000 Z
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: '10.0'
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: '10.0'
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.2
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