pay_dirt 1.0.0 → 1.0.1

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: 2ba73dd9244ee21438def9485c64bbefaf5f16fc
4
- data.tar.gz: 800d06d810a814e537a7e7cbe8990a98e44e6bd8
3
+ metadata.gz: 0b0248574c00871f3ef6676055ad9fb7c48acd35
4
+ data.tar.gz: 8337f0b5fe6377dfaa00183b62465ecec055f17a
5
5
  SHA512:
6
- metadata.gz: 808c005f50e9eeaefc148900d19d3d34dfe37784bdac0feeb9505496f4a131e4fcf23c39e618463bc27ab46a10621ea600e73c731b9d834de06f047af97edd91
7
- data.tar.gz: 7e1c8b35bec2fd5d1096712c6ecdb9f7f9dbeb65d01607ea12a3cd250924a07d466b6d88e956c44bcbf98aa903fa5e4aaec23b5f14fbae5974dffba22dc50f42
6
+ metadata.gz: 7f21b11cf6ae2914e06fdc48c7ae2e7e3de1f6f4f6f47ea79ecbc22ea3ce6cb8e1325e380e7bb97ed4237333810b820cb6d9d1d017c3c51adda455dec8e5ff4d
7
+ data.tar.gz: bc92bbb77e64ad90bad2c5a0a521174d5b6f38c0e3ae827927f62f8d1698ca89f9d2d4a43d8b88557739e10c026123f95e23e3d26b0ebfd0de9c276aeb640283
data/.gitignore CHANGED
@@ -9,5 +9,7 @@
9
9
  .ruby-gemset
10
10
  /.bundle
11
11
 
12
+ lib/service_objects
13
+
12
14
  *.sw?
13
15
  .sw?
data/README.md CHANGED
@@ -4,62 +4,53 @@
4
4
 
5
5
  Provides the basic building blocks of a pattern capable of reducing a towering codebase to modular rubble (or more Ruby gems)
6
6
 
7
- There are two ways to employ the pattern:
7
+ There are two ways to employ the pattern:
8
8
 
9
9
  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)
10
10
  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)
11
11
 
12
- ### Sample PayDirt use case
13
- Example class:
14
- ```ruby
15
- class UseCase
16
- include PayDirt::UseCase
12
+ ### Generators
17
13
 
18
- def initialize(options)
19
- options = {
20
- required_option_with_default_value: true
21
- }.merge(options)
14
+ PayDirt now provides a service object generator, powered by [thor](https://github.com/erikhuda/thor). It takes a few options
22
15
 
23
- load_options(:required_option_with_default_value, :required_option, options)
24
- end
16
+ `--dependencies` or `-d` : An array of required dependencies (this option is required)
25
17
 
26
- def execute!
27
- if !@required_option_with_default_value
28
- return PayDirt::Result.new(data: return_value, success: true)
29
- else
30
- return PayDirt::Result.new(data: return_value, success: false)
31
- end
32
- end
18
+ `--defaults` or `-D` : An optional hash of default values for dependencies
33
19
 
34
- private
35
- def return_value
36
- {
37
- optional_option: @optional_option,
38
- required_option1: @required_option_with_default_value,
39
- required_option2: @required_option
40
- }
41
- end
42
- end
43
- ```
20
+ `--inherit` or `-i` : A boolean flag, raise it for an implementation that inherits from `PayDirt::Base` (this is default behavior)
21
+
22
+ `--include` or `-m` : A boolean flag, raise it for an implementation that includes `PayDirt::UseCase`
23
+
24
+ Example:
44
25
 
45
- Example class usage:
26
+ ```
27
+ $ thor pay_dirt:service_object:new digit_check -d fingers toes -D fingers:10 toes:10
28
+ create lib/service_objects/digit_check.rb
29
+ ```
46
30
 
31
+ Running the above generator will create the following file
47
32
  ```ruby
48
- # Cheating by not injecting all dependencies
49
- result = SomeThing.new(required_option: true).execute! # Returns a PayDirt::Result
50
- !result.successful? #=> false
51
- result.data[:optional_option] #=> nil
52
-
53
- # Playing nice and injecting all required dependencies
54
- result = SomeThing.new(required_option: true, required_option_with_default_value: false).execute!
55
- result.successful? #=> true
56
- result.data[:optional_option] #=> nil
57
-
58
- # Making use of an optional option
59
- result = SomeThing.new(required_option: true, optional_option: true).execute!
60
- !result.successful? #=> false
61
- result.data[:optional_option] #=> true
33
+ require 'pay_dirt'
34
+
35
+ module ServiceObjects
36
+ class DigitCheck < PayDirt::Base
37
+ def initialize(options = {})
38
+ options = {
39
+ fingers: 10,
40
+ toes: 10,
41
+ }.merge(options)
42
+
43
+ load_options(:fingers, :toes, options)
44
+ end
45
+
46
+ def execute!
47
+ return PayDirt::Result.new(success: true, data: nil)
48
+ end
49
+ end
50
+ end
62
51
  ```
