heartwood-service_object 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +134 -0
- data/LICENSE.txt +21 -0
- data/README.md +167 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/heartwood-service_object.gemspec +38 -0
- data/lib/generators/heartwood/service_object_generator.rb +19 -0
- data/lib/generators/heartwood/templates/service_object.erb +16 -0
- data/lib/heartwood/service_object.rb +9 -0
- data/lib/heartwood/service_object/base.rb +92 -0
- data/lib/heartwood/service_object/version.rb +5 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b69cb63a835386d2c9fd0ea2bcc6e8a11acc1c67
|
4
|
+
data.tar.gz: 60611760b60548b1f691d2faabe7d35d7a270937
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: daa25d7662d4ae5ed810d9dac8cda0b011c509141e11a0345900533f6228246ed22d086aa2e55d0009a1338baeacf0c7a45efa6478544d1b8d69fa017facc0e3
|
7
|
+
data.tar.gz: a160b762e00117b24587dcf7dd7bdc99e1c915c20bc97a7743c7247c1576d0f2ed243338b2995c101094e1580e5d12a7e8e69e6099115eb99af0ab5c1a5cc4e8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at scdavis41@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
heartwood-service_object (0.1.0)
|
5
|
+
rails (~> 5.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (5.1.5)
|
11
|
+
actionpack (= 5.1.5)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (~> 0.6.1)
|
14
|
+
actionmailer (5.1.5)
|
15
|
+
actionpack (= 5.1.5)
|
16
|
+
actionview (= 5.1.5)
|
17
|
+
activejob (= 5.1.5)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 2.0)
|
20
|
+
actionpack (5.1.5)
|
21
|
+
actionview (= 5.1.5)
|
22
|
+
activesupport (= 5.1.5)
|
23
|
+
rack (~> 2.0)
|
24
|
+
rack-test (>= 0.6.3)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
+
actionview (5.1.5)
|
28
|
+
activesupport (= 5.1.5)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubi (~> 1.4)
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
+
activejob (5.1.5)
|
34
|
+
activesupport (= 5.1.5)
|
35
|
+
globalid (>= 0.3.6)
|
36
|
+
activemodel (5.1.5)
|
37
|
+
activesupport (= 5.1.5)
|
38
|
+
activerecord (5.1.5)
|
39
|
+
activemodel (= 5.1.5)
|
40
|
+
activesupport (= 5.1.5)
|
41
|
+
arel (~> 8.0)
|
42
|
+
activesupport (5.1.5)
|
43
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
44
|
+
i18n (~> 0.7)
|
45
|
+
minitest (~> 5.1)
|
46
|
+
tzinfo (~> 1.1)
|
47
|
+
arel (8.0.0)
|
48
|
+
builder (3.2.3)
|
49
|
+
concurrent-ruby (1.0.5)
|
50
|
+
crass (1.0.3)
|
51
|
+
diff-lcs (1.3)
|
52
|
+
erubi (1.7.1)
|
53
|
+
globalid (0.4.1)
|
54
|
+
activesupport (>= 4.2.0)
|
55
|
+
i18n (0.9.5)
|
56
|
+
concurrent-ruby (~> 1.0)
|
57
|
+
loofah (2.2.0)
|
58
|
+
crass (~> 1.0.2)
|
59
|
+
nokogiri (>= 1.5.9)
|
60
|
+
mail (2.7.0)
|
61
|
+
mini_mime (>= 0.1.1)
|
62
|
+
method_source (0.9.0)
|
63
|
+
mini_mime (1.0.0)
|
64
|
+
mini_portile2 (2.3.0)
|
65
|
+
minitest (5.11.3)
|
66
|
+
nio4r (2.3.0)
|
67
|
+
nokogiri (1.8.2)
|
68
|
+
mini_portile2 (~> 2.3.0)
|
69
|
+
rack (2.0.4)
|
70
|
+
rack-test (0.8.3)
|
71
|
+
rack (>= 1.0, < 3)
|
72
|
+
rails (5.1.5)
|
73
|
+
actioncable (= 5.1.5)
|
74
|
+
actionmailer (= 5.1.5)
|
75
|
+
actionpack (= 5.1.5)
|
76
|
+
actionview (= 5.1.5)
|
77
|
+
activejob (= 5.1.5)
|
78
|
+
activemodel (= 5.1.5)
|
79
|
+
activerecord (= 5.1.5)
|
80
|
+
activesupport (= 5.1.5)
|
81
|
+
bundler (>= 1.3.0)
|
82
|
+
railties (= 5.1.5)
|
83
|
+
sprockets-rails (>= 2.0.0)
|
84
|
+
rails-dom-testing (2.0.3)
|
85
|
+
activesupport (>= 4.2.0)
|
86
|
+
nokogiri (>= 1.6)
|
87
|
+
rails-html-sanitizer (1.0.3)
|
88
|
+
loofah (~> 2.0)
|
89
|
+
railties (5.1.5)
|
90
|
+
actionpack (= 5.1.5)
|
91
|
+
activesupport (= 5.1.5)
|
92
|
+
method_source
|
93
|
+
rake (>= 0.8.7)
|
94
|
+
thor (>= 0.18.1, < 2.0)
|
95
|
+
rake (10.5.0)
|
96
|
+
rspec (3.7.0)
|
97
|
+
rspec-core (~> 3.7.0)
|
98
|
+
rspec-expectations (~> 3.7.0)
|
99
|
+
rspec-mocks (~> 3.7.0)
|
100
|
+
rspec-core (3.7.1)
|
101
|
+
rspec-support (~> 3.7.0)
|
102
|
+
rspec-expectations (3.7.0)
|
103
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
104
|
+
rspec-support (~> 3.7.0)
|
105
|
+
rspec-mocks (3.7.0)
|
106
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
107
|
+
rspec-support (~> 3.7.0)
|
108
|
+
rspec-support (3.7.1)
|
109
|
+
sprockets (3.7.1)
|
110
|
+
concurrent-ruby (~> 1.0)
|
111
|
+
rack (> 1, < 3)
|
112
|
+
sprockets-rails (3.2.1)
|
113
|
+
actionpack (>= 4.0)
|
114
|
+
activesupport (>= 4.0)
|
115
|
+
sprockets (>= 3.0.0)
|
116
|
+
thor (0.20.0)
|
117
|
+
thread_safe (0.3.6)
|
118
|
+
tzinfo (1.2.5)
|
119
|
+
thread_safe (~> 0.1)
|
120
|
+
websocket-driver (0.6.5)
|
121
|
+
websocket-extensions (>= 0.1.0)
|
122
|
+
websocket-extensions (0.1.3)
|
123
|
+
|
124
|
+
PLATFORMS
|
125
|
+
ruby
|
126
|
+
|
127
|
+
DEPENDENCIES
|
128
|
+
bundler (~> 1.16)
|
129
|
+
heartwood-service_object!
|
130
|
+
rake (~> 10.0)
|
131
|
+
rspec (~> 3.0)
|
132
|
+
|
133
|
+
BUNDLED WITH
|
134
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Sean C Davis
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
Heartwood::ServiceObject
|
2
|
+
==========
|
3
|
+
|
4
|
+
Heartwood's service object gem provides a simple DSL for working with service objects within your Rails app.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
----------
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'heartwood-service_object'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install heartwood-service_object
|
22
|
+
|
23
|
+
Usage
|
24
|
+
----------
|
25
|
+
|
26
|
+
You can generate a new service object from the command line:
|
27
|
+
|
28
|
+
$ bundle exec rails g heartwood:service_object do_stuff
|
29
|
+
|
30
|
+
`do_stuff` should be replace with the name of your service. It can be written in snake case or camel case.
|
31
|
+
|
32
|
+
This example would create an empty service object file in `app/services/do_stuff_service.rb`. That file would specify the class name for the service object, which in this case would be `DoStuffService`.
|
33
|
+
|
34
|
+
Within your app, you can call the service using the `call` class method and passing it any options (see below).
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
DoStuffService.call
|
38
|
+
```
|
39
|
+
|
40
|
+
While `call` is a class method, it is mapped to pass the options to a new instance as `DoStuffService.new(options).call`. This is the point at which your options are set.
|
41
|
+
|
42
|
+
Therefore, while you should call the `call` class method on your service object, your executional code should be placed in the `call` instance method. (See below for a simple example.)
|
43
|
+
|
44
|
+
### Options
|
45
|
+
|
46
|
+
There are three types of options which we'll refer to as _attributes_:
|
47
|
+
|
48
|
+
1. Required attributes
|
49
|
+
2. Optional attributes
|
50
|
+
3. Attribute with default values
|
51
|
+
|
52
|
+
#### Required Attributes
|
53
|
+
|
54
|
+
Required attributes use the `required_attr` keyword and can accept a list of all required attributes:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class DoStuffService < Heartwood::ServiceObject::Base
|
58
|
+
required_attr :name, :email
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
You would then be required to include these attributes when calling the service.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# This will not work:
|
66
|
+
DoStuffService.call # => ArgumentError: Missing required option: name
|
67
|
+
|
68
|
+
# Instead, do this:
|
69
|
+
DoStuffService.call(name: 'Mr. F', email: 'mrf@example.com') # => nil
|
70
|
+
```
|
71
|
+
|
72
|
+
These attributes are then available anywhere in your service as the name you specified.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class DoStuffService < Heartwood::ServiceObject::Base
|
76
|
+
required_attr :name, :email
|
77
|
+
|
78
|
+
def call
|
79
|
+
name
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Call the service from elsewhere in your application:
|
84
|
+
DoStuffService.call(name: 'Mr. F', email: 'mrf@example.com') # => "Mr. F"
|
85
|
+
```
|
86
|
+
|
87
|
+
#### Optional Attributes
|
88
|
+
|
89
|
+
Optional attributes use the `optional_attr` method and take the same approach as required attributes, except an error won't be thrown when the attribute does not exist.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class DoStuffService < Heartwood::ServiceObject::Base
|
93
|
+
optional_attr :name
|
94
|
+
|
95
|
+
def call
|
96
|
+
name
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Call the service from elsewhere in your application:
|
101
|
+
DoStuffService.call(name: 'Mr. F') # => nil
|
102
|
+
```
|
103
|
+
|
104
|
+
#### Attributes with Default Values
|
105
|
+
|
106
|
+
You can also have an option with a fallback value via the `attr_with_default` method.
|
107
|
+
|
108
|
+
For these attributes, you'll have to use the `attr_with_default` method for each attribute and can not chain attributes together.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class DoStuffService < Heartwood::ServiceObject::Base
|
112
|
+
attr_with_default :name, 'Mr. F'
|
113
|
+
attr_with_default :email, 'mrf@example.com'
|
114
|
+
|
115
|
+
def call
|
116
|
+
name
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Call the service from elsewhere in your application:
|
121
|
+
DoStuffService.call # => 'Mr F.'
|
122
|
+
|
123
|
+
# Setting the attribute would override the default:
|
124
|
+
DoStuffService.call(name: 'Mr. P') # => 'Mr. P'
|
125
|
+
```
|
126
|
+
|
127
|
+
### Example
|
128
|
+
|
129
|
+
Here's an example that would create a user. It assumes there is a `User` class that has `email`, `password`, and `name` attributes.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
class DoStuffService < Heartwood::ServiceObject::Base
|
133
|
+
|
134
|
+
required_attr :email, :password
|
135
|
+
|
136
|
+
optional_attr :name
|
137
|
+
|
138
|
+
def call
|
139
|
+
User.create(email: email, password: password, name: name)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# Call the service from elsewhere in your application:
|
144
|
+
DoStuffService.call(email: 'mrf@example.com', password: 'password') # => #<User:0x007fb4b5ae3078>
|
145
|
+
```
|
146
|
+
|
147
|
+
Development
|
148
|
+
----------
|
149
|
+
|
150
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
151
|
+
|
152
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
153
|
+
|
154
|
+
Contributing
|
155
|
+
----------
|
156
|
+
|
157
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/seancdavis/heartwood-service_object. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
158
|
+
|
159
|
+
License
|
160
|
+
----------
|
161
|
+
|
162
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
163
|
+
|
164
|
+
Code of Conduct
|
165
|
+
----------
|
166
|
+
|
167
|
+
Everyone interacting in the Heartwood::ServiceObject project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/seancdavis/heartwood-service_object/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "heartwood/service_object"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "heartwood/service_object/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "heartwood-service_object"
|
8
|
+
spec.version = Heartwood::ServiceObject::VERSION
|
9
|
+
spec.authors = ["Sean C Davis"]
|
10
|
+
spec.email = ["scdavis41@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple service object DSL for Rails apps}
|
13
|
+
spec.description = %q{DRY up your service object code by inheriting from Heartwood's base object, which provides a few key helpers.}
|
14
|
+
spec.homepage = "https://github.com/seancdavis/heartwood-service_object"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
|
+
|
37
|
+
spec.add_dependency 'rails', '~> 5.1'
|
38
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Heartwood
|
4
|
+
class ServiceObjectGenerator < Rails::Generators::Base
|
5
|
+
|
6
|
+
desc "Create a new service object for your app."
|
7
|
+
|
8
|
+
argument :name, required: true
|
9
|
+
|
10
|
+
source_root File.expand_path("../templates", __FILE__)
|
11
|
+
|
12
|
+
def create_service_object
|
13
|
+
filename = "#{name.underscore.chomp('_service')}_service.rb"
|
14
|
+
@class_name = "#{name.classify.chomp('Service')}Service"
|
15
|
+
template 'service_object.erb', "app/services/#{filename}"
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class <%= @class_name %> < Heartwood::ServiceObject::Base
|
2
|
+
|
3
|
+
# Add options here. See https://github.com/seancdavis/heartwood-service_object
|
4
|
+
# for more information.
|
5
|
+
#
|
6
|
+
# required_attr :a, :b, :c
|
7
|
+
#
|
8
|
+
# optional_attr :d, :e
|
9
|
+
#
|
10
|
+
# attr_with_default :f, 'default_value'
|
11
|
+
|
12
|
+
def call
|
13
|
+
# Do something awesome ...
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module Heartwood
|
2
|
+
module ServiceObject
|
3
|
+
class Base
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
verify_options(options)
|
7
|
+
process_options(options)
|
8
|
+
process_required_attrs(options)
|
9
|
+
process_attrs_with_defaults(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
# ---------------------------------------- Required Attributes
|
13
|
+
|
14
|
+
def self.required_attrs
|
15
|
+
@required_attrs ||= []
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.required_attr(*names)
|
19
|
+
attr_accessor(*names)
|
20
|
+
required_attrs << names
|
21
|
+
required_attrs.flatten!.uniq!
|
22
|
+
end
|
23
|
+
|
24
|
+
# ---------------------------------------- Optional Attributes
|
25
|
+
|
26
|
+
def self.optional_attrs
|
27
|
+
@optional_attrs ||= []
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.optional_attr(*names)
|
31
|
+
attr_accessor(*names)
|
32
|
+
optional_attrs << names
|
33
|
+
optional_attrs.flatten!.uniq!
|
34
|
+
end
|
35
|
+
|
36
|
+
# ---------------------------------------- Attributes With Default Values
|
37
|
+
|
38
|
+
def self.attrs_with_defaults
|
39
|
+
@attrs_with_defaults ||= []
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.attr_with_default(name, default_value)
|
43
|
+
attr_accessor name
|
44
|
+
attrs_with_defaults << [name, default_value]
|
45
|
+
end
|
46
|
+
|
47
|
+
# ---------------------------------------- Base Calling Methods
|
48
|
+
|
49
|
+
def self.call(options = {})
|
50
|
+
new(options).call
|
51
|
+
end
|
52
|
+
|
53
|
+
def call
|
54
|
+
raise 'The call method is missing in the service object.'
|
55
|
+
end
|
56
|
+
|
57
|
+
# ---------------------------------------- Private Methods
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def process_required_attrs(_options)
|
62
|
+
self.class.required_attrs.each do |name|
|
63
|
+
if send(name).nil?
|
64
|
+
raise ArgumentError, "Missing required option: #{name}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def process_attrs_with_defaults(_options)
|
70
|
+
self.class.attrs_with_defaults.each do |name, default_value|
|
71
|
+
send("#{name}=", default_value) if send(name).blank?
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def process_options(options)
|
76
|
+
options.each do |name, value|
|
77
|
+
begin
|
78
|
+
send("#{name}=", value) if send(name).blank?
|
79
|
+
rescue NoMethodError
|
80
|
+
raise "Option not permitted: #{name}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def verify_options(options)
|
86
|
+
return true if options.is_a?(Hash)
|
87
|
+
raise ArgumentError, 'Must pass options as a hash.'
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heartwood-service_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean C Davis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.1'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.1'
|
69
|
+
description: DRY up your service object code by inheriting from Heartwood's base object,
|
70
|
+
which provides a few key helpers.
|
71
|
+
email:
|
72
|
+
- scdavis41@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
88
|
+
- heartwood-service_object.gemspec
|
89
|
+
- lib/generators/heartwood/service_object_generator.rb
|
90
|
+
- lib/generators/heartwood/templates/service_object.erb
|
91
|
+
- lib/heartwood/service_object.rb
|
92
|
+
- lib/heartwood/service_object/base.rb
|
93
|
+
- lib/heartwood/service_object/version.rb
|
94
|
+
homepage: https://github.com/seancdavis/heartwood-service_object
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata:
|
98
|
+
allowed_push_host: https://rubygems.org
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.5.2
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Simple service object DSL for Rails apps
|
119
|
+
test_files: []
|