abstriker 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: 58fc310fb1c93359f11f08ece41ac5447b228166230656b8a481a21efb13ff42
4
+ data.tar.gz: c941dc1843f1bdb93cb567fa3e2a0c7fa9379b765832a9a01ccfe19ba9dd32f7
5
+ SHA512:
6
+ metadata.gz: aa481fe912ed555b398e61ab3f64b9f3a51e3cada25148143ad8e7587bceacf3fb3afb5208b0b47327c174915168d9577bcb62b87ca629719f5001f5c94cd934
7
+ data.tar.gz: cd96e6d7772f00590e3a260a42602d52d7e14916e32c699c82f6b22930b7b1a962e1a901bf5bcab509c59ef12d79d7111784058248f0393a60ea1833f512973e
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
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
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
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ - 2.4.2
6
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in abstriker.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 joker1007
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,131 @@
1
+ # Abstriker
2
+
3
+ This gem adds `abstract` syntax. that is similar to Java's one.
4
+ `abstract` modified method requires subclass implementation.
5
+
6
+ If subclass does not implement `abstract` method, raise `Abstriker::NotImplementedError`.
7
+ `Abstriker::NotImplementedError` is curently subclass of `::NotImplementedError`.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'abstriker'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install abstriker
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ class A1
29
+ extend Abstriker
30
+
31
+ abstract def foo
32
+ end
33
+ end
34
+
35
+ class A3 < A1
36
+ def foo
37
+ end
38
+ end # => OK
39
+
40
+ class A2 < A1
41
+ end # => raise
42
+
43
+ Class.new(A1) do
44
+ end # => raise
45
+ ```
46
+
47
+ ### for Production
48
+ If you want to disable Abstriker, write `Abstriker.disable = true` at first line.
49
+ If Abstriker is disabled, TracePoint never runs, and so there is no overhead of VM instruction.
50
+
51
+ ### Examples
52
+
53
+ #### include module
54
+
55
+ ```ruby
56
+ module B1
57
+ extend Abstriker
58
+
59
+ abstract def foo
60
+ end
61
+ end
62
+
63
+ class B2
64
+ include B1
65
+ end # => raise
66
+
67
+ Module.new do
68
+ include B1
69
+ end # => raise
70
+ ```
71
+
72
+ #### extend module
73
+
74
+ ```ruby
75
+ module C1
76
+ extend Abstriker
77
+
78
+ abstract def foo
79
+ end
80
+ end
81
+
82
+ class C3
83
+ extend C1
84
+
85
+ def self.foo
86
+ end
87
+ end # => OK
88
+
89
+ class C2
90
+ extend C1
91
+ end # raise
92
+ ```
93
+
94
+ #### singleton class
95
+
96
+ ```ruby
97
+ class D1
98
+ extend Abstriker
99
+
100
+ class << self
101
+ abstract def foo
102
+ end
103
+ end
104
+ end
105
+
106
+ class D3 < D1
107
+ def self.foo
108
+ end
109
+ end # => OK
110
+
111
+ class D2 < D1
112
+ end # => raise
113
+
114
+ Class.new(D1) do
115
+ end # => raise
116
+
117
+ ```
118
+
119
+ ## Development
120
+
121
+ 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.
122
+
123
+ 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).
124
+
125
+ ## Contributing
126
+
127
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joker1007/abstriker.
128
+
129
+ ## License
130
+
131
+ 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/abstriker.gemspec ADDED
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "abstriker/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "abstriker"
8
+ spec.version = Abstriker::VERSION
9
+ spec.authors = ["joker1007"]
10
+ spec.email = ["kakyoin.hierophant@gmail.com"]
11
+
12
+ spec.summary = %q{Add abstract syntax that target method requires subclass implementation.}
13
+ spec.description = %q{Add abstract syntax that target method requires subclass implementation.}
14
+ spec.homepage = "https://github.com/joker1007/abstriker"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ 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
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "abstriker"
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,3 @@
1
+ module Abstriker
2
+ VERSION = "0.1.0"
3
+ end
data/lib/abstriker.rb ADDED
@@ -0,0 +1,160 @@
1
+ require "abstriker/version"
2
+ require "set"
3
+
4
+ module Abstriker
5
+ class NotImplementedError < NotImplementedError
6
+ attr_reader :subclass, :abstract_method
7
+
8
+ def initialize(klass, abstract_method)
9
+ super("#{abstract_method} is abstract, but not implemented by #{klass}")
10
+ @subclass = klass
11
+ @abstract_method = abstract_method
12
+ end
13
+ end
14
+
15
+ @disable = false
16
+
17
+ def self.disable=(v)
18
+ @disable = v
19
+ end
20
+
21
+ def self.disabled?
22
+ @disable
23
+ end
24
+
25
+ def self.enabled?
26
+ !disabled?
27
+ end
28
+
29
+ def self.abstract_methods
30
+ @abstract_methods ||= {}
31
+ end
32
+
33
+ def self.extended(base)
34
+ base.extend(SyntaxMethods)
35
+ base.singleton_class.extend(SyntaxMethods)
36
+ if enabled?
37
+ base.extend(ModuleMethods) if base.is_a?(Module)
38
+ base.extend(ClassMethods) if base.is_a?(Class)
39
+ end
40
+ end
41
+
42
+ module SyntaxMethods
43
+ private
44
+
45
+ def abstract(symbol)
46
+ method_set = Abstriker.abstract_methods[self] ||= Set.new
47
+ method_set.add(symbol)
48
+ end
49
+ end
50
+
51
+ module HookBase
52
+ private
53
+
54
+ def detect_event_type
55
+ caller_info = caller_locations(3, 1)[0]
56
+ if caller_info.label.match?(/block/)
57
+ [:end, :raise]
58
+ elsif caller_info.label.match?(/initialize/) || caller_info.label.match?(/new/)
59
+ [:b_call, :b_return, :raise]
60
+ end
61
+ end
62
+
63
+ def check_abstract_methods(klass, block_count_offset = 0)
64
+ return if Abstriker.disabled?
65
+
66
+ event_type = detect_event_type
67
+
68
+ unless klass.instance_variable_get("@__abstract_trace_point")
69
+ block_count = block_count_offset
70
+
71
+ tp = TracePoint.trace(*event_type) do |t|
72
+ if t.event == :raise
73
+ tp.disable
74
+ next
75
+ end
76
+
77
+ block_count += 1 if t.event == :b_call
78
+ block_count -= 1 if t.event == :b_return
79
+
80
+ if t.self == klass && (t.event == :end || t.event == :b_return && block_count.zero?)
81
+ klass.ancestors.drop(1).each do |mod|
82
+ Abstriker.abstract_methods[mod]&.each do |fmeth_name|
83
+ meth = klass.instance_method(fmeth_name)
84
+ unless meth&.owner == klass
85
+ tp.disable
86
+ klass.instance_variable_set("@__abstract_trace_point", nil)
87
+ raise Abstriker::NotImplementedError.new(klass, meth)
88
+ end
89
+ end
90
+ end
91
+ tp.disable
92
+ klass.instance_variable_set("@__abstract_trace_point", nil)
93
+ end
94
+ end
95
+ klass.instance_variable_set("@__abstract_trace_point", tp)
96
+ end
97
+ end
98
+
99
+ def check_abstract_singleton_methods(klass, block_count_offset = 0)
100
+ return if Abstriker.disabled?
101
+
102
+ event_type = detect_event_type
103
+
104
+ unless klass.instance_variable_get("@__abstract_singleton_trace_point")
105
+ block_count = block_count_offset
106
+
107
+ tp = TracePoint.trace(*event_type) do |t|
108
+ if t.event == :raise
109
+ tp.disable
110
+ next
111
+ end
112
+
113
+ block_count += 1 if t.event == :b_call
114
+ block_count -= 1 if t.event == :b_return
115
+
116
+ if t.self == klass && (t.event == :end || t.event == :b_return && block_count.zero?)
117
+ klass.singleton_class.ancestors.drop(1).each do |mod|
118
+ Abstriker.abstract_methods[mod]&.each do |fmeth_name|
119
+ meth = klass.singleton_class.instance_method(fmeth_name)
120
+ unless meth&.owner == klass.singleton_class
121
+ tp.disable
122
+ klass.instance_variable_set("@__abstract_singleton_trace_point", nil)
123
+ raise Abstriker::NotImplementedError.new(klass, meth)
124
+ end
125
+ end
126
+ end
127
+ tp.disable
128
+ klass.instance_variable_set("@__abstract_singleton_trace_point", nil)
129
+ end
130
+ end
131
+ klass.instance_variable_set("@__abstract_singleton_trace_point", tp)
132
+ end
133
+ end
134
+ end
135
+
136
+ module ClassMethods
137
+ include HookBase
138
+
139
+ private
140
+
141
+ def inherited(subclass)
142
+ check_abstract_methods(subclass)
143
+ check_abstract_singleton_methods(subclass)
144
+ end
145
+ end
146
+
147
+ module ModuleMethods
148
+ include HookBase
149
+
150
+ private
151
+
152
+ def included(base)
153
+ check_abstract_methods(base, 1)
154
+ end
155
+
156
+ def extended(base)
157
+ check_abstract_singleton_methods(base, 1)
158
+ end
159
+ end
160
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abstriker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joker1007
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Add abstract syntax that target method requires subclass implementation.
56
+ email:
57
+ - kakyoin.hierophant@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - abstriker.gemspec
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/abstriker.rb
73
+ - lib/abstriker/version.rb
74
+ homepage: https://github.com/joker1007/abstriker
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.4
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Add abstract syntax that target method requires subclass implementation.
98
+ test_files: []