solid_use_case 2.0.2 → 2.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ddf3a778dbf1eb61bc2f63319813267790aa806
4
- data.tar.gz: 11b3e39668ca2ca7502255f90999eb2b8181753e
3
+ metadata.gz: 5810571bbc17faba88269dbdbe7f205d1e28d13b
4
+ data.tar.gz: 9576bcb516c93bd5e2f7b71ef1432746c3181f54
5
5
  SHA512:
6
- metadata.gz: 9c6177ff65b30961fce7808ecce96401259926075f8d55340767efc6ff104180728e00365fce5044a24dbf4413b8ae08a8df65229e604e61366f98930c57d589
7
- data.tar.gz: 77ff94dd60cb46f013ae170e92863c1c60f027d8dd74817676b644f6385863ac71039f7353c1e7c7cc799691b1bc273ad6838089823acea803a46b04d67def49
6
+ metadata.gz: 51fbf7ebc22c9ee27484bf6834e00cece81b165b233142d1fde1e7509fac6bddb4c1c050fdf173e59c77cd577d77452c5b6e8a0ecf0e41add6e32bc0bdb2afe5
7
+ data.tar.gz: adf744a459a4b8a70febc3bdcc7a0632751e0f0dfb25c895e38df316b8a1bef9bb37cf98bbb0a657846fa4bd6da07a3b7ee71dfdb39d32487af750003c10e306
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'solid_use_case', '~> 2.0.2'
9
+ gem 'solid_use_case', '~> 2.1.0'
10
10
 
11
11
  And then execute:
12
12
 
@@ -26,7 +26,7 @@ The only thing required is using the `#steps` method:
26
26
 
27
27
  ```ruby
28
28
  class UserSignup
29
- include SolidUseCase::Composable
29
+ include SolidUseCase
30
30
 
31
31
  steps :validate, :save_user, :email_user
32
32
 
@@ -57,7 +57,7 @@ class UserSignup
57
57
  end
58
58
  ```
59
59
 
60
- Now you can run your use case in your controller and easily respond to the different outcomes (with **pattern matching**!):
60
+ Now you can run your use case in your controller and easily respond to the different outcomes (with pattern matching!):
61
61
 
62
62
  ```ruby
63
63
  class UsersController < ApplicationController
@@ -95,6 +95,10 @@ end
95
95
 
96
96
  ## Control Flow Helpers
97
97
 
98
+ Because we're using consistent successes and failures, we can use different functions to gain some nice control flow while avoiding those pesky if-else statements :)
99
+
100
+ ### #check_exists
101
+
98
102
  `check_exists` (alias `maybe_continue`) allows you to implicitly return a failure when a value is nil:
99
103
 
100
104
  ```ruby
@@ -118,6 +122,8 @@ find_tag(tag)
118
122
  end
