solid_use_case 1.1.0 → 2.0.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
  SHA1:
3
- metadata.gz: 4e7056ae1ef3157772218479526874fa94e710f7
4
- data.tar.gz: fd9a2327b8919bfd623d72e7fbe66ec6e0ced364
3
+ metadata.gz: bc18c2e9f16bf546cfc9d56f5337456213bbbe51
4
+ data.tar.gz: 1e7c6aa6fd01fdd35095d2fc0d3e066b28237361
5
5
  SHA512:
6
- metadata.gz: 0dcee4954826b006d3c3bda5516536db3db27c1e1eca0c7731029daec17cd00a6122f8846f48237387e3fe45d81a81da2f8998a71b0fa9d3c287cea9c77d5a76
7
- data.tar.gz: 24dab826516eab7bfe84842e89c3361ac06c039edfbbe0d82c226fd339ebbc843bc24a96878c431f59071a8d5637f1aab307fa3215cbc3447fb58fa23861516d
6
+ metadata.gz: 41793a7fef1201f2ab179b85f7d55b8e91df49ef9fcd180c4fc6b11fe1fff29e0dd2aa01910903653dce8b0ff607f11adc5e316aa8631184096fc88ff462a562
7
+ data.tar.gz: a4e35588c5d5def51e02dabb961b45ffb0dfe6450e19efdc11c7a24f793b47811a9dd203095f23aa280a5653248b3ffb5ef248d7c367931f7e358dbf104887a8
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', '~> 1.1.0'
9
+ gem 'solid_use_case', '~> 2.0.0'
10
10
 
11
11
  And then execute:
12
12
 
@@ -20,12 +20,13 @@ Or install it yourself as:
20
20
 
