dslkit 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,5 @@
1
+ 2007-11-29 (0.2.3)
2
+ * Now compatible to Ruby 1.9 (for the moment).
1
3
  2007-04-11 (0.2.2)
2
4
  * Ported Reginald Braithwaite's let DSL to DSLKit as examples/let.rb, see
3
5
  http://weblog.raganwald.com/2007/03/approach-to-composing-domain-specific.html.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
data/examples/mail.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'dslkit/polite'
2
2
  require 'net/smtp'
3
+ require 'time'
3
4
 
4
5
  class Mail
5
6
  extend DSLKit::DSLAccessor
@@ -30,7 +31,7 @@ class Mail
30
31
 
31
32
  def message_id
32
33
  key = [ ENV['HOSTNAME'] || 'localhost', $$ , Time.now ].join
33
- ::Digest::MD5.new(key).to_s
34
+ (::Digest::MD5.new << key).to_s
34
35
  end
35
36
 
36
37
  def msg
@@ -58,6 +59,7 @@ end
58
59
 
59
60
  def prompt
60
61
  STDOUT.print "Send to? "
62
+ STDOUT.flush
61
63
  STDIN.gets.strip
62
64
  end
63
65
 
data/examples/mm.rb CHANGED
@@ -6,17 +6,13 @@ require 'dslkit/polite'
6
6
  module MM
7
7
  class InterpreterError < StandardError; end
8
8
 
9
- class Label < Proc
10
- def initialize(interpreter, name, &block)
11
- @interpreter = interpreter
12
- @name = name
13
- super(&block)
14
- end
9
+ class ::Proc
10
+ attr_accessor :interpreter
15
11
 
16
- attr_reader :name
12
+ attr_accessor :name
17
13
 
18
14
  def execute
19
- @interpreter.display_registers(self)
15
+ interpreter.display_registers(self)
20
16
  call
21
17
  end
22
18
  end
@@ -88,7 +84,8 @@ module MM
88
84
  def label(name, &block)
89
85
  @labels.find { |l| l.name == name } and
90
86
  raise InterpreterError, "label named '#{name}' was already defined"
91
- @labels << Label.new(self, name, &block)
87
+ block.interpreter, block.name = self, name
88
+ @labels << block
92
89
  end
93
90
 
94
91
  def register_fetch(register)
data/lib/dslkit/polite.rb CHANGED
@@ -1,3 +1,8 @@
1
+ # :stopdoc:
2
+ require 'thread'
3
+ require 'sync'
4
+ # :startdoc:
5
+
1
6
  # = dslkit - Building Blocks for Domain Specific Languages (DSL)
2
7
  #
3
8
  # == Description
@@ -79,6 +84,17 @@ module DSLKit
79
84
  end
80
85
 
81
86
  module ThreadLocal
87
+ @@mutex = Mutex.new
88
+
89
+ @@cleanup = lambda do |my_object_id|
90
+ my_id = "__thread_local_#{my_object_id}__"
91
+ @mutex.synchronize do
92
+ for t in Thread.list
93
+ t[my_id] = nil if t[my_id]
94
+ end
95
+ end
96
+ end
97
+
82
98
  # Define a thread local variable named _name_ in this module/class. If the
83
99
  # value _value_ is given, it is used to initialize the variable.
84
100
  def thread_local(name, value = nil)
@@ -87,21 +103,7 @@ module DSLKit
87
103
  name = name.to_s
88
104
  my_id = "__thread_local_#{__id__}__"
89
105
 
90
- cleanup = eval <<-'EOT', TOPLEVEL_BINDING
91
- lambda do |my_object_id|
92
- my_id = "__thread_local_#{my_object_id}__"
93
- begin
94
- old = Thread.critical
95
- Thread.critical = true
96
- for t in Thread.list
97
- t[my_id] = nil if t[my_id]
98
- end
99
- ensure
100
- Thread.critical = old
101
- end
102
- end
103
- EOT
104
- ObjectSpace.define_finalizer(self, cleanup)
106
+ ObjectSpace.define_finalizer(self, @@cleanup)
105
107
 
106
108
  define_method(name) do
107
109
  Thread.current[my_id] ||= {}
@@ -138,7 +140,6 @@ module DSLKit
138
140
  def thread_global(name, value = nil)
139
141
  is_a?(Module) or raise TypeError, "receiver has to be a Module"
140
142
 
141
- require 'thread'
142
143
  name = name.to_s