119
123
  ```
120
124
 
125
+ ### #attempt
126
+
121
127
  `attempt` allows you to catch an exception. It's useful when you want to attempt something that might fail, but don't want to write all that exception-handling boilerplate.
122
128
 
123
129
  `attempt` also **auto-wraps your values**; in other words, the inner code does **not** have to return a success or failure.
@@ -164,7 +170,7 @@ describe MyApp::SignUp do
164
170
  expect(result).to fail_with(:invalid_password)
165
171
 
166
172
  # The above `fail_with` line is equivalent to:
167
- # expect(result.value).to be_a SolidUseCase::Composable::ErrorStruct
173
+ # expect(result.value).to be_a SolidUseCase::Either::ErrorStruct
168
174
  # expect(result.value.type).to eq :invalid_password
169
175
 
170
176
  # You still have access to your arbitrary error data
@@ -1,5 +1,5 @@
1
1
  module SolidUseCase
2
- module Composable
2
+ module Either
3
3
  module ClassMethods
4
4
 
5
5
  def run(input_hash={})
@@ -11,7 +11,7 @@ module SolidUseCase
11
11
  @__steps += args
12
12
  end
13
13
 
14
- def composable?
14
+ def can_run_either?
15
15
  true
16
16
  end
17
17
 
@@ -1,5 +1,5 @@
1
1
  module SolidUseCase
2
- module Composable
2
+ module Either
3
3
  class ErrorStruct < OpenStruct
4
4
  def ==(error_type_symbol)
5
5
  self[:type] == error_type_symbol
File without changes
@@ -1,10 +1,5 @@
1
1
  module SolidUseCase
2
- module Composable
3
-
4
- def self.included(includer)
5
- includer.send :include, Deterministic::CoreExt::Either
6
- includer.extend ClassMethods
7
- end
2
+ module Either
8
3
 
9
4
  def run(inputs)
10
5
  steps = self.class.instance_variable_get("@__steps").clone
@@ -14,7 +9,7 @@ module SolidUseCase
14
9
  while steps.count > 0
15
10
  next_step = steps.shift
16
11
 
17
- if next_step.is_a?(Class) && (next_step.respond_to? :composable?) && next_step.composable?
12
+ if next_step.is_a?(Class) && (next_step.respond_to? :can_run_either?) && next_step.can_run_either?
18
13
  subresult = next_step.run(result.value)
19
14
  elsif next_step.is_a?(Symbol)
20
15
  subresult = self.send(next_step, result.value)
@@ -46,6 +41,16 @@ module SolidUseCase
46
41
  end
47
42
  end
48
43
 
44
+ def catch(required, *exceptions)
45
+ exceptions << required
46
+ result = attempt_all do
47
+ try { yield }
48
+ end
49
+ if result.is_a?(Failure) && exceptions.any?
50
+ raise result.value unless exceptions.include?(result.value)
51
+ end
52
+ end
53
+
49
54
  def fail(type, data={})
50
55
  data[:type] = type
51
56
  Failure(ErrorStruct.new(data))
@@ -17,7 +17,7 @@ module SolidUseCase
17
17
 
18
18
  def failure_message
19
19
  "expected result to be a success\n" +
20
- if @result.value.is_a? SolidUseCase::Composable::ErrorStruct
20
+ if @result.value.is_a? SolidUseCase::Either::ErrorStruct
21
21
  "Error & Data:\n #{@result.value.type} - #{@result.value.inspect}"
22
22
  elsif @result.value.is_a? Exception
23
23
  backtrace = @result.value.backtrace.reject do |file|
@@ -1,3 +1,3 @@
1
1
  module SolidUseCase
2
- VERSION = "2.0.2"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -2,12 +2,17 @@ require "deterministic"
2
2
  require "deterministic/core_ext/either"
3
3
 
4
4
  require "solid_use_case/version"
5
- require 'solid_use_case/composable/class_methods.rb'
6
- require 'solid_use_case/composable/error_struct.rb'
7
- require 'solid_use_case/composable/util.rb'
8
- require 'solid_use_case/composable.rb'
5
+ require 'solid_use_case/either/class_methods.rb'
6
+ require 'solid_use_case/either/error_struct.rb'
7
+ require 'solid_use_case/either/util.rb'
8
+ require 'solid_use_case/either.rb'
9
9
 
10
10
  module SolidUseCase
11
+ def self.included(includer)
12
+ includer.send :include, Deterministic::CoreExt::Either
13
+ includer.send :include, Either
14
+ includer.extend Either::ClassMethods
15
+ end
11
16
  end
12
17
 
13
18
  class Deterministic::Either
@@ -4,7 +4,7 @@ describe "Control Flow Helpers" do
4
4
 
5
5
  describe '#check_exists' do
6
6
  class FloodGate
7
- include SolidUseCase::Composable
7
+ include SolidUseCase
8
8
 
9
9
  def basic(input)
10
10
  check_exists(input).and_then {|x| Success(x * 2) }
@@ -44,7 +44,7 @@ describe "Control Flow Helpers" do
44
44
 
45
45
  describe '#attempt' do
46
46
  class Bubble
47
- include SolidUseCase::Composable
47
+ include SolidUseCase
48
48
 
49
49
  def pop1
50
50
  attempt { "pop!" }
@@ -1,10 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SolidUseCase::Composable do
3
+ describe SolidUseCase::Either do
4
4
 
5
5
  describe 'Stepping' do
6
6
  class GiantSteps
7
- include SolidUseCase::Composable
7
+ include SolidUseCase
8
8
 
9
9
  def run(inputs)
10
10
  attempt_all do
@@ -34,7 +34,7 @@ describe SolidUseCase::Composable do
34
34
 
35
35
  describe 'Stepping DSL' do
36
36
  class GiantStepsDSL
37
- include SolidUseCase::Composable
37
+ include SolidUseCase
38
38
 
39
39
  steps :step_1, :step_2
40
40
 
@@ -63,7 +63,7 @@ describe SolidUseCase::Composable do
63
63
  end
64
64
 
65
65
  class SubStep
66
- include SolidUseCase::Composable
66
+ include SolidUseCase
67
67
  steps GiantStepsDSL, :last_step
68
68
 
69
69
  def last_step(inputs)
@@ -82,7 +82,7 @@ describe SolidUseCase::Composable do
82
82
 
83
83
  describe 'Failure Matching' do
84
84
  class FailureMatch
85
- include SolidUseCase::Composable
85
+ include SolidUseCase
86
86
 
87
87
  def run(inputs)
88
88
  attempt_all do
@@ -101,7 +101,7 @@ describe SolidUseCase::Composable do
101
101
  # Custom RSpec matcher
102
102
  expect(result).to_not be_a_success
103
103
 
104
- expect(result.value).to be_a SolidUseCase::Composable::ErrorStruct
104
+ expect(result.value).to be_a SolidUseCase::Either::ErrorStruct
105
105
  expect(result.value.type).to eq :abc
106
106
 
107
107
  matched = false
@@ -4,21 +4,21 @@ describe 'Custom RSpec Matchers' do
4
4
  include SolidUseCase::RSpecMatchers
5
5
 
6
6
  class FailCase
7
- include SolidUseCase::Composable
7
+ include SolidUseCase
8
8
  def run(error)
9
9
  fail(error)
10
10
  end
11
11
  end
12
12
 
13
13
  class SuccessCase
14
- include SolidUseCase::Composable
14
+ include SolidUseCase
15
15
  def run(val)
16
16
  continue(val)
17
17
  end
18
18
  end
19
19
 
20
20
  class ExceptionCase
21
- include SolidUseCase::Composable
21
+ include SolidUseCase
22
22
  def run(val)
23
23
  attempt_all do
24
24
  try { raise_exception }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solid_use_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilbert
@@ -81,15 +81,15 @@ files:
81
81
  - README.md
82
82
  - Rakefile
83
83
  - lib/solid_use_case.rb
84
- - lib/solid_use_case/composable.rb
85
- - lib/solid_use_case/composable/class_methods.rb
86
- - lib/solid_use_case/composable/error_struct.rb
87
- - lib/solid_use_case/composable/util.rb
84
+ - lib/solid_use_case/either.rb
85
+ - lib/solid_use_case/either/class_methods.rb
86
+ - lib/solid_use_case/either/error_struct.rb
87
+ - lib/solid_use_case/either/util.rb
88
88
  - lib/solid_use_case/rspec_matchers.rb
89
89
  - lib/solid_use_case/version.rb
90
90
  - solid_use_case.gemspec
91
- - spec/composable_spec.rb
92
91
  - spec/control_flow_spec.rb
92
+ - spec/either_spec.rb
93
93
  - spec/rspec_matchers_spec.rb
94
94
  - spec/spec_helper.rb
95
95
  homepage: https://github.com/mindeavor/solid_use_case
@@ -117,8 +117,8 @@ signing_key:
117
117
  specification_version: 4
118
118
  summary: A flexible UseCase pattern that works *with* your workflow, not against it.
119
119
  test_files:
120
- - spec/composable_spec.rb
121
120
  - spec/control_flow_spec.rb
121
+ - spec/either_spec.rb
122
122
  - spec/rspec_matchers_spec.rb
123
123
  - spec/spec_helper.rb
124
124
  has_rdoc: