rails-active_action 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2ad3a261e2b61e68548d91595e457626e33e5f1403352606b77ae42e9e049152
4
+ data.tar.gz: 185ae1a5afcc71ce2ed79d7d6d756f8822c0e056a87e8b799fc668783a005f46
5
+ SHA512:
6
+ metadata.gz: f6436c19c114ad50c6849d91dee2d7fee62a785aa5963d0ca7ca510d7e79f26da4db2c41c6ee836462a43cee3f1d7754d3913dd12263bf73210b22bdc026df47
7
+ data.tar.gz: 2379e06a68a31caa19c43a90ace2e1bfe9d2422acdf5e9c058898d4cbf04a250741ede94b7b4ea9ca75ab9f9b16e9a30f46d511530d24726292b65062e941a5e
data/.gitignore ADDED
@@ -0,0 +1,13 @@
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
12
+
13
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
@@ -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 lukasz.sliwa@apptamers.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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'activesupport', '~> 3.0.0'
6
+ gem 'i18n', '~> 1.8.9'
7
+
8
+ group :test do
9
+ gem 'rake', '~> 12.0'
10
+ gem 'rspec', '~> 3.0'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,40 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails-active_action (3.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (3.0.20)
10
+ concurrent-ruby (1.1.8)
11
+ diff-lcs (1.4.4)
12
+ i18n (1.8.9)
13
+ concurrent-ruby (~> 1.0)
14
+ rake (12.3.3)
15
+ rspec (3.10.0)
16
+ rspec-core (~> 3.10.0)
17
+ rspec-expectations (~> 3.10.0)
18
+ rspec-mocks (~> 3.10.0)
19
+ rspec-core (3.10.1)
20
+ rspec-support (~> 3.10.0)
21
+ rspec-expectations (3.10.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-mocks (3.10.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-support (3.10.2)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ activesupport (~> 3.0.0)
34
+ i18n (~> 1.8.9)
35
+ rails-active_action!
36
+ rake (~> 12.0)
37
+ rspec (~> 3.0)
38
+
39
+ BUNDLED WITH
40
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Łukasz Śliwa (lukasz.sliwa@apptamers.com)
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Łukasz Śliwa (lukasz.sliwa@apptamers.com)
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,182 @@
1
+ # ActiveAction
2
+
3
+ A simple way to build and use Service Objects in Ruby on Rails.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rails-active_action'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails-active_action
20
+
21
+ ## ActiveAction example
22
+
23
+ ```ruby
24
+ # app/actions/my_action.rb
25
+ class MyAction < ActiveAction::Base
26
+ def perform
27
+ # Your code here
28
+ end
29
+ end
30
+
31
+ # app/controllers/my_controller.rb
32
+ class MyController < ApplicationController
33
+ def create
34
+ MyAction.perform
35
+ end
36
+ end
37
+ ```
38
+
39
+ or `perform!` to raise an error if occurred.
40
+
41
+ ```ruby
42
+ # app/actions/my_action.rb
43
+ class MyAction < ActiveAction::Base
44
+ def perform
45
+ # Your code here
46
+ error!
47
+ end
48
+ end
49
+
50
+ # app/controllers/my_controller.rb
51
+ class MyController < ApplicationController
52
+ def create
53
+ MyAction.perform! # it will raise ActiveAction::Error
54
+ end
55
+ end
56
+ ```
57
+
58
+ ## Demo
59
+
60
+ ```ruby
61
+ # app/actions/create_user_action.rb
62
+ class CreateUser < ActiveAction::Base
63
+ attributes :user
64
+
65
+ after_perform :send_email_on_success, on: :success
66
+
67
+ def perform(company, params)
68
+ self.user = company.users.build(params)
69
+ if self.user.save
70
+ success!
71
+ else
72
+ error!
73
+ end
74
+ end
75
+
76
+ def send_email_on_success
77
+ User.welcome.with(user: self.user).deliver
78
+ end
79
+ end
80
+
81
+ # app/controllers/users_controller.rb
82
+ class UsersController < ApplicationController
83
+ def create
84
+ context = CreateUser.perform(current_company, params_user)
85
+
86
+ respond_to do |format|
87
+ if context.success?
88
+ format.html { redirect_to dashboard_path, notice: 'Welcome to our app' }
89
+ else
90
+ format.html { render action: :new }
91
+ end
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ def current_company
98
+ @company ||= Company.find_by!(subdomain: request.subdomain)
99
+ end
100
+ end
101
+ ```
102
+
103
+ ## Callbacks
104
+
105
+ On `success` or `error`:
106
+ * `after_perform`
107
+ * `before_perform`
108
+
109
+ ```ruby
110
+ # app/actions/create_post.rb
111
+ class CreatePost < ActiveAction::Base
112
+ before_perform do
113
+ # run a code before perform within a block
114
+ end
115
+
116
+ after_perform do
117
+ # run a code after perform within a block
118
+ end
119
+
120
+ after_perform :run_callback, on: :success
121
+ after_perform :run_warning_email, on: :error
122
+
123
+ def perform(user, params: {})
124
+ # your code here
125
+ end
126
+ end
127
+ ```
128
+
129
+ * `around_perform`
130
+
131
+ ```ruby
132
+ # app/actions/run_process.rb
133
+ class RunProcess < ActiveAction::Base
134
+ around_perform :measure
135
+
136
+ def measure
137
+ @start = Time.current
138
+ yield
139
+ @end = Time.current
140
+ end
141
+
142
+ def perform(id)
143
+ `#{Process.find(id).command}`
144
+ end
145
+ end
146
+ ```
147
+
148
+ ## Statuses
149
+
150
+ Call `success!`, `succeed!`, `done!`, `correct!`, `ready!`, `active!` if the action performed successfully or `error!`, `fail!`, `failure!`, `failed!`, `invalid!`, `incorrect!`, `inactive!` otherwise.
151
+
152
+ ```ruby
153
+ # app/actions/run.rb
154
+ class Run < ActiveAction::Base
155
+ def perform
156
+ if rand(100) % 2 == 0
157
+ success!
158
+ else
159
+ fail!
160
+ end
161
+ end
162
+
163
+ after_perform :send_email_on_success
164
+
165
+ def send_email_on_success
166
+ Mail.deliver! if success?
167
+ end
168
+ end
169
+ ```
170
+
171
+ ## Contributing
172
+
173
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lukaszsliwa/active_action. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/lukaszsliwa/active_action/blob/master/CODE_OF_CONDUCT.md).
174
+
175
+
176
+ ## License
177
+
178
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
179
+
180
+ ## Code of Conduct
181
+
182
+ Everyone interacting in the ActiveAction project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/lukaszsliwa/active_action/blob/master/CODE_OF_CONDUCT.md).
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 "active_action"
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,31 @@
1
+ require 'active_support/dependencies/autoload'
2
+ require 'active_support/callbacks'
3
+
4
+ require 'active_action/version'
5
+ require 'active_action/statuses'
6
+ require 'active_action/callbacks'
7
+ require 'active_action/base'
8
+ require 'active_action/error'
9
+
10
+ #
11
+ # Examples
12
+ #
13
+ # class MyAction < ActiveAction::Base
14
+ # after_perform :send_email, on: :success
15
+ #
16
+ # def perform(email_address)
17
+ #
18
+ # end
19
+ #
20
+ # def send_email
21
+ #
22
+ # end
23
+ # end
24
+ #
25
+
26
+ module ActiveAction
27
+ extend ActiveSupport::Autoload
28
+
29
+ autoload :Base
30
+ autoload :Error
31
+ end
@@ -0,0 +1,36 @@
1
+ module ActiveAction
2
+ class Base
3
+ include ActiveSupport::Callbacks
4
+ define_callbacks :perform
5
+
6
+ extend ActiveAction::Callbacks
7
+ include ActiveAction::Statuses
8
+
9
+ attr_reader :status
10
+ attr_accessor :value
11
+
12
+ class << self
13
+ alias_method :attributes, :attr_accessor
14
+
15
+ def perform(*args)
16
+ instance = self.new
17
+ instance.value = instance.send(:perform_with_callbacks, *args)
18
+ instance.success! if instance.status.nil?
19
+ instance
20
+ end
21
+
22
+ def perform!(*args)
23
+ if (instance = perform(*args)).error?
24
+ raise ActiveAction::Error.new
25
+ end
26
+ instance
27
+ end
28
+ end
29
+
30
+ def perform_with_callbacks(*args)
31
+ run_callbacks :perform do
32
+ perform(*args)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveAction
2
+ module Callbacks
3
+ def after_perform(method_name = nil, params = {}, &block)
4
+ prepare_callback(:perform, :after, method_name, params, &block)
5
+ end
6
+
7
+ def before_perform(method_name = nil, params = {}, &block)
8
+ prepare_callback(:perform, :before, method_name, params, &block)
9
+ end
10
+
11
+ def around_perform(method_name)
12
+ set_callback(:perform, :around, method_name)
13
+ end
14
+
15
+ private
16
+
17
+ def prepare_callback(on, what, method_name = nil, params = {}, &block)
18
+ set_callback(on, what) do
19
+ if params[:on] == :error && error? || \
20
+ params[:on] == :success && success? || params[:on].nil?
21
+ block_given? ? instance_eval(&block) : self.method(method_name).call
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveAction
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveAction
2
+ module Statuses
3
+ {
4
+ success: %i(success succeed done correct ready active),
5
+ error: %i(error failure fail invalid incorrect inactive)
6
+ }.each do |key, values|
7
+ values.each do |value|
8
+ define_method(:"#{value}?") { @status == key }
9
+
10
+ define_method(:"#{value}!") { @status = key }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveAction
2
+ VERSION = '3.0.0'
3
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/active_action/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rails-active_action"
5
+ spec.version = ActiveAction::VERSION
6
+ spec.authors = ["Łukasz Śliwa"]
7
+ spec.email = ["lukasz.sliwa@apptamers.com"]
8
+
9
+ spec.summary = %q{A simple way to build and use Service Objects in Ruby on Rails.}
10
+ spec.description = %q{ActiveAction makes you able to organise and move some actions to the separated classes.}
11
+ spec.homepage = "https://github.com/lukaszsliwa/active_action"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/lukaszsliwa/active_action"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-active_action
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Łukasz Śliwa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ActiveAction makes you able to organise and move some actions to the
14
+ separated classes.
15
+ email:
16
+ - lukasz.sliwa@apptamers.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/active_action.rb
34
+ - lib/active_action/base.rb
35
+ - lib/active_action/callbacks.rb
36
+ - lib/active_action/error.rb
37
+ - lib/active_action/statuses.rb
38
+ - lib/active_action/version.rb
39
+ - rails-active_action.gemspec
40
+ homepage: https://github.com/lukaszsliwa/active_action
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/lukaszsliwa/active_action
45
+ source_code_uri: https://github.com/lukaszsliwa/active_action
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.1.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A simple way to build and use Service Objects in Ruby on Rails.
65
+ test_files: []