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.
@@ -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
@@ -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,18 @@
1
+
2
+ class Class
3
+ private
4
+ def as_context
5
+ # self.class == Class
6
+ define_method(:bind) do
7
+ # self.class == object class
8
+ p self.class
9
+ end
10
+ end
11
+ end
12
+
13
+ class MyClass
14
+ as_context
15
+ end
16
+
17
+ o = MyClass.new
18
+ o.bind
@@ -0,0 +1,12 @@
1
+
2
+ class Class
3
+ def as_context
4
+ p "#{self.to_s} as context"
5
+ end
6
+ end
7
+
8
+ class MyClass
9
+ as_context
10
+ end
11
+
12
+ MyClass.as_context
@@ -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
@@ -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
@@ -0,0 +1,3 @@
1
+ Dir[File.join(File.dirname(__FILE__), 'bunraku/**/*.rb')].sort.each do |lib|
2
+ require lib
3
+ end
@@ -0,0 +1,9 @@
1
+
2
+ require "rubygems"
3
+ gem "activesupport"
4
+
5
+ require "active_support/core_ext/string/inflections"
6
+ class String
7
+ include ActiveSupport::CoreExtensions::String::Inflections
8
+ end
9
+