21
21
  At its core, this library is a light wrapper around [Deterministic](https://github.com/pzol/deterministic), a practical abstraction over the Either monad. Don't let that scare you - you don't have to understand monad theory to reap its benefits.
22
22
 
23
- The only required method is the `#run` method.
23
+ The only thing required is using the `#steps` method:
24
24
 
25
25
  ### Rails Example
26
26
 
27
27
  ```ruby
28
- class UserSignup < SolidUseCase::Base
28
+ class UserSignup
29
+ include SolidUseCase::Composable
29
30
 
30
31
  steps :validate, :save_user, :email_user
31
32
 
@@ -92,6 +93,45 @@ class UsersController < ApplicationController
92
93
  end
93
94
  ```
94
95
 
96
+ ## RSpec Matchers
97
+
98
+ If you're using RSpec, Solid Use Case provides some helpful matchers for testing.
99
+
100
+ First you mix them them into RSpec:
101
+
102
+ ```ruby
103
+ # In your spec_helper.rb
104
+ require 'solid_use_case'
105
+ require 'solid_use_case/rspec_matchers'
106
+
107
+ RSpec.configure do |config|
108
+ config.include(SolidUseCase::RSpecMatchers)
109
+ end
110
+ ```
111
+
112
+ And then you can use the matchers, with helpful error messages:
113
+
114
+ ```ruby
115
+ describe MyApp::SignUp do
116
+ it "runs successfully" do
117
+ result = MyApp::SignUp.run(:username => 'alice', :password => '123123')
118
+ expect(result).to be_a_success
119
+ end
120
+
121
+ it "fails when password is too short" do
122
+ result = MyApp::SignUp.run(:username => 'alice', :password => '5')
123
+ expect(result).to fail_with(:invalid_password)
124
+
125
+ # The above `fail_with` line is equivalent to:
126
+ # expect(result.value).to be_a SolidUseCase::Composable::ErrorStruct
127
+ # expect(result.value.type).to eq :invalid_password
128
+
129
+ # You still have access to your arbitrary error data
130
+ expect(result.value.something).to eq 'whatever'
131
+ end
132
+ end
133
+ ```
134
+
95
135
  ## Testing
96
136
 
97
137
  $ bundle exec rspec
@@ -0,0 +1,20 @@
1
+ module SolidUseCase
2
+ module Composable
3
+ module ClassMethods
4
+
5
+ def run(input_hash={})
6
+ self.new.run(input_hash)
7
+ end
8
+
9
+ def steps(*args)
10
+ @__steps ||= []
11
+ @__steps += args
12
+ end
13
+
14
+ def composable?
15
+ true
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module SolidUseCase
2
+ module Composable
3
+ class ErrorStruct < OpenStruct
4
+ def ==(error_type_symbol)
5
+ self[:type] == error_type_symbol
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module SolidUseCase
2
- module BaseUtil
2
+ module Util
3
3
 
4
4
  def symbolize_names(object)
5
5
  case object
@@ -1,15 +1,9 @@
1
1
  module SolidUseCase
2
- class Base
3
- include Deterministic::CoreExt::Either
4
- include BaseUtil
2
+ module Composable
5
3
 
6
- def self.run(input_hash={})
7
- self.new.run(input_hash)
8
- end
9
-
10
- def self.steps(*args)
11
- @__steps ||= []
12
- @__steps += args
4
+ def self.included(includer)
5
+ includer.send :include, Deterministic::CoreExt::Either
6
+ includer.extend ClassMethods
13
7
  end
14
8
 
15
9
  def run(inputs)
@@ -20,7 +14,7 @@ module SolidUseCase
20
14
  while steps.count > 0
21
15
  next_step = steps.shift
22
16
 
23
- if next_step.is_a?(Class) && (next_step < SolidUseCase::Base)
17
+ if next_step.is_a?(Class) && (next_step.respond_to? :composable?) && next_step.composable?
24
18
  subresult = next_step.run(result.value)
25
19
  elsif next_step.is_a?(Symbol)
26
20
  subresult = self.send(next_step, result.value)
@@ -17,7 +17,7 @@ module SolidUseCase
17
17
 
18
18
  def failure_message_for_should
19
19
  "expected result to be a success\n" +
20
- if @result.value.is_a? SolidUseCase::ErrorStruct
20
+ if @result.value.is_a? SolidUseCase::Composable::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 = "1.1.0"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -2,9 +2,10 @@ require "deterministic"
2
2
  require "deterministic/core_ext/either"
3
3
 
4
4
  require "solid_use_case/version"
5
- require 'solid_use_case/base/util.rb'
6
- require 'solid_use_case/base/error_struct.rb'
7
- require 'solid_use_case/base.rb'
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'
8
9
 
9
10
  module SolidUseCase
10
11
  end
@@ -1,9 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SolidUseCase::Base do
3
+ describe SolidUseCase::Composable do
4
4
 
5
5
  describe 'Stepping' do
6
- class GiantSteps < SolidUseCase::Base
6
+ class GiantSteps
7
+ include SolidUseCase::Composable
8
+
7
9
  def run(inputs)
8
10
  attempt_all do
9
11
  step { step_1(inputs) }
@@ -31,7 +33,8 @@ describe SolidUseCase::Base do
31
33
 
32
34
 
33
35
  describe 'Stepping DSL' do
34
- class GiantStepsDSL < SolidUseCase::Base
36
+ class GiantStepsDSL
37
+ include SolidUseCase::Composable
35
38
 
36
39
  steps :step_1, :step_2
37
40
 
@@ -59,7 +62,8 @@ describe SolidUseCase::Base do
59
62
  expect(result.value[:number]).to eq(40)
60
63
  end
61
64
 
62
- class SubStep < SolidUseCase::Base
65
+ class SubStep
66
+ include SolidUseCase::Composable
63
67
  steps GiantStepsDSL, :last_step
64
68
 
65
69
  def last_step(inputs)
@@ -77,7 +81,9 @@ describe SolidUseCase::Base do
77
81
 
78
82
 
79
83
  describe 'Failure Matching' do
80
- class FailureMatch < SolidUseCase::Base
84
+ class FailureMatch
85
+ include SolidUseCase::Composable
86
+
81
87
  def run(inputs)
82
88
  attempt_all do
83
89
  step { fail_it(inputs) }
@@ -95,7 +101,7 @@ describe SolidUseCase::Base do
95
101
  # Custom RSpec matcher
96
102
  expect(result).to_not be_a_success
97
103
 
98
- expect(result.value).to be_a SolidUseCase::ErrorStruct
104
+ expect(result.value).to be_a SolidUseCase::Composable::ErrorStruct
99
105
  expect(result.value.type).to eq :abc
100
106
 
101
107
  matched = false
@@ -3,19 +3,22 @@ require 'spec_helper'
3
3
  describe 'Custom RSpec Matchers' do
4
4
  include SolidUseCase::RSpecMatchers
5
5
 
6
- class FailCase < SolidUseCase::Base
6
+ class FailCase
7
+ include SolidUseCase::Composable
7
8
  def run(error)
8
9
  fail(error)
9
10
  end
10
11
  end
11
12
 
12
- class SuccessCase < SolidUseCase::Base
13
+ class SuccessCase
14
+ include SolidUseCase::Composable
13
15
  def run(val)
14
16
  continue(val)
15
17
  end
16
18
  end
17
19
 
18
- class ExceptionCase < SolidUseCase::Base
20
+ class ExceptionCase
21
+ include SolidUseCase::Composable
19
22
  def run(val)
20
23
  attempt_all do
21
24
  try { raise_exception }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solid_use_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-22 00:00:00.000000000 Z
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deterministic
@@ -81,13 +81,14 @@ files:
81
81
  - README.md
82
82
  - Rakefile
83
83
  - lib/solid_use_case.rb
84
- - lib/solid_use_case/base.rb
85
- - lib/solid_use_case/base/error_struct.rb
86
- - lib/solid_use_case/base/util.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
87
88
  - lib/solid_use_case/rspec_matchers.rb
88
89
  - lib/solid_use_case/version.rb
89
90
  - solid_use_case.gemspec
90
- - spec/base_spec.rb
91
+ - spec/composable_spec.rb
91
92
  - spec/rspec_matchers_spec.rb
92
93
  - spec/spec_helper.rb
93
94
  homepage: https://github.com/mindeavor/solid_use_case
@@ -115,7 +116,7 @@ signing_key:
115
116
  specification_version: 4
116
117
  summary: A flexible UseCase pattern that works *with* your workflow, not against it.
117
118
  test_files:
118
- - spec/base_spec.rb
119
+ - spec/composable_spec.rb
119
120
  - spec/rspec_matchers_spec.rb
120
121
  - spec/spec_helper.rb
121
122
  has_rdoc:
@@ -1,7 +0,0 @@
1
- module SolidUseCase
2
- class ErrorStruct < OpenStruct
3
- def ==(error_type_symbol)
4
- self[:type] == error_type_symbol
5
- end
6
- end
7
- end