bunraku 3.0.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.
- data/Manifest.txt +39 -0
- data/README.txt +25 -0
- data/Rakefile +93 -0
- data/examples/corechain.rb +46 -0
- data/examples/person.rb +67 -0
- data/examples/person2.rb +62 -0
- data/examples/person3.rb +72 -0
- data/examples/rentalshop.rb +127 -0
- data/examples/requires.rb +50 -0
- data/examples/scope.rb +63 -0
- data/hacks/add_method_by_method_of_Class.rb +18 -0
- data/hacks/add_method_to_Class.rb +12 -0
- data/hacks/add_method_with_local_context.rb +27 -0
- data/hacks/anonymous_method.rb +37 -0
- data/hacks/class_creation_events.rb +45 -0
- data/hacks/get_nested_class.rb +22 -0
- data/hacks/hook_method.rb +50 -0
- data/hacks/method_missing_chain.rb +33 -0
- data/hacks/method_missing_chain_override.rb +39 -0
- data/hacks/pluralize.rb +24 -0
- data/lib/bunraku.rb +3 -0
- data/lib/bunraku/base.rb +9 -0
- data/lib/bunraku/class_ext.rb +37 -0
- data/lib/bunraku/core.rb +257 -0
- data/lib/bunraku/exceptions.rb +10 -0
- data/lib/bunraku/proxies.rb +121 -0
- data/lib/bunraku/utils.rb +21 -0
- data/lib/bunraku/version.rb +9 -0
- data/scripts/makemanifest.rb +20 -0
- data/scripts/uproadwebsite.sh +18 -0
- data/setup.rb +1585 -0
- data/spec/def_context_spec.rb +34 -0
- data/spec/interaction_spec.rb +127 -0
- data/spec/require_spec.rb +44 -0
- data/spec/role2_spec.rb +116 -0
- data/spec/role_spec.rb +120 -0
- data/test/test_bunraku.rb +11 -0
- data/test/test_helper.rb +2 -0
- data/website/index.html +13 -0
- metadata +94 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
require "bunraku"
|
3
|
+
|
4
|
+
class Company
|
5
|
+
as_context
|
6
|
+
|
7
|
+
class Employer
|
8
|
+
as_role
|
9
|
+
def pay(salary)
|
10
|
+
context.Employees.paid(salary)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Employee
|
15
|
+
as_role
|
16
|
+
requires do |req|
|
17
|
+
req.method? :deposit
|
18
|
+
end
|
19
|
+
|
20
|
+
def paid(salary)
|
21
|
+
deposit(salary)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Person
|
27
|
+
def initialize
|
28
|
+
@salary = 0
|
29
|
+
end
|
30
|
+
attr :salary
|
31
|
+
def deposit(salary)
|
32
|
+
@salary += salary
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
tnaka = Person.new
|
37
|
+
sto = Person.new
|
38
|
+
tkahashi = Person.new
|
39
|
+
|
40
|
+
tdai = Company.new
|
41
|
+
tdai.Employee.bind(tnaka)
|
42
|
+
tdai.Employee.bind(sto)
|
43
|
+
tdai.Employee.bind(tkahashi)
|
44
|
+
tdai.Employer.bind(tkahashi)
|
45
|
+
|
46
|
+
tkahashi.pay 100
|
47
|
+
tkahashi.pay 150
|
48
|
+
p tnaka.salary #=> 250
|
49
|
+
p sto.salary #=> 250
|
50
|
+
p tkahashi.salary #=> 250
|
data/examples/scope.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
|
2
|
+
require "bunraku"
|
3
|
+
|
4
|
+
class Company
|
5
|
+
as_context
|
6
|
+
|
7
|
+
class Employer
|
8
|
+
as_role
|
9
|
+
def pay(salary)
|
10
|
+
context.Employees.paid(salary)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Employee
|
15
|
+
as_role
|
16
|
+
def paid(salary)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Person
|
22
|
+
def initialize
|
23
|
+
@salary = 0
|
24
|
+
end
|
25
|
+
attr :salary
|
26
|
+
def add_salary(salary)
|
27
|
+
@salary += salary
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
tnaka = Person.new
|
32
|
+
sto = Person.new
|
33
|
+
tkahashi = Person.new
|
34
|
+
|
35
|
+
tdai = Company.new
|
36
|
+
tdai.bind { |context|
|
37
|
+
context.Employee.bind(tnaka) do |b|
|
38
|
+
b.replace_to :paid, :add_salary
|
39
|
+
end
|
40
|
+
context.Employee.bind(sto) do |b|
|
41
|
+
b.def_method(:paid) do |salary|
|
42
|
+
add_salary(salary)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
context.Employer.bind(tkahashi)
|
46
|
+
}.call do
|
47
|
+
tkahashi.pay 100
|
48
|
+
tkahashi.pay 150
|
49
|
+
p tnaka.salary #=> 250
|
50
|
+
p sto.salary #=> 250
|
51
|
+
end
|
52
|
+
|
53
|
+
begin
|
54
|
+
tkahashi.pay 150
|
55
|
+
rescue => ex
|
56
|
+
p ex #=> NoMethodError
|
57
|
+
end
|
58
|
+
|
59
|
+
begin
|
60
|
+
sto.paid 150
|
61
|
+
rescue => ex
|
62
|
+
p ex #=> NoMethodError
|
63
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
def add_method(obj, name, &block)
|
3
|
+
mod = Module.new
|
4
|
+
mod.instance_eval do
|
5
|
+
define_method(name, &block)
|
6
|
+
end
|
7
|
+
obj.extend(mod)
|
8
|
+
end
|
9
|
+
|
10
|
+
obj0 = Object.new
|
11
|
+
obj = Object.new
|
12
|
+
|
13
|
+
family = "yamada"
|
14
|
+
add_method(obj, :foo) do |name|
|
15
|
+
p "Hello " + name + " " + family
|
16
|
+
end
|
17
|
+
|
18
|
+
obj.foo("taro")
|
19
|
+
|
20
|
+
family = "tanaka"
|
21
|
+
obj.foo("jiro")
|
22
|
+
|
23
|
+
begin
|
24
|
+
obj0.foo("saburo")
|
25
|
+
rescue
|
26
|
+
p "method foo not found"
|
27
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
def anonymous_method(obj, &block)
|
3
|
+
mod = Module.new
|
4
|
+
name = "__foo__"
|
5
|
+
mod.instance_eval do
|
6
|
+
define_method(name, &block)
|
7
|
+
end
|
8
|
+
obj.extend(mod)
|
9
|
+
meth = obj.method(name)
|
10
|
+
mod.instance_eval do
|
11
|
+
undef_method(name, &block)
|
12
|
+
end
|
13
|
+
meth
|
14
|
+
end
|
15
|
+
|
16
|
+
class Foo
|
17
|
+
def initialize(mid)
|
18
|
+
@mid = mid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
obj = Foo.new("J")
|
23
|
+
|
24
|
+
family = "yamada"
|
25
|
+
m1 = anonymous_method(obj) do |name|
|
26
|
+
p "Hello " + name + " " + @mid + " " + family
|
27
|
+
end
|
28
|
+
m2 = anonymous_method(obj) do |name|
|
29
|
+
p "Bye " + name + " " + @mid + " " + family
|
30
|
+
end
|
31
|
+
|
32
|
+
m1.call("taro")
|
33
|
+
|
34
|
+
family = "amada"
|
35
|
+
m2.call("jiro")
|
36
|
+
|
37
|
+
p obj.methods
|
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
class Class
|
3
|
+
def method_added(name)
|
4
|
+
p "method added " + name.to_s + " on " + self.to_s
|
5
|
+
end
|
6
|
+
def method_removed(name)
|
7
|
+
p "method removed " + name.to_s + " on " + self.to_s
|
8
|
+
end
|
9
|
+
def method_undefined(name)
|
10
|
+
p "method undefined " + name.to_s + " on " + self.to_s
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class SClass
|
15
|
+
def singleton_method_added(name)
|
16
|
+
p "singleton method added " + name.to_s + " on " + self.to_s
|
17
|
+
end
|
18
|
+
def singleton_method_removed(name)
|
19
|
+
p "method removed " + name.to_s + " on " + self.to_s
|
20
|
+
end
|
21
|
+
def singleton_method_undefined(name)
|
22
|
+
p "singleton method undefined " + name.to_s + " on " + self.to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class MyClass < SClass
|
27
|
+
def foo
|
28
|
+
end
|
29
|
+
def bar
|
30
|
+
end
|
31
|
+
def buzz
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
o = MyClass.new
|
36
|
+
o.class.instance_eval do
|
37
|
+
undef_method :foo
|
38
|
+
end
|
39
|
+
class << o
|
40
|
+
undef :bar
|
41
|
+
undef_method :buzz
|
42
|
+
def hoge
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
class Class
|
3
|
+
def as_context
|
4
|
+
define_method(:method_missing) do |name, *args|
|
5
|
+
self.class.const_get(name)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class MyClass
|
11
|
+
as_context
|
12
|
+
|
13
|
+
class E1
|
14
|
+
end
|
15
|
+
|
16
|
+
class E2
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
o = MyClass.new
|
21
|
+
p o.E1
|
22
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
def hook(obj)
|
3
|
+
cache_methods = {}
|
4
|
+
obj.methods.each do |name|
|
5
|
+
cache_methods[name.to_sym] = obj.method(name)
|
6
|
+
end
|
7
|
+
mod = Module.new
|
8
|
+
mod.instance_eval do
|
9
|
+
cache_methods.each_key do |name|
|
10
|
+
define_method(name) do |*args|
|
11
|
+
self.method_missing(name, *args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
define_method(:method_missing) do |name, *args|
|
15
|
+
p "call #{name}"
|
16
|
+
m = cache_methods[name]
|
17
|
+
return m.call(*args) if m
|
18
|
+
super(name, *args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
obj.extend(mod)
|
22
|
+
mod
|
23
|
+
end
|
24
|
+
|
25
|
+
def unhook(mod)
|
26
|
+
names = mod.instance_methods
|
27
|
+
mod.instance_eval do
|
28
|
+
names.each do |name|
|
29
|
+
define_method(name.to_sym) do |*args|
|
30
|
+
super(*args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
class Foo
|
39
|
+
def foo(name)
|
40
|
+
p "hello #{name}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
obj = Foo.new
|
45
|
+
m = hook(obj)
|
46
|
+
|
47
|
+
obj.foo("taro")
|
48
|
+
|
49
|
+
unhook(m)
|
50
|
+
obj.foo("jiro")
|
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
module Module1
|
3
|
+
def method_missing(name, *args)
|
4
|
+
p "Module 1"
|
5
|
+
super(name, *args)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
module Module2
|
11
|
+
def method_missing(name, *args)
|
12
|
+
p "Module 2"
|
13
|
+
super(name, *args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
module Module3
|
19
|
+
def method_missing(name, *args)
|
20
|
+
p "Module 3"
|
21
|
+
super(name, *args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
obj = Object.new
|
26
|
+
obj.extend(Module1)
|
27
|
+
obj.extend(Module2)
|
28
|
+
obj.extend(Module3)
|
29
|
+
|
30
|
+
begin
|
31
|
+
obj.foo
|
32
|
+
rescue
|
33
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
module Module1
|
3
|
+
def method_missing(name, *args)
|
4
|
+
p "Module 1"
|
5
|
+
super(name, *args)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
module Module2
|
11
|
+
def method_missing(name, *args)
|
12
|
+
p "Module 2"
|
13
|
+
super(name, *args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
module Module3
|
19
|
+
def method_missing(name, *args)
|
20
|
+
p "Module 3"
|
21
|
+
super(name, *args)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
obj = Object.new
|
26
|
+
obj.extend(Module1)
|
27
|
+
obj.extend(Module2)
|
28
|
+
obj.extend(Module3)
|
29
|
+
|
30
|
+
module Module2
|
31
|
+
def method_missing(name, *args)
|
32
|
+
super(name, *args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
begin
|
37
|
+
obj.foo
|
38
|
+
rescue
|
39
|
+
end
|
data/hacks/pluralize.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# needs rubygems and rails activesupport
|
2
|
+
require "rubygems"
|
3
|
+
gem "activesupport"
|
4
|
+
require "active_support/inflector"
|
5
|
+
|
6
|
+
include Inflector
|
7
|
+
|
8
|
+
p (pluralize "Employee")
|
9
|
+
p (pluralize "Employer")
|
10
|
+
p (singularize "Employees")
|
11
|
+
p (singularize "Employers")
|
12
|
+
|
13
|
+
require "active_support/core_ext/string/inflections"
|
14
|
+
class String
|
15
|
+
include ActiveSupport::CoreExtensions::String::Inflections
|
16
|
+
end
|
17
|
+
|
18
|
+
p "Employer".pluralize
|
19
|
+
p "Employee".pluralize
|
20
|
+
p "Employers".singularize
|
21
|
+
p "Employees".singularize
|
22
|
+
|
23
|
+
p "stewardess".pluralize
|
24
|
+
p "stewardesses".singularize
|
data/lib/bunraku.rb
ADDED