sebastian 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: de693a44f27ac0fef9f80d070dbd78ff25539d449649de9da9851751f7928c9a
4
+ data.tar.gz: afaa439a236276acc8b8abcfb1d3f3378bbfeaa1e1d1845d4beefb3051f872f7
5
+ SHA512:
6
+ metadata.gz: e32393427345236bdbfdd5cd2b13bd301f9bf80084899837a3b9667fcd0ba076898058b43d1bd746554c37af2f61de57a8d8ab1e69bc2122546134e0db84ecf8
7
+ data.tar.gz: 8ce143e656e546682d45dd50635c05c926974f891f4da5307a952b31eabe209b3b68f2b8cc95104b478ff71a98db273f61f05d276304767cb7dda005e04e9ee0
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ # OSX
16
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ Metrics/LineLength:
4
+ Enabled: true
5
+ Max: 120
6
+ Style/FrozenStringLiteralComment:
7
+ Description: 'Ruby 3.0 transition for frozen string literals'
8
+ Enabled: false
9
+ Style/BlockLength:
10
+ Exclude:
11
+ - 'spec/**/*.rb'
12
+ Documentation:
13
+ Exclude:
14
+ - 'spec/**/*.rb'
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 PayPro
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,94 @@
1
+ # Sebastian
2
+
3
+ Sebastian makes it easy for you to have service objects in Ruby. It gives you a place to put your business logic.
4
+ It also helps you write safer code by validating that your inputs conform to your expectations.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sebastian'
13
+ ```
14
+
15
+ Or install it manually:
16
+ ```
17
+ $ gem install sebastian
18
+ ```
19
+ ## Usage
20
+
21
+ To define a service object, create a subclass of Sebastian::Base. Then you need to do two things:
22
+
23
+ 1. **Define your attributes.**
24
+ 2. **Define your business logic.** Do this by implementing the #execute method. Each attribute you defined will be available. If any of the attributes are invalid, `#execute` won't be run.
25
+
26
+ That covers the basics. Let's put it all together into a simple example that squares a number.
27
+
28
+ ```ruby
29
+ class CreatePayment < Sebastian::Base
30
+ attr_accessor :amount
31
+ attr_accessor :email
32
+
33
+ validates :amount, numericality: { only_integer: true }
34
+
35
+ private
36
+
37
+ def execute
38
+ 'payment_created' if create_payment
39
+ errors.add(:payment)
40
+ end
41
+
42
+ def create_payment
43
+ Payment.create!(customer: create_customer, amount: amount)
44
+ end
45
+
46
+ def create_customer
47
+ Customer.create!(email: email)
48
+ end
49
+ end
50
+ ```
51
+
52
+ Call `.perform` on your service to execute it. You must pass a single hash to `.perform`. It will return an instance of your service. By convention, we call this an result. You can use the `#ok?` method to ask the result if it's ok. If it's not ok, take a look at its errors with `#errors`. When `#ok?`, the value returned from `#execute` will be stored in `#value`.
53
+
54
+ ```ruby
55
+ result = CreatePayment.perform(email: 'ciel@phantomhive.com', amount: 500)
56
+ result.ok?
57
+ # => true
58
+ result.value
59
+ # => "payment_created"
60
+
61
+ result = CreatePayment.perform(amount: 500)
62
+ result.ok?
63
+ # => false
64
+ result.errors.messages
65
+ # => {:payment=>["is not a valid"]}
66
+ result.value
67
+ # => Sebastian::ResultHasErrorsError: Cannot call value while the service has errors, you should call #ok? first to check
68
+ ```
69
+
70
+ ### Validations
71
+
72
+ These validations aren't provided by Sebastian. They're from ActiveModel. You can also use any custom validations you wrote yourself in your services.
73
+
74
+ ```ruby
75
+ result = CreatePayment.perform(email: 'ciel@phantomhive.com', amount: '5,00')
76
+ result.ok?
77
+ # => false
78
+ result.errors.messages
79
+ # => {:amount=>["is not a number"]}
80
+ ```
81
+
82
+ ## Development
83
+
84
+ 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.
85
+
86
+ 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).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/paypronl/sebastian.
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'sebastian'
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sebastian
4
+ # Provides service functionality. Subclass this to create an service.
5
+ #
6
+ # Example:
7
+ # class ExampleService < Sebastian::Base
8
+ # # Required
9
+ # attr_accessor :foo
10
+ # attr_accessor :bar
11
+ #
12
+ # def execute
13
+ # foo + bar
14
+ # end
15
+ # end
16
+ #
17
+ # result = ExampleService.perform(foo: 10, bar: 5)
18
+ # if result.ok?
19
+ # result.value
20
+ # else
21
+ # result.errors
22
+ # end
23
+ class Base
24
+ include ActiveModel::AttributeAssignment
25
+ include ActiveModel::Validations
26
+ extend ActiveModel::Translation
27
+
28
+ class << self
29
+ def perform(params = {})
30
+ new(params).perform
31
+ end
32
+ end
33
+
34
+ def initialize(params = {})
35
+ assign_attributes(params)
36
+ end
37
+
38
+ def perform
39
+ Result.new(value: valid? ? execute : nil, errors: errors)
40
+ end
41
+
42
+ protected
43
+
44
+ def execute
45
+ raise NotImplementedError, "Must implement #execute in #{self.class} to make the service work"
46
+ end
47
+
48
+ def ok?
49
+ errors.empty?
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sebastian
4
+ # Top-level error class. All other errors subclass this.
5
+ Error = Class.new(StandardError)
6
+
7
+ # Raised if execute is not implemented on subclass.
8
+ NotImplementedError = Class.new(Error)
9
+
10
+ # Raised if value is called while there are errors.
11
+ ResultHasErrorsError = Class.new(Error)
12
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sebastian
4
+ # Invokes the supplied block and wraps the return value in a Sebastian::Result object.
5
+ #
6
+ # Example:
7
+ # result = Sebastian::Result.new(value: 'foo')
8
+ # result.ok?
9
+ # # => true
10
+ # result.value
11
+ # # => 'foo'
12
+ #
13
+ # result = Sebastian::Result.new(value: 'foo', errors: ['bar'])
14
+ # result.ok?
15
+ # # => false
16
+ # result.value
17
+ # # => Sebastian::ServiceReturnedErrorsError
18
+ class Result
19
+ attr_reader :errors
20
+
21
+ def initialize(value:, errors:)
22
+ @value = value
23
+ @errors = errors
24
+ end
25
+
26
+ def ok?
27
+ @errors.empty?
28
+ end
29
+
30
+ def value
31
+ return @value if ok?
32
+ raise(
33
+ ResultHasErrorsError,
34
+ 'Cannot call value while the service has errors, you should call #ok? first to check'
35
+ )
36
+ end
37
+
38
+ def to_s
39
+ if ok?
40
+ "#<Sebastian::Result value: #{@value.inspect}>"
41
+ else
42
+ "#<Sebastian::Result errors: #{@errors.details.inspect}>"
43
+ end
44
+ end
45
+
46
+ alias inspect to_s
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sebastian
4
+ VERSION = '0.1.0'.freeze
5
+ end
data/lib/sebastian.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+
5
+ # Demon Butler
6
+ module Sebastian
7
+ end
8
+
9
+ require 'sebastian/version'
10
+ require 'sebastian/errors'
11
+
12
+ require 'sebastian/result'
13
+ require 'sebastian/base'
data/sebastian.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'sebastian/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'sebastian'
8
+ gem.version = Sebastian::VERSION
9
+ gem.authors = ['Sander Tuin']
10
+ gem.email = ['sander@paypro.nl']
11
+
12
+ gem.summary = 'Your personal demon butler at your services.'
13
+ gem.homepage = 'https://github.com/paypronl/sebastian'
14
+ gem.license = 'MIT'
15
+
16
+ gem.require_paths = ['lib']
17
+ gem.required_ruby_version = '>= 2.2.2'
18
+
19
+ gem.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ gem.add_dependency 'activemodel', '>= 4', '< 6'
24
+
25
+ gem.add_development_dependency 'bundler', '~> 1.16'
26
+ gem.add_development_dependency 'pry', '~> 0.11'
27
+ gem.add_development_dependency 'rake', '~> 10.0'
28
+ gem.add_development_dependency 'rspec', '~> 3.0'
29
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sebastian
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sander Tuin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.16'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.16'
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.11'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.11'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ description:
90
+ email:
91
+ - sander@paypro.nl
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".rspec"
98
+ - ".rubocop.yml"
99
+ - Gemfile
100
+ - LICENSE.txt
101
+ - README.md
102
+ - Rakefile
103
+ - bin/console
104
+ - bin/setup
105
+ - lib/sebastian.rb
106
+ - lib/sebastian/base.rb
107
+ - lib/sebastian/errors.rb
108
+ - lib/sebastian/result.rb
109
+ - lib/sebastian/version.rb
110
+ - sebastian.gemspec
111
+ homepage: https://github.com/paypronl/sebastian
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 2.2.2
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.7.3
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Your personal demon butler at your services.
135
+ test_files: []