pay_dirt 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -56
- data/lib/pay_dirt/use_case.rb +6 -1
- data/lib/pay_dirt/version.rb +1 -1
- data/test/unit/pay_dirt/use_case_test.rb +60 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07dc40d695e2b51aa09cac2f1accd89c2cd1579e
|
4
|
+
data.tar.gz: 53388f63da7158e9bb2cfe408699ab35ad8717a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9462187bf0aad679b35c874a2406f30af1b579a46d85b57f6f3adf5d9875f61271555b2c7ae65b027cd14a903086668402ef9d78999732529668d9651005f43
|
7
|
+
data.tar.gz: a70f7bfa4b89d89d96adacd574206ee76e7750df99f3b192cec26b7400ce721f9030e62900c4949191f82cf837dc8b043d7ea1178bfef3da4e2d28f16f33618b
|
data/README.md
CHANGED
@@ -3,60 +3,59 @@
|
|
3
3
|
|
4
4
|
Provides the basic building blocks of a pattern capable of reducing a towering codebase to modular rubble (or more Ruby gems)
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
it "must inherit from PayDirt::Base" do
|
32
|
-
assert_operator @use_case, :<, PayDirt::Base
|
33
|
-
end
|
34
|
-
|
35
|
-
it "must error when initialized without required options" do
|
36
|
-
begin
|
37
|
-
@use_case.new
|
38
|
-
rescue => e
|
39
|
-
e.must_be_kind_of ArgumentError
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
it "can execute successfully" do
|
44
|
-
dependencies = {
|
45
|
-
the_secret_to_life_the_universe_and_everything: 42
|
46
|
-
}
|
47
|
-
|
48
|
-
result = @use_case.new(dependencies).execute!
|
49
|
-
|
50
|
-
result.successful?.must_equal true
|
51
|
-
end
|
52
|
-
|
53
|
-
it "can execute successfully" do
|
54
|
-
dependencies = {
|
55
|
-
the_secret_to_life_the_universe_and_everything: :i_dunno
|
56
|
-
}
|
57
|
-
|
58
|
-
result = @use_case.new(dependencies).execute!
|
59
|
-
|
60
|
-
result.successful?.must_equal false
|
61
|
-
end
|
6
|
+
There are two ways to employ the pattern:
|
7
|
+
|
8
|
+
1. use a class that inherits from [PayDirt::Base](https://github.com/rthbound/pay_dirt/blob/master/test/unit/pay_dirt/base_test.rb#L6-L24)
|
9
|
+
2. use a class or module that includes [PayDirt::UseCase](https://github.com/rthbound/pay_dirt/blob/master/test/unit/pay_dirt/use_case_test.rb#L6-L26)
|
10
|
+
|
11
|
+
### Sample PayDirt use case
|
12
|
+
Example class:
|
13
|
+
```ruby
|
14
|
+
class UseCase
|
15
|
+
include PayDirt::UseCase
|
16
|
+
|
17
|
+
def initialize(options)
|
18
|
+
options = {
|
19
|
+
required_option_with_default_value: true
|
20
|
+
}.merge(options)
|
21
|
+
|
22
|
+
load_options(:required_option_with_default_value, :required_option, options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def execute!
|
26
|
+
if !@required_option_with_default_value
|
27
|
+
return PayDirt::Result.new(data: return_value, success: true)
|
28
|
+
else
|
29
|
+
return PayDirt::Result.new(data: return_value, success: false)
|
62
30
|
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def return_value
|
35
|
+
{
|
36
|
+
optional_option: @optional_option,
|
37
|
+
required_option1: @required_option_with_default_value,
|
38
|
+
required_option2: @required_option
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
Example class usage:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
# Cheating by not injecting all dependencies
|
48
|
+
result = SomeThing.new(required_option: true).execute! # Returns a PayDirt::Result
|
49
|
+
!result.successful? #=> false
|
50
|
+
result.data[:optional_option] #=> nil
|
51
|
+
|
52
|
+
# Playing nice and injecting all required dependencies
|
53
|
+
result = SomeThing.new(required_option: true, required_option_with_default_value: false).execute!
|
54
|
+
result.successful? #=> true
|
55
|
+
result.data[:optional_option] #=> nil
|
56
|
+
|
57
|
+
# Making use of an optional option
|
58
|
+
result = SomeThing.new(required_option: true, optional_option: true).execute!
|
59
|
+
!result.successful? #=> false
|
60
|
+
result.data[:optional_option] #=> true
|
61
|
+
```
|
data/lib/pay_dirt/use_case.rb
CHANGED
@@ -6,10 +6,15 @@ module PayDirt
|
|
6
6
|
instance_variable_set("@#{option}", options.fetch(option.to_sym) {
|
7
7
|
raise "Missing required option: #{option}"
|
8
8
|
})
|
9
|
+
|
10
|
+
options.delete(option)
|
11
|
+
|
12
|
+
return options
|
9
13
|
end
|
10
14
|
|
11
15
|
def load_options(*option_names, options)
|
12
|
-
option_names.each { |o| load_option(o, options) }
|
16
|
+
option_names.each { |o| options = load_option(o, options) }
|
17
|
+
options.each_key { |o| options = load_option(o, options) }
|
13
18
|
end
|
14
19
|
end
|
15
20
|
end
|
data/lib/pay_dirt/version.rb
CHANGED
@@ -3,6 +3,7 @@ require_relative "../../../lib/pay_dirt/use_case"
|
|
3
3
|
|
4
4
|
describe PayDirt::UseCase do
|
5
5
|
before do
|
6
|
+
|
6
7
|
module UseCase
|
7
8
|
class UltimateQuestion
|
8
9
|
include PayDirt::UseCase
|
@@ -17,11 +18,20 @@ describe PayDirt::UseCase do
|
|
17
18
|
|
18
19
|
def execute!
|
19
20
|
if @the_secret_to_life_the_universe_and_everything == 42
|
20
|
-
return PayDirt::Result.new(data:
|
21
|
+
return PayDirt::Result.new(data: return_value, success: true)
|
21
22
|
else
|
22
23
|
return PayDirt::Result.new(data: @the_secret_to_life_the_universe_and_everything, success: false)
|
23
24
|
end
|
24
25
|
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def return_value
|
30
|
+
{
|
31
|
+
secret: @the_secret_to_life_the_universe_and_everything,
|
32
|
+
some_random_option: @some_random_option
|
33
|
+
}
|
34
|
+
end
|
25
35
|
end
|
26
36
|
end
|
27
37
|
|
@@ -39,6 +49,11 @@ describe PayDirt::UseCase do
|
|
39
49
|
@use_case.new({}).must_respond_to :execute!
|
40
50
|
end
|
41
51
|
|
52
|
+
it "loads options that are not required" do
|
53
|
+
result = @use_case.new({ some_random_option: true }).execute!
|
54
|
+
assert result.data[:some_random_option]
|
55
|
+
end
|
56
|
+
|
42
57
|
it "can execute successfully" do
|
43
58
|
dependencies = {
|
44
59
|
}
|
@@ -58,4 +73,48 @@ describe PayDirt::UseCase do
|
|
58
73
|
|
59
74
|
result.successful?.must_equal false
|
60
75
|
end
|
76
|
+
|
77
|
+
it "has optional options" do
|
78
|
+
class SomeThing
|
79
|
+
include PayDirt::UseCase
|
80
|
+
|
81
|
+
def initialize(options)
|
82
|
+
options = {
|
83
|
+
required_option_with_default_value: true
|
84
|
+
}.merge(options)
|
85
|
+
|
86
|
+
load_options(:required_option_with_default_value, :required_option, options)
|
87
|
+
end
|
88
|
+
|
89
|
+
def execute!
|
90
|
+
if !@required_option_with_default_value
|
91
|
+
return PayDirt::Result.new(data: return_value, success: true)
|
92
|
+
else
|
93
|
+
return PayDirt::Result.new(data: return_value, success: false)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
def return_value
|
99
|
+
{
|
100
|
+
optional_option: @optional_option,
|
101
|
+
required_option1: @required_option_with_default_value,
|
102
|
+
required_option2: @required_option
|
103
|
+
}
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Cheating by not injecting all dependencies
|
108
|
+
result = SomeThing.new(required_option: true).execute! # Returns a PayDirt::Result
|
109
|
+
assert !result.successful? #=> false
|
110
|
+
result.data[:optional_option].must_be_nil #=> nil
|
111
|
+
# Playing nice and injecting all required dependencies
|
112
|
+
result = SomeThing.new(required_option: true, required_option_with_default_value: false).execute!
|
113
|
+
assert result.successful? #=> true
|
114
|
+
result.data[:optional_option].must_be_nil #=> nil
|
115
|
+
# Making use of an optional option
|
116
|
+
result = SomeThing.new(required_option: true, optional_option: true).execute!
|
117
|
+
assert !result.successful? #=> false
|
118
|
+
assert result.data[:optional_option] #=> true
|
119
|
+
end
|
61
120
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pay_dirt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tad Hosford
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|