multiblocks 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ require 'multiblocks'
2
+
3
+ # run with "demo" as an argument to see how it works and see the
4
+ # #demo method in the "if $0 == __FILE__" part for a simple example
5
+ # of usage (you can pass also a numeric argument)
6
+
7
+ # asks for key-block pairs, then executes blocks in the order of the keys
8
+ class PermuteBlocks
9
+ callgroup(:do,:&)
10
+ def sanitizer
11
+ grpargv[-1][1].size == 1 and grpargv[-1][2]
12
+ end
13
+ grp_finalizer :end do
14
+ grpargv.sort_by {|a| a[1]}.each {|a| a[2][] }
15
+ end
16
+ end
17
+
18
+ # composes passed blocks (one's result is fed to the next one)
19
+ class Composer
20
+ callgroup(:do,:o)
21
+ def sanitizer
22
+ grpargv[-1][1].empty? and grpargv[-1][2]
23
+ end
24
+ grp_finalizer :end do |x|
25
+ grpargv.each {|a|
26
+ x=a[2][x]
27
+ }
28
+ x
29
+ end
30
+ end
31
+
32
+ if __FILE__ == $0
33
+
34
+ do_demo = $*.delete "demo"
35
+
36
+ $n = ($*[0] || 2).to_i
37
+
38
+ def demo
39
+ PermuteBlocks.new.do(2){puts "a"}.&(1){puts "b"}.&(3){puts "c"}.end
40
+ print "This is how PermuteBlocks' callgroups look like:\n "
41
+ p PermuteBlocks.callgroups
42
+
43
+ print "(#{$n}+1)**2 - 1 = "
44
+ p Composer.new.do{|x| x + 1}.o{|x| x*x}.o{|x| x-1}.end($n)
45
+ end
46
+
47
+ if do_demo
48
+ demo
49
+ exit
50
+ end
51
+
52
+ require 'stringio'
53
+ $sou = StringIO.open("","w")
54
+
55
+ def sou_stdout
56
+ stdout_orig = $>
57
+ begin
58
+ $> = $sou
59
+ yield
60
+ ensure
61
+ $> = stdout_orig
62
+ end
63
+ end
64
+
65
+ require "test/unit"
66
+
67
+ class Testie < Test::Unit::TestCase
68
+
69
+ def test_permute
70
+ sou_stdout do
71
+ PermuteBlocks.new.do(2){puts "a"}.&(1){puts "b"}.&(3){puts "c"}.end
72
+ end
73
+ assert_equal $sou.string, "b\na\nc\n"
74
+ assert_equal(
75
+ PermuteBlocks.callgroups,
76
+ {:@grpargv=>{:sanitizer=>:sanitizer, :calls=>[:do, :&], :finalizers=>[:end]}}
77
+ )
78
+ end
79
+
80
+ def test_composer
81
+ res = Composer.new.do{|x| x + 1}.o{|x| x*x}.o{|x| x-1}.end($n)
82
+ assert_equal res, ($n+1)**2-1
83
+ end
84
+
85
+ def test_overwrite
86
+ c = Class.new PermuteBlocks
87
+ assert_equal PermuteBlocks.callgroups, c.callgroups
88
+ c.class_eval { callgroup :x }
89
+ assert_equal c.callgroups, {:@grpargv => { :calls=>[:x], :sanitizer=>:sanitizer }}
90
+ end
91
+
92
+ def test_inheritance
93
+ c = Class.new PermuteBlocks
94
+ assert_equal PermuteBlocks.callgroups, c.callgroups
95
+ c.class_eval {
96
+ import_callgroups
97
+ callgroup :x
98
+ }
99
+ assert_equal(
100
+ c.callgroups,
101
+ {:@grpargv=>{:sanitizer=>:sanitizer, :calls=>[:do, :&, :x], :finalizers=>[:end]}}
102
+ )
103
+ end
104
+
105
+ def test_named_callgroup
106
+ c = Class.new
107
+ c.class_eval { callgroup(:x) { %w(@foogrp insane) } }
108
+ assert_equal(
109
+ c.callgroups,
110
+ {:@foogrp=>{:sanitizer=>:insane, :calls=>[:x] }}
111
+ )
112
+ end
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.1
3
+ specification_version: 1
4
+ name: multiblocks
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2005-03-19
8
+ summary: multiblocks is a framework for emulating Smalltalk-like method calls which can take more than one block parameter
9
+ require_paths:
10
+ - lib
11
+ author: Csaba Henk
12
+ email: csaba-ruby@creo.hu
13
+ homepage: http://www.creo.hu/~csaba/ruby/multiblocks
14
+ rubyforge_project: multiblocks
15
+ description:
16
+ autorequire: multiblocks
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: true
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ -
23
+ - ">"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.0.0
26
+ version:
27
+ platform: ruby
28
+ files:
29
+ - "."
30
+ - "./makedoc.rb"
31
+ - "./Changelog"
32
+ - "./multiblocks.gemspec"
33
+ - "./examples"
34
+ - "./examples/method_if.rb"
35
+ - "./examples/README"
36
+ - "./setup.rb"
37
+ - "./README"
38
+ - "./multiblocks-0.1.0.gem"
39
+ - "./test"
40
+ - "./test/demo.rb"
41
+ - "./lib"
42
+ - "./lib/multiblocks.rb"
43
+ test_files:
44
+ - test/demo.rb
45
+ rdoc_options:
46
+ - "-a"
47
+ extra_rdoc_files: []
48
+ executables: []
49
+ extensions: []
50
+ requirements: []
51
+ dependencies: []