52
+ We can now call `ServiceObjects::DigitCheck.new(fingers: 10, toes: 10).execute!`
53
+ and see a successful return object. Where you take it from there is up to you.
63
54
 
64
55
  ### Other examples
65
56
  1. [rubeuler](https://github.com/rthbound/rubeuler)
@@ -1,3 +1,3 @@
1
1
  module PayDirt
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/pay_dirt.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.require_paths = ["lib"]
26
26
 
27
27
  s.add_development_dependency "minitest"
28
+ s.add_development_dependency "thor"
28
29
  s.add_development_dependency "coveralls"
29
30
  s.add_development_dependency "rake"
30
31
  end
data/pay_dirt.thor ADDED
@@ -0,0 +1,86 @@
1
+ module PayDirt
2
+ class ServiceObject < Thor
3
+ include Thor::Actions
4
+
5
+ desc "new FILE", "create a service object"
6
+ method_option :defaults,
7
+ type: :hash,
8
+ aliases: "-D",
9
+ desc: "Specify default dependencies"
10
+
11
+ method_option :dependencies,
12
+ type: :array,
13
+ aliases: "-d",
14
+ desc: "Specify required dependencies",
15
+ required: true
16
+
17
+ method_option :inherit,
18
+ type: :boolean,
19
+ desc: "Should inherit from PayDirt::Base (default)",
20
+ aliases: "-i",
21
+ default: true,
22
+ lazy_default: true
23
+
24
+ method_option :include,
25
+ type: :boolean,
26
+ desc: "Should include the PayDirt::UseCase module",
27
+ aliases: "-m",
28
+ lazy_default: true
29
+
30
+ def new(file)
31
+ rets = ""
32
+ class_names = file.split("/").map { |str| str.split("_").map{ |s| (s[0].upcase + s[1..-1]) }.join("") }
33
+ defaults = options[:defaults] || []
34
+ dependencies = options[:dependencies]
35
+
36
+ create_file "lib/service_objects/#{file}.rb" do
37
+ rets << "require 'pay_dirt'\n\n"
38
+ rets << "module ServiceObjects\n"
39
+ class_names[0..-2].each_with_index do |mod,i|
40
+ rets << (" " * ( (i+1) * 2 )) + "module #{mod}\n"
41
+ end
42
+
43
+ klass, klass_index = class_names[-1], class_names.length
44
+
45
+ if options[:include]
46
+ rets << (" " * ( klass_index * 2)) + "class #{class_names[-1]}\n"
47
+ rets << (" " * ((klass_index + 1) * 2)) + "include PayDirt::UseCase\n"
48
+ elsif options[:inherit]
49
+ rets << (" " * ( klass_index * 2)) + "class #{class_names[-1]} < PayDirt::Base\n"
50
+ end
51
+
52
+ inner_index = klass_index + 1
53
+
54
+ # The initialize method
55
+ rets << (" " * ( inner_index * 2 )) + "def initialize(options = {})\n"
56
+
57
+ # Configure dependencies' default values
58
+ if options[:defaults]
59
+ rets << (" " * ( (inner_index + 1) * 2 )) + "options = {\n"
60
+
61
+ defaults.each do |k,v|
62
+ rets << (" " * ( (inner_index + 2) * 2 )) + "#{k}: #{v}" + ",\n"
63
+ end
64
+
65
+ rets << (" " * ( (inner_index + 1) * 2 )) + "}.merge(options)\n\n"
66
+ end
67
+
68
+ rets << (" " * ( (inner_index + 1) * 2 )) + "load_options(:#{dependencies.join(', :')}, options)\n"
69
+ rets << (" " * ( inner_index * 2 )) + "end\n\n"
70
+
71
+ # The execute! method
72
+ rets << (" " * ( inner_index * 2 )) + "def execute!\n"
73
+ rets << (" " * ( (inner_index + 1) * 2 )) + "return PayDirt::Result.new(success: true, data: nil)\n"
74
+ rets << (" " * ( inner_index * 2 )) + "end\n"
75
+
76
+ rets << (" " * ( klass_index * 2)) + "end\n"
77
+
78
+ class_names[0..-1].each_with_index do |mod,i|
79
+ rets << (" " * ( (class_names.length - (i + 1)).abs * 2 )) + "end\n"
80
+ end
81
+
82
+ rets
83
+ end
84
+ end
85
+ end
86
+ 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: 1.0.0
4
+ version: 1.0.1
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-06-07 00:00:00.000000000 Z
11
+ date: 2013-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: coveralls
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -73,6 +87,7 @@ files:
73
87
  - lib/pay_dirt/use_case.rb
74
88
  - lib/pay_dirt/version.rb
75
89
  - pay_dirt.gemspec
90
+ - pay_dirt.thor
76
91
  - test/test_helper.rb
77
92
  - test/unit/.gitkeep
78
93
  - test/unit/pay_dirt/base_test.rb