tiger 0.0.2
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +110 -0
- data/Rakefile +1 -0
- data/lib/tiger.rb +26 -0
- data/lib/tiger/class_method.rb +26 -0
- data/lib/tiger/version.rb +3 -0
- data/test/test_tiger.rb +106 -0
- data/tiger.gemspec +23 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ead7bdf43a75449e998429c0181e2a0f77bf4c09
|
4
|
+
data.tar.gz: 4815a48505611d2434f94cd2633a6ba3d3e777eb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 913a6ed14c5c8f22ed24eca96a8805c177ba22c5fc462619213212680d8f9ac3e9270b0b92bc60463a179f00d8ba6483ffbba24dae82d57ed240c988f1dc2afc
|
7
|
+
data.tar.gz: d1d567ae83bd596a8c30bb416b90dd765067fb0e1f1476b62ad30e215bb471e1eb559fdd06e5fe1bd7a02853ff7e13aa273b348f54d9871c1b985599bd19381e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 hisaichi5518
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Tiger
|
2
|
+
|
3
|
+
Tiger is a mixin class to on/emit triggers (or hooks) that get called at some points you specify.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'tiger'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install tiger
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'tiger'
|
23
|
+
|
24
|
+
module YourModule
|
25
|
+
include Tiger
|
26
|
+
|
27
|
+
def self.hoge(*args)
|
28
|
+
emit(:before_hoge, args)
|
29
|
+
# ...
|
30
|
+
emit(:after_hoge, args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
YourModule.on(:before_hoge) do |args|
|
35
|
+
puts "before hoge"
|
36
|
+
end
|
37
|
+
|
38
|
+
YourModule.on(:after_hoge) do |args|
|
39
|
+
puts "after hoge"
|
40
|
+
end
|
41
|
+
|
42
|
+
YourModule.hoge({name: "hisaichi5518"})
|
43
|
+
#=> before hoge
|
44
|
+
#=> after hoge
|
45
|
+
```
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
require 'tiger'
|
49
|
+
|
50
|
+
class YourClass
|
51
|
+
include Tiger
|
52
|
+
|
53
|
+
def self.hoge(*args)
|
54
|
+
emit(:before_hoge, args)
|
55
|
+
# ...
|
56
|
+
emit(:after_hoge, args)
|
57
|
+
end
|
58
|
+
|
59
|
+
def fuga(*args)
|
60
|
+
emit_all(:before_fuga, args)
|
61
|
+
# ...
|
62
|
+
emit_all(:after_fuga, args)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
YourClass.on(:before_hoge) do |args|
|
68
|
+
puts "before hoge"
|
69
|
+
end
|
70
|
+
|
71
|
+
YourClass.on(:after_hoge) do |args|
|
72
|
+
puts "after hoge"
|
73
|
+
end
|
74
|
+
YourClass.hoge({name: "hisaichi5518"})
|
75
|
+
#=> before hoge
|
76
|
+
#=> after hoge
|
77
|
+
|
78
|
+
YourClass.on(:before_fuga) do |args|
|
79
|
+
puts "before fuga"
|
80
|
+
end
|
81
|
+
YourClass.on(:after_fuga) do |args|
|
82
|
+
puts "after fuga"
|
83
|
+
end
|
84
|
+
your_class = YourClass.new
|
85
|
+
your_class.on :before_fuga do
|
86
|
+
puts "before_fuga: only your_class instance"
|
87
|
+
end
|
88
|
+
your_class.on :after_fuga do
|
89
|
+
puts "after_fuga: only your_class instance"
|
90
|
+
end
|
91
|
+
|
92
|
+
your_class.fuga({name: "hisaichi5518"})
|
93
|
+
#=> before fuga
|
94
|
+
#=> before_fuga: only your_class instance
|
95
|
+
#=> after fuga
|
96
|
+
#=> after_fuga: only your_class instance
|
97
|
+
|
98
|
+
your_class2 = YourClass.new
|
99
|
+
your_class2.fuga({name: "hisaichi5518"})
|
100
|
+
#=> before fuga
|
101
|
+
#=> after fuga
|
102
|
+
```
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
1. Fork it ( http://github.com/hisaichi5518/tiger-rb/fork )
|
107
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
108
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
109
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
110
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/tiger.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "tiger/version"
|
2
|
+
require "tiger/class_method"
|
3
|
+
|
4
|
+
module Tiger
|
5
|
+
include ClassMethod
|
6
|
+
|
7
|
+
def self.included(klass)
|
8
|
+
klass.extend ClassMethod
|
9
|
+
end
|
10
|
+
|
11
|
+
def all_triggers
|
12
|
+
module_triggers = self.class.triggers || {}
|
13
|
+
class_triggers = self.triggers || {}
|
14
|
+
|
15
|
+
module_triggers.merge(class_triggers) do |key, m, c|
|
16
|
+
m + c
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def emit_all(name, *args)
|
21
|
+
triggers = all_triggers[name] || []
|
22
|
+
triggers.each do |code|
|
23
|
+
self.instance_exec(*args, &code)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Tiger
|
2
|
+
module ClassMethod
|
3
|
+
attr_accessor :triggers
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@triggers = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def on(name, &code)
|
10
|
+
trigger(name).push(code)
|
11
|
+
end
|
12
|
+
|
13
|
+
def emit(name, *args)
|
14
|
+
trigger(name).each do |code|
|
15
|
+
self.instance_exec(*args, &code)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def trigger(name)
|
21
|
+
@triggers ||= {}
|
22
|
+
@triggers[name] ||= []
|
23
|
+
@triggers[name]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/test/test_tiger.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'minitest/unit'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'tiger'
|
4
|
+
|
5
|
+
class TestTigerClass
|
6
|
+
include Tiger
|
7
|
+
end
|
8
|
+
|
9
|
+
module TestTigerModule
|
10
|
+
include Tiger
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestTiger < MiniTest::Unit::TestCase
|
14
|
+
def test_class_on
|
15
|
+
test_tiger_class = ::TestTigerClass.new
|
16
|
+
|
17
|
+
assert test_tiger_class.triggers[:test_on].nil?
|
18
|
+
|
19
|
+
::TestTigerClass.on :test_on do
|
20
|
+
end
|
21
|
+
test_tiger_class.on :test_on do
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_equal ::TestTigerClass.triggers[:test_on].size, 1
|
25
|
+
assert_equal test_tiger_class.triggers[:test_on].size, 1
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_module_on
|
29
|
+
::TestTigerModule.triggers ||= {}
|
30
|
+
assert ::TestTigerModule.triggers[:test_on].nil?
|
31
|
+
|
32
|
+
::TestTigerModule.on :test_on do
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_equal ::TestTigerModule.triggers[:test_on].size, 1
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_class_emit
|
39
|
+
test_tiger_class = ::TestTigerClass.new
|
40
|
+
::TestTigerClass.on :test_emit do |args|
|
41
|
+
args[:count] += 1
|
42
|
+
args[:self] = self
|
43
|
+
end
|
44
|
+
test_tiger_class.on :test_emit do |args|
|
45
|
+
args[:count] += 1
|
46
|
+
args[:self] = self
|
47
|
+
end
|
48
|
+
|
49
|
+
args = {count: 0}
|
50
|
+
assert_equal args[:count], 0
|
51
|
+
test_tiger_class.emit(:test_emit, args)
|
52
|
+
assert_equal args[:count], 1
|
53
|
+
assert_instance_of TestTigerClass, args[:self]
|
54
|
+
|
55
|
+
::TestTigerClass.emit(:test_emit, args)
|
56
|
+
assert_equal args[:count], 2
|
57
|
+
assert_equal args[:self], TestTigerClass
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_moudle_emit
|
61
|
+
::TestTigerModule.on :test_emit do |args|
|
62
|
+
args[:count] += 1
|
63
|
+
args[:self] = self
|
64
|
+
end
|
65
|
+
|
66
|
+
args = {count: 0}
|
67
|
+
assert_equal args[:count], 0
|
68
|
+
|
69
|
+
::TestTigerModule.emit(:test_emit, args)
|
70
|
+
assert_equal args[:count], 1
|
71
|
+
assert_equal args[:self], TestTigerModule
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_class_all_triggers
|
75
|
+
test_tiger_class = ::TestTigerClass.new
|
76
|
+
|
77
|
+
assert test_tiger_class.all_triggers[:test_all_triggers].nil?
|
78
|
+
|
79
|
+
::TestTigerClass.on :test_all_triggers do |args|
|
80
|
+
args[:count] += 1
|
81
|
+
end
|
82
|
+
test_tiger_class.on :test_all_triggers do |args|
|
83
|
+
args[:count] += 1
|
84
|
+
end
|
85
|
+
|
86
|
+
assert_equal test_tiger_class.all_triggers[:test_all_triggers].size, 2
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_class_emit_all
|
90
|
+
test_tiger_class = ::TestTigerClass.new
|
91
|
+
::TestTigerClass.on :test_emit_all do |args|
|
92
|
+
args[:count] += 1
|
93
|
+
end
|
94
|
+
test_tiger_class.on :test_emit_all do |args|
|
95
|
+
args[:count] += 1
|
96
|
+
end
|
97
|
+
|
98
|
+
args = {count: 0}
|
99
|
+
assert_equal args[:count], 0
|
100
|
+
|
101
|
+
test_tiger_class.emit_all(:test_emit_all, args)
|
102
|
+
|
103
|
+
assert_equal args[:count], 2
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
data/tiger.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tiger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tiger"
|
8
|
+
spec.version = Tiger::VERSION
|
9
|
+
spec.authors = ["hisaichi5518"]
|
10
|
+
spec.email = ["hisaichi5518@gmail.com"]
|
11
|
+
spec.summary = %q{Tiger is a mixin class to on/emit triggers (or hooks) that get called at some points you specify.}
|
12
|
+
spec.description = %q{Tiger is a mixin class to on/emit triggers (or hooks) that get called at some points you specify.}
|
13
|
+
spec.homepage = "https://github.com/hisaichi5518/tiger"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hisaichi5518
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-27 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Tiger is a mixin class to on/emit triggers (or hooks) that get called
|
42
|
+
at some points you specify.
|
43
|
+
email:
|
44
|
+
- hisaichi5518@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/tiger.rb
|
55
|
+
- lib/tiger/class_method.rb
|
56
|
+
- lib/tiger/version.rb
|
57
|
+
- test/test_tiger.rb
|
58
|
+
- tiger.gemspec
|
59
|
+
homepage: https://github.com/hisaichi5518/tiger
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.0
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Tiger is a mixin class to on/emit triggers (or hooks) that get called at
|
83
|
+
some points you specify.
|
84
|
+
test_files:
|
85
|
+
- test/test_tiger.rb
|