contextr 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/History.txt +5 -18
  2. data/License.txt +20 -0
  3. data/Manifest.txt +15 -19
  4. data/README.txt +2 -49
  5. data/Rakefile +55 -88
  6. data/examples/general.rb +152 -0
  7. data/examples/ordering.rb +29 -0
  8. data/lib/contextr.rb +15 -15
  9. data/lib/contextr/class_methods.rb +90 -0
  10. data/lib/contextr/core_ext.rb +2 -0
  11. data/lib/contextr/core_ext/module.rb +18 -0
  12. data/lib/contextr/core_ext/object.rb +9 -0
  13. data/lib/contextr/event_machine.rb +71 -0
  14. data/lib/contextr/layer.rb +57 -381
  15. data/lib/contextr/modules/mutex_code.rb +22 -0
  16. data/lib/contextr/modules/unique_id.rb +13 -0
  17. data/lib/contextr/public_api.rb +58 -0
  18. data/lib/contextr/version.rb +2 -2
  19. data/lib/ext/active_support_subset.rb +85 -0
  20. data/spec/contextr_spec.rb +11 -0
  21. data/spec/spec.opts +1 -0
  22. data/spec/spec_helper.rb +1 -1
  23. data/test/test_contextr.rb +11 -0
  24. data/website/index.html +23 -101
  25. data/website/index.txt +27 -95
  26. data/website/stylesheets/screen.css +9 -0
  27. data/website/template.rhtml +3 -8
  28. metadata +21 -26
  29. data/COPYING.txt +0 -340
  30. data/LICENSE.txt +0 -57
  31. data/examples/education.rb +0 -71
  32. data/examples/fibonacci.rb +0 -124
  33. data/examples/with_current_context.rb +0 -29
  34. data/lib/contextr/contextr.rb +0 -160
  35. data/lib/core_ext/class.rb +0 -29
  36. data/lib/core_ext/module.rb +0 -62
  37. data/lib/core_ext/proc.rb +0 -53
  38. data/lib/ext/method_nature.rb +0 -80
  39. data/rake/group_spec_task.rb +0 -7
  40. data/rake/specific_group_spec_task.rb +0 -7
  41. data/spec/contextr/contextr_api_spec.rb +0 -126
  42. data/spec/contextr/contextr_class_side_spec.rb +0 -77
  43. data/spec/contextr/contextr_functional_spec.rb +0 -293
  44. data/spec/core_ext/module_spec.rb +0 -101
  45. data/spec/core_ext/proc_spec.rb +0 -61
  46. data/tasks/annotations.rb +0 -107
  47. data/test/contextr/test_contextr.rb +0 -7