143
144
  var_name = "@__#{name}_#{__id__.abs}__"
144
145
 
@@ -171,47 +172,49 @@ module DSLKit
171
172
  end
172
173
 
173
174
  module InstanceExec
174
- class << self
175
- attr_accessor :pool
176
- attr_accessor :count
177
- end
178
- self.count = 0
179
- self.pool = []
180
-
181
- # This is a pure ruby implementation of Ruby 1.9's instance_exec method. It
182
- # executes _block_ in the context of this object while parsing <i>*args</i> into
183
- # the block.
184
- def instance_exec(*args, &block)
185
- instance = self
186
- id = instance_exec_fetch_symbol
187
- InstanceExec.module_eval do
188
- begin
189
- define_method id, block
190
- instance.__send__ id, *args
191
- ensure
192
- remove_method id if method_defined?(id)
175
+ unless (Object.instance_method(:instance_exec) rescue nil)
176
+ class << self
177
+ attr_accessor :pool
178
+ attr_accessor :count
179
+ end
180
+ self.count = 0
181
+ self.pool = []
182
+
183
+ # This is a pure ruby implementation of Ruby 1.9's instance_exec method. It
184
+ # executes _block_ in the context of this object while parsing <i>*args</i> into
185
+ # the block.
186
+ def instance_exec(*args, &block)
187
+ instance = self
188
+ id = instance_exec_fetch_symbol
189
+ InstanceExec.module_eval do
190
+ begin
191
+ define_method id, block
192
+ instance.__send__ id, *args
193
+ ensure
194
+ remove_method id if method_defined?(id)
195
+ end
193
196
  end
197
+ ensure
198
+ InstanceExec.pool << id
194
199
  end
195
- ensure
196
- InstanceExec.pool << id
197
- end
198
200
 
199
- private
201
+ private
200
202
 
201
- # Fetch a symbol from a pool in thread save way. If no more symbols are
202
- # available create a new one, that will be pushed into the pool later.
203
- def instance_exec_fetch_symbol
204
- old = Thread.critical
205
- Thread.critical = true
206
- if InstanceExec.pool.empty?
207
- InstanceExec.count += 1
208
- symbol = :"__instance_exec_#{InstanceExec.count}__"
209
- else
210
- symbol = InstanceExec.pool.shift
203
+ @@mutex = Mutex.new
204
+
205
+ # Fetch a symbol from a pool in thread save way. If no more symbols are
206
+ # available create a new one, that will be pushed into the pool later.
207
+ def instance_exec_fetch_symbol
208
+ @@mutex.synchronize do
209
+ if InstanceExec.pool.empty?
210
+ InstanceExec.count += 1
211
+ symbol = :"__instance_exec_#{InstanceExec.count}__"
212
+ else
213
+ symbol = InstanceExec.pool.shift
214
+ end
215
+ return symbol
216
+ end
211
217
  end
212
- return symbol
213
- ensure
214
- Thread.critical = old
215
218
  end
216
219
  end
217
220
 
@@ -408,7 +411,8 @@ module DSLKit
408
411
  klass = Class.new
409
412
  klass.instance_eval do
410
413
  instance_methods.each do |m|
411
- undef_method m unless m =~ /^__/ or ids.any? { |i| i === m }
414
+ m = m.to_s
415
+ undef_method m unless m =~ /^(__|object_id)/ or ids.any? { |i| i === m }
412
416
  end
413
417
  end
414
418
  klass
@@ -477,28 +481,26 @@ module DSLKit
477
481
  end
478
482
  end
479
483
 
484
+ @@sync = Sync.new
485
+
480
486
  # Start deflecting method calls named _id_ to the _from_ class using the
481
487
  # Deflector instance deflector.
482
488
  def deflect_start(from, id, deflector)
483
- old = Thread.critical
484
- Thread.critical = true
485
- Deflect.deflecting ||= DeflectorCollection.new
486
- Deflect.deflecting.member?(from, id) and
487
- raise DeflectError, "#{from}##{id} is already deflected"
488
- Deflect.deflecting.add(from, id, deflector)
489
- from.class_eval do
490
- define_method(id) do |*args|
491
- if Deflect.deflecting and d = Deflect.deflecting.find(self.class, id)
492
- Thread.critical = old
493
- d.call(self, id, *args)
494
- else
495
- Thread.critical = old
496
- super
489
+ @@sync.synchronize do
490
+ Deflect.deflecting ||= DeflectorCollection.new
491
+ Deflect.deflecting.member?(from, id) and
492
+ raise DeflectError, "#{from}##{id} is already deflected"
493
+ Deflect.deflecting.add(from, id, deflector)
494
+ from.class_eval do
495
+ define_method(id) do |*args|
496
+ if Deflect.deflecting and d = Deflect.deflecting.find(self.class, id)
497
+ d.call(self, id, *args)
498
+ else
499
+ super(*args)
500
+ end
497
501
  end
