service_record 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 904da829c98aa5d247c5979f1a01d4a23214e96cb24855bd3ca501275f28dbec
4
+ data.tar.gz: 1b5443f94ceab5481cbf3afcb08180ca3af00de5ab4e9ec04e315bb431f9c3ef
5
+ SHA512:
6
+ metadata.gz: 02bebf02ace3c9af193819c7ff4ce0cd33427ef6f7e91b8911a0f6c3ceb2a547c0f4dbd301409652dc0f199e1ca5de304b8e2732c8516d655f11fb7597504961
7
+ data.tar.gz: cd925c97f73d12b5af2ca54351b93ee64654e5065c7e9363281f5542976941ccf99f73dbd474d77b834c8b622e0c4d737e373c221064803cdf01a959b24c2401
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in service_record.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.3'
7
+ gem 'rspec', '~> 3.9'
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ service_record (1.0.0)
5
+ activemodel (~> 6.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (6.0.2.2)
11
+ activesupport (= 6.0.2.2)
12
+ activesupport (6.0.2.2)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ zeitwerk (~> 2.2)
18
+ concurrent-ruby (1.1.6)
19
+ diff-lcs (1.3)
20
+ i18n (1.8.2)
21
+ concurrent-ruby (~> 1.0)
22
+ minitest (5.14.0)
23
+ rake (12.3.3)
24
+ rspec (3.9.0)
25
+ rspec-core (~> 3.9.0)
26
+ rspec-expectations (~> 3.9.0)
27
+ rspec-mocks (~> 3.9.0)
28
+ rspec-core (3.9.1)
29
+ rspec-support (~> 3.9.1)
30
+ rspec-expectations (3.9.1)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-mocks (3.9.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-support (3.9.2)
37
+ thread_safe (0.3.6)
38
+ tzinfo (1.2.6)
39
+ thread_safe (~> 0.1)
40
+ zeitwerk (2.3.0)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ rake (~> 12.3)
47
+ rspec (~> 3.9)
48
+ service_record!
49
+
50
+ BUNDLED WITH
51
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Muhammad Usman
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.
@@ -0,0 +1,182 @@
1
+ # ServiceRecord
2
+
3
+ An ActiveRecord lookalike but for business model requirements, a.k.a Service Objects.
4
+
5
+ Rails is packed with all the amazing tools to get you started with building your new awesome project and enforces reliable and battle-tested guidelines. One of the guideline is "**thin controllers and fat models**", but sometimes (actually most of the time) its difficult to follow because most business requirements are not that simple like most CRUD operations.
6
+
7
+ Enters, ServiceRecord. Its similar to ActiveRecord models but their sole purpose is to perform a big/complex/muilt-step task without bloating the controllers or models.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'service_record'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```shell
20
+ $ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```shell
26
+ $ gem install service_record
27
+ ```
28
+
29
+ Now you can start creating new service classes. Rails generator can be used to create new services, e.g, the following will create a service class file `app/services/authenticate_user.rb`.
30
+
31
+ ```shell
32
+ rails g service authenticate_user
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ A basic Service class looks like the following
38
+
39
+ ```ruby
40
+ class MyService < ApplicationService
41
+ attribute :email, :string
42
+ attribute :password, :string
43
+
44
+ validates :email, :password, presence: true
45
+
46
+ def perform
47
+ end
48
+ end
49
+ ```
50
+
51
+ Now you can invoke this service by writting;
52
+
53
+ ```ruby
54
+ response = MyService.perform(email: '', password: '')
55
+ ```
56
+
57
+ The returned response from a service will have the following useful attributes/methods,
58
+
59
+ * `success?` contains true if service was performed without any errors, false otherwise
60
+ * `failure?` contains opposite of success?
61
+ * `result` contains returned value of service perform function
62
+ * `errors` contains details about issues that occurr while performing the service
63
+
64
+
65
+
66
+ ## Example
67
+
68
+ Let's take a real world example of users controller and the sign_in action that involes JWT authentication.
69
+
70
+ Without ServiceRecord 🙈
71
+
72
+ ```ruby
73
+ # Inside controllers/users_controller.rb
74
+ def sign_in
75
+ token = nil
76
+ errors = []
77
+
78
+ # Basic validation
79
+ errors << 'Email is required' if params[:email].blank?
80
+ errors << 'Email is invalid' if params[:email].present? && /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i.match?(params[:email])
81
+ errors << 'Password is required' if params[:password].blank?
82
+
83
+ if errors.size == 0
84
+ user = User.find_by(email: params[:email]).try(:authenticate, params[:password])
85
+
86
+ if user.present?
87
+ token = JsonWebToken.encode(user_id: user.id)
88
+ else
89
+ errors << 'Invalid credentials'
90
+ end
91
+ end
92
+
93
+ if errors.size == 0
94
+ render json: token
95
+ else
96
+ render json: errors, status: :unauthorized
97
+ end
98
+ end
99
+ ```
100
+
101
+ With ServiceRecord 😍
102
+
103
+ ```ruby
104
+ # Inside services/authenticate_user.rb
105
+ class AuthenticateUser < ApplicationService
106
+ attribute :email, :string
107
+ attribute :password, :string
108
+
109
+ validates :email, :password, presence: true
110
+ validates :email, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
111
+
112
+ def perform
113
+ user = User.find_by(email: email).try(:authenticate, password)
114
+
115
+ return JsonWebToken.encode(user_id: user.id) if user.present?
116
+
117
+ errors.add :authentication, 'invalid credentials'
118
+ end
119
+ end
120
+
121
+
122
+ # Inside controllers/users_controller.rb
123
+ def sign_in
124
+ response = AuthenticateUser.perform(params.permit(:email, :password))
125
+
126
+ if response.success?
127
+ render json: response.result
128
+ else
129
+ render json: response.errors, status: :unauthorized
130
+ end
131
+ end
132
+ ```
133
+
134
+ ## Validations
135
+
136
+ ServiceRecord extends on `ActiveModel::Validations`, so, everything that you can do there can be done inside a service class and ServiceRecord will make sure that a service only runs the perform function when all validations are passed, otherwise `errors` will contain details about the validation issues. Since services don't have an underlining object that needs to persisted like in ActiveRecord, you should avoid conditons filters like on create/update/save etc.
137
+
138
+ You can also define callbacks like `before_validation` and `after_validation` just like you do inside an ActiveRecord class.
139
+
140
+
141
+ ## Custom Errors
142
+
143
+ Just like validation errors, you can also add custom errors that you want to be reported. It useful to handle errors which are not related to input parameters validation. E.g.
144
+
145
+ ```ruby
146
+ errors.add :authentication, 'invalid credentials'
147
+ ```
148
+
149
+ ## Callbacks
150
+
151
+ You can also add callbacks on the perform function similar to [ActiveJob's](https://edgeguides.rubyonrails.org/active_job_basics.html#callbacks) perform function. E.g.
152
+
153
+ ```ruby
154
+ class SampleService < ApplicationService
155
+ before_perform :do_something
156
+
157
+ def perform
158
+ end
159
+
160
+ private
161
+ def do_something
162
+ end
163
+ end
164
+ ```
165
+
166
+ Availble callbacks are `before_perform`, `after_perform` and `around_perform`. If a `before_perform` calls `throw :abort`, the callback chain is hallted and perform function would not be called.
167
+
168
+
169
+ ## Development
170
+
171
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
172
+
173
+ 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).
174
+
175
+ ## Contributing
176
+
177
+ Bug reports and pull requests are welcome on GitHub at https://github.com/uxxman-sherwani/service_record.
178
+
179
+
180
+ ## License
181
+
182
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "service_record"
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__)
@@ -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,29 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module Rails
4
+ module Generators
5
+ class ServiceGenerator < Rails::Generators::NamedBase
6
+ desc 'This generator creates a service file at app/services'
7
+
8
+ def self.default_generator_root
9
+ __dir__
10
+ end
11
+
12
+ def create_service_file
13
+ template 'service.rb', File.join('app/services', class_path, "#{file_name}.rb")
14
+
15
+ in_root do
16
+ if behavior == :invoke && !File.exist?(application_service_file_name)
17
+ template 'application_service.rb', application_service_file_name
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def application_service_file_name
25
+ 'app/services/application_service.rb'
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ <% module_namespacing do -%>
2
+ class ApplicationService < ServiceRecord::Base
3
+ end
4
+ <% end -%>
@@ -0,0 +1,15 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %> < ApplicationService
3
+ # Example attributes
4
+ #
5
+ # attribute :email, :string
6
+ # attribute :password, :string
7
+
8
+ # Example validations
9
+ #
10
+ # validates :email, :password, presence: true
11
+
12
+ def perform
13
+ end
14
+ end
15
+ <% end -%>
@@ -0,0 +1,3 @@
1
+ require 'active_model'
2
+ require 'service_record/base'
3
+ require 'service_record/version'
@@ -0,0 +1,38 @@
1
+ require 'service_record/callbacks'
2
+
3
+ module ServiceRecord
4
+ class Base
5
+ include Callbacks
6
+ include ActiveModel::Attributes
7
+ include ActiveModel::Validations
8
+ include ActiveModel::AttributeAssignment
9
+ include ActiveModel::Validations::Callbacks
10
+
11
+ def self.perform(args = {})
12
+ new.tap do |service|
13
+ service.attributes = args
14
+
15
+ if service.valid?
16
+ service.run_callbacks :perform do
17
+ service.result = service.perform
18
+ service.result = nil if service.failure?
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ attr_accessor :result
25
+
26
+ def success?
27
+ errors.empty?
28
+ end
29
+
30
+ def failure?
31
+ !success?
32
+ end
33
+
34
+ def perform
35
+ raise NotImplementedError
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ module ServiceRecord
2
+ module Callbacks
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include ActiveSupport::Callbacks
7
+
8
+ define_callbacks :perform
9
+ end
10
+
11
+ class_methods do
12
+ def before_perform(*filters, &blk)
13
+ set_callback(:perform, :before, *filters, &blk)
14
+ end
15
+
16
+ def after_perform(*filters, &blk)
17
+ set_callback(:perform, :after, *filters, &blk)
18
+ end
19
+
20
+ def around_perform(*filters, &blk)
21
+ set_callback(:perform, :around, *filters, &blk)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module ServiceRecord
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/service_record/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'service_record'
5
+ spec.version = ServiceRecord::VERSION
6
+ spec.authors = ['Muhammad Usman']
7
+ spec.email = ['uxman.sherwani@gmail.com']
8
+
9
+ spec.summary = 'Service objects for rails'
10
+ spec.description = 'ActiveRecord lookalike but for business model requirements'
11
+ spec.homepage = 'https://github.com/uxxman/service_record'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['changelog_uri'] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'activemodel', '~> 6.0'
30
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: service_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Muhammad Usman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-17 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: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ description: ActiveRecord lookalike but for business model requirements
28
+ email:
29
+ - uxman.sherwani@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - lib/rails/generators/service/service_generator.rb
45
+ - lib/rails/generators/service/templates/application_service.rb.tt
46
+ - lib/rails/generators/service/templates/service.rb.tt
47
+ - lib/service_record.rb
48
+ - lib/service_record/base.rb
49
+ - lib/service_record/callbacks.rb
50
+ - lib/service_record/version.rb
51
+ - service_record.gemspec
52
+ homepage: https://github.com/uxxman/service_record
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/uxxman/service_record
57
+ source_code_uri: https://github.com/uxxman/service_record
58
+ changelog_uri: https://github.com/uxxman/service_record
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.5.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.1.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Service objects for rails
78
+ test_files: []