@@ -1,57 +0,0 @@
1
- ContextR is copyrighted free software by Gregor Schmidt <ruby@schmidtwisser.de>.
2
- You can redistribute it and/or modify it under either the terms of the GPL
3
- (see COPYING file), or the conditions below:
4
-
5
- 1. You may make and give away verbatim copies of the source form of the
6
- software without restriction, provided that you duplicate all of the
7
- original copyright notices and associated disclaimers.
8
-
9
- 2. You may modify your copy of the software in any way, provided that
10
- you do at least ONE of the following:
11
-
12
- a) place your modifications in the Public Domain or otherwise
13
- make them Freely Available, such as by posting said
14
- modifications to Usenet or an equivalent medium, or by allowing
15
- the author to include your modifications in the software.
16
-
17
- b) use the modified software only within your corporation or
18
- organization.
19
-
20
- c) rename any non-standard executables so the names do not conflict
21
- with standard executables, which must also be provided.
22
-
23
- d) make other distribution arrangements with the author.
24
-
25
- 3. You may distribute the software in object code or executable
26
- form, provided that you do at least ONE of the following:
27
-
28
- a) distribute the executables and library files of the software,
29
- together with instructions (in the manual page or equivalent)
30
- on where to get the original distribution.
31
-
32
- b) accompany the distribution with the machine-readable source of
33
- the software.
34
-
35
- c) give non-standard executables non-standard names, with
36
- instructions on where to get the original software distribution.
37
-
38
- d) make other distribution arrangements with the author.
39
-
40
- 4. You may modify and include the part of the software into any other
41
- software (possibly commercial). But some files in the distribution
42
- are not written by the author, so that they are not under this terms.
43
-
44
- They are ./tasks/annotations.rb and all files under the lib/ext
45
- directory. See each file for the copying condition.
46
-
47
- 5. The scripts and library files supplied as input to or produced as
48
- output from the software do not automatically fall under the
49
- copyright of the software, but belong to whomever generated them,
50
- and may be sold commercially, and may be aggregated with this
51
- software.
52
-
53
- 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
- PURPOSE.
57
-
@@ -1,71 +0,0 @@
1
- require "rubygems"
2
- require "contextr"
3
-
4
- class Person
5
- attr_accessor :name, :address, :university
6
-
7
- def initialize name, address, university
8
- self.name = name
9
- self.address = address
10
- self.university = university
11
- end
12
-
13
- def to_s
14
- "Name: #{name}"
15
- end
16
- end
17
-
18
- class University
19
- attr_accessor :name, :address
20
-
21
- def initialize name, address
22
- self.name = name
23
- self.address = address
24
- end
25
-
26
- def to_s
27
- "Name: #{name}"
28
- end
29
- end
30
-
31
- class Person
32
- layer :address, :education
33
-
34
- address.post :to_s do | n |
35
- n.return_value += "; Address: #{address}"
36
- end
37
-
38
- education.post :to_s do | n |
39
- n.return_value += ";\n[Education] #{university}"
40
- end
41
- end
42
-
43
- class University
44
- layer :address
45
-
46
- address.post :to_s do | n |
47
- n.return_value += "; Address: #{address}"
48
- end
49
- end
50
-
51
-
52
- hpi = University.new( "Hasso-Plattner-Institut", "Potsdam" )
53
- somePerson = Person.new( "Gregor Schmidt", "Berlin", hpi )
54
-
55
- puts
56
- puts somePerson
57
- ContextR::with_layers :education do
58
- puts
59
- puts somePerson
60
-
61
- ContextR::with_layers :address do
62
- puts
63
- puts somePerson
64
-
65
- ContextR::without_layers :education do
66
- puts
67
- puts somePerson
68
- end
69
- end
70
- end
71
- puts
@@ -1,124 +0,0 @@
1
- require "rubygems"
2
- require "contextr"
3
-
4
- require 'test/unit'
5
- require 'benchmark'
6
-
7
- class Fibonacci
8
- class << self
9
- def compute fixnum
10
- if fixnum == 1 or fixnum == 0
11
- fixnum
12
- elsif fixnum < 0
13
- raise ArgumentError, "Fibonacci not defined for negative numbers"
14
- else
15
- compute(fixnum - 1) + compute(fixnum - 2)
16
- end
17
- end
18
-
19
- layer :fib_pre_post, :fib_wrap
20
- attr_accessor :cache
21
-
22
- fib_pre_post.pre :compute do | nature |
23
- self.cache ||= {}
24
- if self.cache.key? nature.arguments.first
25
- nature.break! self.cache[nature.arguments.first]
26
- end
27
- end
28
-
29
- fib_pre_post.post :compute do | nature |
30
- self.cache[nature.arguments.first] = nature.return_value
31
- end
32
-
33
- fib_wrap.wrap :compute do | nature |
34
- self.cache ||= {}
35
- if self.cache.key? nature.arguments.first
36
- nature.return_value = self.cache[nature.arguments.first]
37
- else
38
- nature.call_next
39
- self.cache[nature.arguments.first] = nature.return_value
40
- end
41
- end
42
- end
43
- end
44
-
45
- class Fixnum
46
- def fibonacci
47
- if self == 1 or self == 0
48
- self
49
- elsif self < 0
50
- raise ArgumentError, "Fibonacci not defined for negative numbers"
51
- else
52
- old_fib, fib = 0, 1
53
- for i in 2..self
54
- fib, old_fib = old_fib + fib, fib
55
- end
56
- fib
57
- end
58
- end
59
- end
60
-
61
- class FibonacciTest < Test::Unit::TestCase
62
- def setup
63
- Fibonacci.cache = {}
64
- end
65
-
66
- def test_basic_function
67
- Benchmark.bm(20) do |x|
68
- x.report("Recursive:") {
69
- assert_equal 0, Fibonacci.compute( 0 )
70
- assert_equal 1, Fibonacci.compute( 1 )
71
- assert_equal 1, Fibonacci.compute( 2 )
72
- assert_equal 55, Fibonacci.compute( 10 )
73
- assert_equal 6765, Fibonacci.compute( 20 )
74
- # The following are too hard for the simple solution
75
- assert_equal 75_025, 25.fibonacci
76
- assert_equal 9227465, 35.fibonacci
77
- assert_equal 280571172992510140037611932413038677189525,
78
- 200.fibonacci
79
- assert_equal 176023680645013966468226945392411250770384383304492191886725992896575345044216019675,
80
- 400.fibonacci
81
- }
82
- end
83
- end
84
-
85
- def test_layered_function_with_pre_post
86
- Benchmark.bm(20) do |x|
87
- x.report("Layered Pre/Post:") {
88
- ContextR.with_layers :fib_pre_post do
89
- assert_equal 0, Fibonacci.compute( 0 )
90
- assert_equal 1, Fibonacci.compute( 1 )
91
- assert_equal 1, Fibonacci.compute( 2 )
92
- assert_equal 55, Fibonacci.compute( 10 )
93
- assert_equal 6765, Fibonacci.compute( 20 )
94
- assert_equal 75_025, Fibonacci.compute( 25 )
95
- assert_equal 9227465, Fibonacci.compute( 35 )
96
- assert_equal 280571172992510140037611932413038677189525,
97
- Fibonacci.compute( 200 )
98
- assert_equal 176023680645013966468226945392411250770384383304492191886725992896575345044216019675,
99
- Fibonacci.compute( 400 )
100
- end
101
- }
102
- end
103
- end
104
-
105
- def test_layered_function_with_wrap
106
- Benchmark.bm(20) do |x|
107
- x.report("Layered Wrap:") {
108
- ContextR.with_layers :fib_wrap do
109
- assert_equal 0, Fibonacci.compute( 0 )
110
- assert_equal 1, Fibonacci.compute( 1 )
111
- assert_equal 1, Fibonacci.compute( 2 )
112
- assert_equal 55, Fibonacci.compute( 10 )
113
- assert_equal 6765, Fibonacci.compute( 20 )
114
- assert_equal 75_025, Fibonacci.compute( 25 )
115
- assert_equal 9227465, Fibonacci.compute( 35 )
116
- assert_equal 280571172992510140037611932413038677189525,
117
- Fibonacci.compute( 200 )
118
- assert_equal 176023680645013966468226945392411250770384383304492191886725992896575345044216019675,
119
- Fibonacci.compute( 400 )
120
- end
121
- }
122
- end
123
- end
124
- end
@@ -1,29 +0,0 @@
1
- require "rubygems"
2
- require "contextr"
3
-
4
- class A
5
- class << self
6
- def a
7
- "a"
8
- end
9
-
10
- layer :foo
11
-
12
- foo.post :a do | n |
13
- n.return_value << " post"
14
- end
15
- end
16
- end
17
-
18
- ContextR::add_context_sensor do
19
- [:foo]
20
- end
21
-
22
- puts A.a # => "a"
23
- ContextR::with_layer :foo do
24
- puts A.a # => "a post"
25
- end
26
- ContextR::with_current_context do
27
- puts A.a # => "a post"
28
- end
29
- puts A.a # => "a"
@@ -1,160 +0,0 @@
1
- module ContextR
2
-
3
- # This module is mixed into ContextR module, so that all public methods
4
- # are available via
5
- # ContextR::current_layers
6
- # ContextR::with_layers( layer_name, ... ) { ... }
7
- # ContextR::without_layers( layer_name, ... ) { ... }
8
- module ClassMethods
9
- # allows the explicit activation of layers within a block context
10
- #
11
- # ContextR::with_layers( :foo, :bar ) do
12
- # ContextR::current_layers # => [:default, :foo, :bar]
13
- #
14
- # ContextR::with_layers( :baz ) do
15
- # ContextR::current_layers # => [:default, :foo, :bar, :baz]
16
- # end
17
- #
18
- # end
19
- #
20
- # :call-seq:
21
- # with_layers( layer_name, ... ) { ... }
22
- #
23
- def with_layers( *layer_symbols, &block )
24
- layers = layer_symbols.collect do | layer_symbol |
25
- ContextR.layer_by_name( ContextR.layerize( layer_symbol ) )
26
- end
27
- Dynamic.let( { :layers => Dynamic[:layers] | layers }, &block )
28
- end
29
- alias with_layer with_layers
30
-
31
- # allows the explicit activation of layers
32
- #
33
- # ContextR::activate_layers( :foo, :bar )
34
- # ContextR::current_layers # => [:default, :foo, :bar]
35
- #
36
- # :call-seq:
37
- # deactivate_layers( layer_name, ... )
38
- #
39
- def activate_layers( *layer_symbols )
40
- layers = layer_symbols.collect do | layer_symbol |
41
- ContextR.layer_by_name( ContextR.layerize( layer_symbol ) )
42
- end
43
- Dynamic[:layers] |= layers
44
- end
45
- alias activate_layer activate_layers
46
-
47
- # allows the explicit deactivation of layers within a block context
48
- #
49
- # ContextR::with_layers( :foo, :bar ) do
50
- # ContextR::current_layers # => [:default, :foo, :bar]
51
- #
52
- # ContextR::without_layers( :foo ) do
53
- # ContextR::current_layers # => [:default, :bar]
54
- # end
55
- #
56
- # end
57
- #
58
- # :call-seq:
59
- # without_layers( layer_name, ... ) { ... }
60
- #
61
- def without_layers( *layer_symbols, &block )
62
- layers = layer_symbols.collect do | layer_symbol |
63
- ContextR.layer_by_name( ContextR.layerize( layer_symbol ) )
64
- end
65
- Dynamic.let( { :layers => Dynamic[:layers] - layers }, &block )
66
- end
67
- alias without_layer without_layers
68
-
69
- # allows the explicit deactivation of layers
70
- #
71
- # ContextR::activate_layers( :foo, :bar )
72
- # ContextR::current_layers # => [:default, :foo, :bar]
73
- #
74
- # ContextR::deactivate_layers( :foo )
75
- # ContextR::current_layers # => [:default, :bar]
76
- #
77
- # :call-seq:
78
- # deactivate_layers( layer_name, ... )
79
- #
80
- def deactivate_layers( *layer_symbols )
81
- layers = layer_symbols.collect do | layer_symbol |
82
- ContextR.layer_by_name( ContextR.layerize( layer_symbol ) )
83
- end
84
- Dynamic[:layers] -= layers
85
- end
86
- alias deactivate_layer deactivate_layers
87
-
88
- # returns the names of the currently activated layers
89
- #
90
- # ContextR::current_layers # => [:default]
91
- #
92
- # ContextR::with_layers :foo do
93
- # ContextR::current_layers # => [:default, :foo]
94
- # end
95
- def current_layers
96
- Dynamic[:layers].collect{ | layer_class | self.symbolize( layer_class ) }
97
- end
98
-
99
- # allows the registration of context sensors. These are blocks that are
100
- # called on with_current_context and should return a list of layers, that
101
- # should be activated.
102
- #
103
- # ContextR::add_context_sensor do
104
- # # some clever code computes some layers to activate
105
- # [ :foo ]
106
- # end
107
- #
108
- # :call-seq:
109
- # add_context_sensor() { ... }
110
- #
111
- def add_context_sensor &block
112
- @sensors ||= []
113
- @sensors << block
114
- end
115
-
116
- # asks all sensors to compute the current context, e.g. layers that should
117
- # be active, and executes the given block in the context. It works basically
118
- # like with_layers
119
- #
120
- # :call-seq:
121
- # with_current_context() { ... }
122
- #
123
- def with_current_context(&block)
124
- layers = @sensors.inject([]) do | akku, sensor |
125
- akku | sensor.call
126
- end
127
- ContextR::with_layers(*layers) do
128
- block.call
129
- end
130
- end
131
-
132
- def symbolize( layer_klass ) # :nodoc:
133
- layer_klass.namespace_free_name.gsub( "Layer", "" ).downcase.to_sym
134
- end
135
-
136
- def layerize( layer_symbol ) # :nodoc:
137
- "#{layer_symbol}_layer".camelize
138
- end
139
-
140
- def layer_by_symbol( layer_symbol ) # :nodoc:
141
- layer_by_name( layerize( layer_symbol ) )
142
- end
143
-
144
- def layer_by_name( layer_name ) # :nodoc:
145
- unless ContextR.const_defined?( layer_name )
146
- ContextR::module_eval(
147
- "class #{layer_name} < Layer; end", __FILE__, __LINE__ )
148
- # ContextR.const_set( layer_name, Class.new( ContextR::Layer ) )
149
- end
150
- ContextR.const_get( layer_name )
151
- end
152
-
153
- def current_layer # :nodoc:
154
- Layer.compose( Dynamic[:layers] )
155
- end
156
-
157
- end
158
-
159
- extend ClassMethods
160
- end
@@ -1,29 +0,0 @@
1
- # ContextR extends Class to allow the definition of context-dependent behaviour.
2
- #
3
- # After registering a layer within in class, it is accessable via its name,
4
- # to add behaviour to methods.
5
- class Class
6
- # register a layer to be used within a class body
7
- def layer( *layer_keys )
8
- layer_keys.each do | layer_key |
9
- layer_key = layer_key.to_s.downcase.to_sym
10
- layer_name = ContextR::layerize( layer_key )
11
- layer = ContextR.layer_by_name( layer_name )
12
- layer_in_class = ContextR::LayerInClass.new( self, layer )
13
-
14
- define_private_class_method( layer_key ) do
15
- layer_in_class
16
- end
17
- end
18
-
19
- nil
20
- end
21
-
22
- protected
23
- def define_private_class_method( symbol, &block ) # :nodoc:
24
- (class << self; self; end).instance_eval do
25
- define_method( symbol, block )
26
- private symbol
27
- end
28
- end
29
- end