embed_callbacks 0.1.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: 30084b7f7b4e7428eab54df0b76aa442b48ed867e0f422d8f84495afbb5f8c3f
4
+ data.tar.gz: 9961711ce671a6056ec9d18dc5d3ba4920588c7f46392abb56437799b7ed6ed6
5
+ SHA512:
6
+ metadata.gz: e1bc18923a237a94070a290a62eb70adbb119b4790baaa4c2bf3bb7fd18170c3ac069e71660b5ba8873b7d97e34b08647ee2e567ca32e8eee04efb27ffc95bef
7
+ data.tar.gz: c1267dafece5ce7a0f8e38c709bdf5c6c3ced181f3636c7661f3032b1a2912c10b10d1319f29a5bf786d96a0e0b132cba4416d21c8d5f075561a40bd546c0c84
@@ -0,0 +1,31 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Publish
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ matrix:
16
+ ruby: [ '2.7.x', '2.6.x', '2.5.x' ]
17
+
18
+ steps:
19
+ - uses: actions/checkout@v2
20
+ - name: Set up Ruby
21
+ uses: actions/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby }}
24
+
25
+ - name: Install dependencies
26
+ run: |
27
+ gem install bundler --no-document
28
+ bundle install
29
+
30
+ - name: Run test
31
+ run: bundle exec rspec spec
@@ -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,9 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - "2.5.7"
6
+ - "2.6.5"
7
+ - "2.7.0"
8
+ before_install: gem install bundler -v 2.1.4
9
+ script: "bundle exec rake spec"
@@ -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 hotoolong.hogehoge+github@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 [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,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in embed_callbacks.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'codecov', :require => false, :group => :test
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ embed_callbacks (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ codecov (0.2.6)
10
+ colorize
11
+ json
12
+ simplecov
13
+ colorize (0.8.1)
14
+ diff-lcs (1.4.4)
15
+ docile (1.3.2)
16
+ json (2.3.1)
17
+ rake (12.3.3)
18
+ rspec (3.9.0)
19
+ rspec-core (~> 3.9.0)
20
+ rspec-expectations (~> 3.9.0)
21
+ rspec-mocks (~> 3.9.0)
22
+ rspec-core (3.9.2)
23
+ rspec-support (~> 3.9.3)
24
+ rspec-expectations (3.9.2)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-mocks (3.9.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.9.0)
30
+ rspec-support (3.9.3)
31
+ simplecov (0.19.0)
32
+ docile (~> 1.1)
33
+ simplecov-html (~> 0.11)
34
+ simplecov-html (0.12.2)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ codecov
41
+ embed_callbacks!
42
+ rake (~> 12.0)
43
+ rspec (~> 3.0)
44
+ simplecov
45
+
46
+ BUNDLED WITH
47
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 hotoolong
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,206 @@
1
+ # EmbedCallbacks
2
+
3
+ [![Build Status](https://travis-ci.org/hotoolong/embed_callbacks.svg?branch=main)](https://travis-ci.org/hotoolong/embed_callbacks)
4
+ [![codecov](https://codecov.io/gh/hotoolong/embed_callbacks/branch/main/graph/badge.svg)](https://codecov.io/gh/hotoolong/embed_callbacks)
5
+
6
+ This gem makes it easy to create callbacks.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'simple_callbacker', github: 'hotoolong/embed_callbacks'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install specific_install
23
+ $ gem specific_install git@github.com:hotoolong/embed_callbacks.git main
24
+
25
+ ## Usage
26
+
27
+ embed_callback provides the following callbacks.
28
+
29
+ - `before` is called the operation before the specified method.
30
+ - `after` is called processing after the specified method.
31
+ - `around` is called processing before and after the specified method.
32
+ - `rescue` is called if the specified method produces an error.
33
+ - `ensure` is always called if the given method completes.
34
+
35
+ It also lets you decide whether or not to run the process according to the conditions.
36
+ The conditions can be if and unless.
37
+
38
+ ### before example
39
+
40
+ ```ruby
41
+ require 'embed_callbacks'
42
+
43
+ class Sample
44
+ include EmbedCallbacks
45
+ set_callback :target, :before, :before_callback
46
+
47
+ def target
48
+ puts 'target'
49
+ end
50
+
51
+ def before_callback
52
+ puts 'before_callback'
53
+ end
54
+
55
+ end
56
+ sample = Sample.new
57
+ sample.target
58
+ #=> before_callback
59
+ #=> target
60
+ ```
61
+
62
+ ### after example
63
+
64
+ ```ruby
65
+ require 'embed_callbacks'
66
+
67
+ class Sample
68
+ include EmbedCallbacks
69
+ set_callback :target, :after, :after_callback
70
+
71
+ def target
72
+ puts 'target'
73
+ end
74
+
75
+ def after_callback
76
+ puts 'after_callback'
77
+ end
78
+
79
+ end
80
+ sample = Sample.new
81
+ sample.target
82
+ #=> target
83
+ #=> after_callback
84
+ ```
85
+
86
+ ### around example
87
+
88
+ ```ruby
89
+ require 'embed_callbacks'
90
+
91
+ class Sample
92
+ include EmbedCallbacks
93
+ set_callback :target, :around, :around_callback
94
+
95
+ def target
96
+ puts 'target'
97
+ end
98
+
99
+ def around_callback
100
+ puts 'around_callback'
101
+ end
102
+
103
+ end
104
+ sample = Sample.new
105
+ sample.target
106
+ #=> around_callback
107
+ #=> target
108
+ #=> around_callback
109
+ ```
110
+ ### rescue example
111
+
112
+ ```ruby
113
+ require 'embed_callbacks'
114
+
115
+ class Sample
116
+ include EmbedCallbacks
117
+ set_callback :target, :rescue, :rescue_callback
118
+
119
+ def target
120
+ raise 'target'
121
+ end
122
+
123
+ def rescue_callback
124
+ puts 'rescue_callback'
125
+ end
126
+
127
+ end
128
+ sample = Sample.new
129
+ sample.target
130
+
131
+ #=> rescue_callback
132
+ #=> RuntimeError (target)
133
+ ```
134
+
135
+ ### ensure example
136
+
137
+ ```ruby
138
+ require 'embed_callbacks'
139
+
140
+ class Sample
141
+ include EmbedCallbacks
142
+ set_callback :target, :ensure, :ensure_callback
143
+
144
+ def target
145
+ puts 'target'
146
+ end
147
+
148
+ def ensure_callback
149
+ puts 'ensure_callback'
150
+ end
151
+
152
+ end
153
+ sample = Sample.new
154
+ sample.target
155
+ #=> target
156
+ #=> ensure_callback
157
+ ```
158
+
159
+ ### condition example
160
+
161
+ ```ruby
162
+ require 'embed_callbacks'
163
+
164
+ class Sample
165
+ include EmbedCallbacks
166
+ attr_accessor :check_flag
167
+
168
+ set_callback :target, :before, :before_callback, if: ->(record) { record.check_flag }
169
+
170
+ def target
171
+ puts 'target'
172
+ end
173
+
174
+ def before_callback
175
+ puts 'before_callback'
176
+ end
177
+
178
+ end
179
+ sample = Sample.new
180
+ sample.check_flag = false
181
+ sample.target
182
+ #=> target
183
+ sample.check_flag = true
184
+ sample.target
185
+ #=> before_callback
186
+ #=> target
187
+ ```
188
+
189
+ ## Development
190
+
191
+ 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.
192
+
193
+ 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).
194
+
195
+ ## Contributing
196
+
197
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hotoolong/embed_callbacks. 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/hotoolong/embed_callbacks/blob/main/CODE_OF_CONDUCT.md).
198
+
199
+
200
+ ## License
201
+
202
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
203
+
204
+ ## Code of Conduct
205
+
206
+ Everyone interacting in the EmbedCallbacks project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hotoolong/embed_callbacks/blob/main/CODE_OF_CONDUCT.md).
@@ -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 "embed_callbacks"
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,26 @@
1
+ require_relative 'lib/embed_callbacks/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "embed_callbacks"
5
+ spec.version = EmbedCallbacks::VERSION
6
+ spec.authors = ["hotoolong"]
7
+ spec.email = ["hotoolong.hogehoge+github@gmail.com"]
8
+
9
+ spec.summary = %q{Create a method callback.}
10
+ spec.description = %q{Whenever you want to add a callback, you can easily incorporate the process.\n }
11
+ spec.homepage = "https://github.com/hotoolong/embed_callbacks"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
14
+
15
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "rake", "~> 12.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency 'simplecov'
25
+ spec.add_development_dependency 'codecov', '>= 0.2.0'
26
+ end
@@ -0,0 +1,68 @@
1
+ require "embed_callbacks/version"
2
+
3
+ module EmbedCallbacks
4
+ def self.included(base)
5
+ base.extend(PrependMethods)
6
+ end
7
+
8
+ module PrependMethods
9
+ def set_callback(target_method_name, behavior_sym, callback_function_name, **options)
10
+ behavior = Behavior.new(behavior_sym)
11
+ m = Module.new
12
+ m.define_method(target_method_name) do |*params|
13
+ condition = Condition.new(options).call(self)
14
+ begin
15
+ method(callback_function_name).call if condition && behavior.before?
16
+ return_value = super(*params)
17
+ method(callback_function_name).call if condition && behavior.after?
18
+ return_value
19
+ rescue => e
20
+ method(callback_function_name).call if condition && behavior.rescue?
21
+ raise e
22
+ ensure
23
+ method(callback_function_name).call if condition && behavior.ensure?
24
+ end
25
+ end
26
+ prepend m
27
+ end
28
+ end
29
+
30
+ class Condition
31
+ def initialize(options)
32
+ @if = options[:if]
33
+ @unless = options[:unless]
34
+ raise ArgumentError, "Don't use if and unless at the same time." if @if && @unless
35
+ end
36
+
37
+ def call(record)
38
+ return @if.call(record) if @if
39
+ return !@unless&.call(record) if @unless
40
+ true
41
+ end
42
+ end
43
+
44
+ class Behavior
45
+ KINDS = %i(before after around rescue ensure)
46
+
47
+ def initialize(behavior)
48
+ @behavior = behavior
49
+ raise ArgumentError, 'The behavior should be set in the before after around rescue' unless KINDS.include?(behavior)
50
+ end
51
+
52
+ def before?
53
+ %i(before around).include?(@behavior)
54
+ end
55
+
56
+ def after?
57
+ %i(after around).include?(@behavior)
58
+ end
59
+
60
+ def rescue?
61
+ :rescue == @behavior
62
+ end
63
+
64
+ def ensure?
65
+ :ensure == @behavior
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module EmbedCallbacks
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embed_callbacks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hotoolong
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '12.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '12.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.0
69
+ description: 'Whenever you want to add a callback, you can easily incorporate the
70
+ process.\n '
71
+ email:
72
+ - hotoolong.hogehoge+github@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/gem-push.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - embed_callbacks.gemspec
90
+ - lib/embed_callbacks.rb
91
+ - lib/embed_callbacks/version.rb
92
+ homepage: https://github.com/hotoolong/embed_callbacks
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 2.5.0
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.1.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Create a method callback.
115
+ test_files: []