simple_method_callback 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 498a10a40f0b8be3285e78d64c3a56a59e39350f90196940cccb055a683faeba
4
+ data.tar.gz: 45712fbf0faddee83e6062e543f57f0d822f73e8c87a1bed9a9d8dd7ab7d18e4
5
+ SHA512:
6
+ metadata.gz: 9551341bde30a22cb8c3dca6be6d0623752f8a08c37fef70b9dd59fe62861e1abf2a5a0ea5881091ceb2c952199f1f0fe64bfce7b4d392854782087b2d1e3180
7
+ data.tar.gz: 54a07e703339753bf09dcc5ff29829ec79ace363266dbda8096e938c1adcf004e044508eba9f755a00f203a14866642bbb27456d9493d169c2a9bcbe29c5a82e
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,24 @@
1
+ # SimpleMethodCallback
2
+
3
+ 简单的在class添加回调,比如在service、model中
4
+
5
+ #### 使用说明
6
+ ```ruby
7
+ include SimpleMethodCallback
8
+ around_action :everyday, only: %w[one two three]
9
+
10
+ def one
11
+ end
12
+
13
+ def two
14
+ end
15
+
16
+ def three
17
+ end
18
+
19
+ def everyday(&block)
20
+ yield
21
+ end
22
+ ```
23
+
24
+ 作用和 `controller` 里面的 `around_action` 一样。
@@ -0,0 +1,57 @@
1
+ require "simple_method_callback/version"
2
+
3
+ module SimpleMethodCallback
4
+ class ArgumentTypeError < StandardError; end
5
+ def self.included(base)
6
+
7
+ base.instance_eval do
8
+ def around_action(around_method, only: nil)
9
+ include ActiveSupport::Callbacks
10
+
11
+ if only.nil?
12
+ only = base.singleton_methods(false)
13
+ elsif only.is_a?(Symbol) || only.is_a?(String)
14
+ only = [only]
15
+ end
16
+
17
+ raise ArgumentTypeError, 'only argument type error' unless only.is_a?(Array)
18
+
19
+ around_method_list ||= @@around_method_list ||= Set.new
20
+ around_only_method_list ||= @@around_only_method_list ||= Set.new
21
+ around_method_list << around_method
22
+ around_only_method_list += only
23
+
24
+ define_callbacks *around_only_method_list.map(&:to_sym)
25
+ #TODO 苏铭轩 20200925 添加before 以及 after 即可实现 前置和后置回调
26
+ # 以下这种方式比较low,需要借鉴优秀的项目是怎么去处理这种情况的
27
+ class_variable_set('@@around_method_list', around_method_list)
28
+ class_variable_set('@@around_only_method_list', around_only_method_list)
29
+
30
+ extend ClassMethods
31
+ prepend InstanceMethods
32
+ end
33
+ end
34
+ end
35
+
36
+ module ClassMethods
37
+ def self.extended(sub)
38
+ sub.class_variable_get('@@around_method_list').map(&:to_sym).each do |am|
39
+ sub.class_variable_get('@@around_only_method_list').map(&:to_sym).each do |om|
40
+ sub.set_callback om, :around, am
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ module InstanceMethods
47
+ def self.prepended(mod)
48
+ mod.class_variable_get('@@around_only_method_list').map(&:to_sym).each do |method_name|
49
+ define_method(method_name) do |*options|
50
+ run_callbacks method_name do
51
+ super(*options)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleMethodCallback
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'lib/simple_method_callback/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "simple_method_callback"
5
+ spec.version = SimpleMethodCallback::VERSION
6
+ spec.authors = ["sumingxuan"]
7
+ spec.email = ["1154621382@qq.com"]
8
+
9
+ spec.summary = %q{nil}
10
+ spec.description = %q{简单的在class添加回调,比如在service、model中}
11
+ spec.homepage = "https://github.com/SuMingXuan"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_method_callback
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sumingxuan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 简单的在class添加回调,比如在service、model中
14
+ email:
15
+ - 1154621382@qq.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - README.md
22
+ - lib/simple_method_callback.rb
23
+ - lib/simple_method_callback/version.rb
24
+ - simple_method_callback.gemspec
25
+ homepage: https://github.com/SuMingXuan
26
+ licenses: []
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.3.0
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.0.8
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: nil
47
+ test_files: []