finalize_block 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: 5c30605a4c43d8f27732234ca71d46dd77d01c5cf6306ee86b7289a6a7b5085e
4
+ data.tar.gz: 484e1428642f4603538e7e6e3406e9c325147d3e86c728edef3edd3089863be1
5
+ SHA512:
6
+ metadata.gz: ac716a88ddc643386e0c528a09016d3d1ff3f433a05001e3a81043a6675149225da41fc1b2f25ebc884b70a7970c52e54920dc77c38670bba50fb3b02f2cea63
7
+ data.tar.gz: f32866fe67b3e8f083be24cb7983d95727754565d67fb39bbb4065cf23175a60b3d2ab23246ca6b1f1f2c660c3335cfb6c5e2807bba95605e0580ef7b6f18311
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.2
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 finalize_block.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Koichi Sasada
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,52 @@
1
+ # FinalizeBlock
2
+
3
+ Joke gem - This gem provide `finalize_block` method.
4
+ (or demonstration)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'finalize_block'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install finalize_block
21
+
22
+ ## Usage
23
+
24
+ You can use `finalize_block` method with a cleanup rule for a Class.
25
+
26
+ ```
27
+ require 'finalize_block'
28
+
29
+ finalize_block String => -> str do
30
+ # clear procedure by replacing with emtpy string.
31
+ str.replace ''
32
+ end do
33
+ $g = 'a' * 256
34
+ p $g.size #=> 256
35
+ end
36
+
37
+ p $g #=> '' # cleared
38
+ ```
39
+
40
+ ## Development
41
+
42
+ 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.
43
+
44
+ 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).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/finalize_block.
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "rake/extensiontask"
4
+
5
+ Rake::ExtensionTask.new('finalize_block')
6
+
7
+ task :run => :compile do
8
+ ruby %q{-I ./lib test.rb}
9
+ end
10
+
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << "test"
13
+ t.libs << "lib"
14
+ t.test_files = FileList["test/**/*_test.rb"]
15
+ end
16
+
17
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "finalize_block"
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,2 @@
1
+ require 'mkmf'
2
+ create_makefile('finalize_block')
@@ -0,0 +1,63 @@
1
+ #include "ruby/ruby.h"
2
+ #include "ruby/debug.h"
3
+
4
+ static VALUE c_FinalizeBlock;
5
+ static struct st_table *g_allocated_objects;
6
+
7
+ static void
8
+ newobj_i(VALUE tpval, void *data)
9
+ {
10
+ rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
11
+ VALUE obj = rb_tracearg_object(tparg);
12
+ VALUE klass = rb_obj_class(obj);
13
+ VALUE target_class = (VALUE)data;
14
+
15
+ if (klass == target_class) {
16
+ st_insert(g_allocated_objects, (st_data_t)obj, 1);
17
+ }
18
+ }
19
+
20
+ static void
21
+ freeobj_i(VALUE tpval, void *data)
22
+ {
23
+ rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
24
+ VALUE obj = rb_tracearg_object(tparg);
25
+ st_data_t key, val;
26
+
27
+ if (st_lookup(g_allocated_objects, (st_data_t)obj, &val)) {
28
+ st_delete(g_allocated_objects, &key, &val);
29
+ }
30
+ }
31
+
32
+ static VALUE
33
+ setup_traces(VALUE self, VALUE klass)
34
+ {
35
+ VALUE traces = rb_ary_new();
36
+ rb_ary_push(traces, rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_NEWOBJ, newobj_i, (void *)klass));
37
+ rb_ary_push(traces, rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_FREEOBJ, freeobj_i, NULL));
38
+ return traces;
39
+ }
40
+
41
+ static int
42
+ each_target_objects_i(st_data_t key, st_data_t val, st_data_t data)
43
+ {
44
+ VALUE obj = (VALUE)key;
45
+ rb_yield(obj);
46
+ return ST_CONTINUE;
47
+ }
48
+
49
+ static VALUE
50
+ each_target_objects(VALUE self)
51
+ {
52
+ st_foreach(g_allocated_objects, each_target_objects_i, 0);
53
+ return self;
54
+ }
55
+
56
+ void
57
+ Init_finalize_block(void)
58
+ {
59
+ c_FinalizeBlock = rb_define_class("FinalizeBlock", rb_cObject);
60
+ rb_define_method(c_FinalizeBlock, "setup_traces", setup_traces, 1);
61
+ rb_define_method(c_FinalizeBlock, "each_target_objects", each_target_objects, 0);
62
+ g_allocated_objects = st_init_numtable();
63
+ }
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "finalize_block/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "finalize_block"
8
+ spec.version = FinalizeBlock::VERSION
9
+ spec.authors = ["Koichi Sasada"]
10
+ spec.email = ["ko1@atdot.net"]
11
+
12
+ spec.summary = %q{Joke gem - This gem provide `finalize_block` method.}
13
+ spec.description = %q{Joke gem - This gem provide `finalize_block` method.}
14
+ spec.homepage = "https://github.com/ko1/finalize_block"
15
+ spec.license = "MIT"
16
+
17
+ =begin
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+ =end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.16"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "minitest", "~> 5.0"
40
+ spec.add_development_dependency "rake-compiler"
41
+ end
@@ -0,0 +1,22 @@
1
+ require 'finalize_block.so'
2
+ require "finalize_block/version"
3
+
4
+ class FinalizeBlock
5
+ def initialize rules
6
+ raise "Now, one rule should be accepted: #{rules.inspect}" if rules.keys.size != 1
7
+ rules.each{|k, v| @klass = k; @action = v}
8
+ @traces = setup_traces @klass
9
+ @traces.each{|trace| trace.enable}
10
+ yield
11
+ ensure
12
+ @traces.each{|trace| trace.disable}
13
+
14
+ each_target_objects do |obj|
15
+ @action.call(obj)
16
+ end
17
+ end
18
+ end
19
+
20
+ def finalize_block rules, &b
21
+ FinalizeBlock.new rules, &b
22
+ end
@@ -0,0 +1,3 @@
1
+ class FinalizeBlock
2
+ VERSION = "0.1.0"
3
+ end
data/test.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'finalize_block'
2
+
3
+ finalize_block String => -> str do
4
+ # clear procedure by replacing with emtpy string.
5
+ str.replace ''
6
+ end do
7
+ $g = 'a' * 256
8
+ p $g.size #=> 256
9
+ end
10
+
11
+ p $g #=> '' # cleared
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: finalize_block
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Koichi Sasada
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-29 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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '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'
69
+ description: Joke gem - This gem provide `finalize_block` method.
70
+ email:
71
+ - ko1@atdot.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - ext/finalize_block/extconf.rb
85
+ - ext/finalize_block/finalize_block.c
86
+ - finalize_block.gemspec
87
+ - lib/finalize_block.rb
88
+ - lib/finalize_block/version.rb
89
+ - test.rb
90
+ homepage: https://github.com/ko1/finalize_block
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.7.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Joke gem - This gem provide `finalize_block` method.
114
+ test_files: []