498
502
  end
499
503
  end
500
- ensure
501
- Thread.critical = old
502
504
  end
503
505
 
504
506
  # Return true if method _id_ is deflected from class _from_, otherwise
@@ -517,24 +519,23 @@ module DSLKit
517
519
  # Deflector instance deflector. After that yield to the given block and
518
520
  # stop deflecting again.
519
521
  def deflect(from, id, deflector)
520
- old = Thread.critical
521
- Thread.critical = true
522
- deflect_start(from, id, deflector)
523
- yield
524
- ensure
525
- deflect_stop(from, id)
526
- Thread.critical = old
522
+ @@sync.synchronize do
523
+ begin
524
+ deflect_start(from, id, deflector)
525
+ yield
526
+ ensure
527
+ deflect_stop(from, id)
528
+ end
529
+ end
527
530
  end
528
531
 
529
532
  # Stop deflection method calls named _id_ to class _from_.
530
533
  def deflect_stop(from, id)
531
- old = Thread.critical
532
- Thread.critical = true
533
- Deflect.deflecting.delete(from, id) or
534
- raise DeflectError, "#{from}##{id} is not deflected from"
535
- from.instance_eval { remove_method id }
536
- ensure
537
- Thread.critical = old
534
+ @@sync.synchronize do
535
+ Deflect.deflecting.delete(from, id) or
536
+ raise DeflectError, "#{from}##{id} is not deflected from"
537
+ from.instance_eval { remove_method id }
538
+ end
538
539
  end
539
540
  end
540
541
 
@@ -588,7 +589,7 @@ module DSLKit
588
589
  # This method returns the receiver _self_ of the context in which _block_
589
590
  # was created.
590
591
  def block_self(&block)
591
- eval 'self', block.binding
592
+ eval 'self', block.__send__(:binding)
592
593
  end
593
594
  end
594
595
 
@@ -1,6 +1,6 @@
1
1
  module DSLKit
2
2
  # DSLKit version
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: dslkit
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.2
7
- date: 2007-04-13 00:00:00 +02:00
6
+ version: 0.2.3
7
+ date: 2007-11-29 00:00:00 +01:00
8
8
  summary: Kit for building DSLs in Ruby
9
9
  require_paths:
10
10
  - lib
@@ -29,33 +29,33 @@ post_install_message:
29
29
  authors:
30
30
  - Florian Frank
31
31
  files:
32
- - VERSION
33
- - TODO
34
- - tests
35
- - GPL
36
32
  - install.rb
37
- - Rakefile
38
- - examples
39
33
  - lib
34
+ - lib/dslkit
35
+ - lib/dslkit/rude.rb
36
+ - lib/dslkit/polite.rb
37
+ - lib/dslkit/version.rb
38
+ - lib/dslkit.rb
40
39
  - CHANGES
41
- - tests/runner.rb
40
+ - TODO
41
+ - VERSION
42
+ - tests
42
43
  - tests/test_common.rb
43
- - tests/test_rude.rb
44
+ - tests/runner.rb
44
45
  - tests/test_polite.rb
45
- - examples/recipe_common.rb
46
- - examples/multiply.reg
47
- - examples/mail.rb
46
+ - tests/test_rude.rb
47
+ - Rakefile
48
+ - GPL
49
+ - examples
50
+ - examples/recipe.rb
51
+ - examples/mm.rb
48
52
  - examples/recipe2.rb
49
- - examples/null_pattern.rb
50
53
  - examples/subtract.reg
51
- - examples/recipe.rb
54
+ - examples/null_pattern.rb
55
+ - examples/recipe_common.rb
52
56
  - examples/let.rb
53
- - examples/mm.rb
54
- - lib/dslkit
55
- - lib/dslkit.rb
56
- - lib/dslkit/rude.rb
57
- - lib/dslkit/polite.rb
58
- - lib/dslkit/version.rb
57
+ - examples/multiply.reg
58
+ - examples/mail.rb
59
59
  test_files:
60
60
  - tests/runner.rb
61
61
  rdoc_options: []