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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7ff86dfbc9179c07a08070206bc43f1715d0e36
4
- data.tar.gz: eab83e620f4897c405cf4f3fa5a0cb949978da7e
3
+ metadata.gz: 07dc40d695e2b51aa09cac2f1accd89c2cd1579e
4
+ data.tar.gz: 53388f63da7158e9bb2cfe408699ab35ad8717a4
5
5
  SHA512:
6
- metadata.gz: 75104e49d44c143d955932427e8e89cb6b4b05ba1e7c7840228d32889f500eb7d05c2e51476c00b2b6849d0e11692fef058292d64bcda6c6a1bf2436d5dd5951
7
- data.tar.gz: 5560575c53412632cc24eedfc015c0d81cb38d67bab4084f9fc7c75c87d105092afed5a91aefddb8c06b25b99e78f0b4004e706ff2cae753c6444eff7480f7b4
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
- The [pay_dirt/base_test](https://github.com/rthbound/pay_dirt/blob/master/test/unit/pay_dirt/base_test.rb) provides a complete working example. It's all plain old Ruby, but you can require any dependency you like to be injected as an option via [load_options](https://github.com/rthbound/pay_dirt/blob/master/test/unit/pay_dirt/base_test.rb#L8)
7
-
8
- describe PayDirt::Base do
9
- before do
10
-
11
- # A sample use case
12
- module UseCase
13
- class UltimateQuestion < PayDirt::Base
14
- def initialize(options)
15
- load_options(:the_secret_to_life_the_universe_and_everything, options)
16
- end
17
-
18
- def execute!
19
- if @the_secret_to_life_the_universe_and_everything == 42
20
- return PayDirt::Result.new(data: @the_secret_to_life_the_universe_and_everything, success: true)
21
- else
22
- return PayDirt::Result.new(data: @the_secret_to_life_the_universe_and_everything, success: false)
23
- end
24
- end
25
- end
26
- end
27
-
28
- @use_case = UseCase::UltimateQuestion
29
- end
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
+ ```
@@ -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
@@ -1,3 +1,3 @@
1
1
  module PayDirt
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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: @the_secret_to_life_the_universe_and_everything, success: true)
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.2
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-28 00:00:00.000000000 Z
11
+ date: 2013-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest