dslkit 0.2.1 → 0.2.2

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/CHANGES CHANGED
@@ -1,3 +1,8 @@
1
+ 2007-04-11 (0.2.2)
2
+ * Ported Reginald Braithwaite's let DSL to DSLKit as examples/let.rb, see
3
+ http://weblog.raganwald.com/2007/03/approach-to-composing-domain-specific.html.
4
+ Added BlockSelf and MethodMissingDelegator modules for the implementation.
5
+ * Added version.rb.
1
6
  2007-02-22 (0.2.1)
2
7
  * Add coverage to tests.
3
8
  * New delegate method for classes/modules.
data/Rakefile CHANGED
@@ -32,9 +32,6 @@ task :clean do
32
32
  rm_rf %w[doc coverage]
33
33
  end
34
34
 
35
- desc "Prepare a release"
36
- task :release => [:clean, :package]
37
-
38
35
  spec = Gem::Specification.new do |s|
39
36
  #### Basic information.
40
37
 
@@ -85,4 +82,24 @@ Rake::GemPackageTask.new(spec) do |pkg|
85
82
  pkg.need_tar = true
86
83
  pkg.package_files += PKG_FILES
87
84
  end
85
+
86
+ desc m = "Writing version information for #{PKG_VERSION}"
87
+ task :version do
88
+ puts m
89
+ File.open(File.join('lib', 'dslkit', 'version.rb'), 'w') do |v|
90
+ v.puts <<EOT
91
+ module DSLKit
92
+ # DSLKit version
93
+ VERSION = '#{PKG_VERSION}'
94
+ VERSION_ARRAY = VERSION.split(/\\./).map { |x| x.to_i } # :nodoc:
95
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
96
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
97
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
98
+ end
99
+ EOT
100
+ end
101
+ end
102
+
103
+ desc "Prepare a release"
104
+ task :release => [:clean, :version, :package]
88
105
  # vim: set et sw=2 ts=2:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
data/examples/let.rb ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dslkit/polite'
4
+
5
+ LetScope = DSLKit::BlankSlate.with :instance_eval, :to_s, :inspect, :extend
6
+ class LetScope
7
+ include DSLKit::MethodMissingDelegator::DelegatorModule
8
+ include DSLKit::BlockSelf
9
+
10
+ def initialize(my_self, bindings = {}, outer_scope = nil)
11
+ super(my_self)
12
+ @outer_scope = outer_scope
13
+ @bindings = bindings
14
+ extend DSLKit::Eigenclass
15
+ eigenclass_eval { extend DSLKit::Constant }
16
+ each_binding do |name, value|
17
+ eigenclass_eval { constant name, value }
18
+ end
19
+ end
20
+
21
+ def each_binding(&block)
22
+ if @outer_scope
23
+ @outer_scope.each_binding(&block)
24
+ end
25
+ @bindings.each(&block)
26
+ end
27
+
28
+ def let(bindings = {}, &block)
29
+ ls = LetScope.new(block_self(&block), bindings, self)
30
+ ls.instance_eval(&block)
31
+ end
32
+
33
+ # Inlcuding this module into your current namespace defines the let method.
34
+ module Include
35
+ include DSLKit::BlockSelf
36
+
37
+ def let(bindings = {}, &block)
38
+ ls = LetScope.new(block_self(&block), bindings)
39
+ ls.instance_eval(&block)
40
+ end
41
+ end
42
+ end
43
+
44
+ if $0 == __FILE__
45
+ class Foo
46
+ include LetScope::Include
47
+
48
+ def twice(x)
49
+ 2 * x
50
+ end
51
+
52
+ def test
53
+ let :x => 1, :y => twice(1) do
54
+ let :z => twice(x) do
55
+ puts "#{x} * #{y} == #{z} # => #{x * y == twice(x)}"
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ Foo.new.test
62
+ end
data/examples/mail.rb CHANGED
@@ -4,9 +4,11 @@ require 'net/smtp'
4
4
  class Mail
5
5
  extend DSLKit::DSLAccessor
6
6
  include DSLKit::InstanceExec
7
+ include DSLKit::MethodMissingDelegator::DelegatorModule
8
+ include DSLKit::BlockSelf
7
9
 
8
- def initialize(dispatch = nil, &block)
9
- @dispatch = dispatch
10
+ def initialize(&block)
11
+ super block_self(&block)
10
12
  instance_exec(&block)
11
13
  end
12
14
 
@@ -48,18 +50,10 @@ class Mail
48
50
  smtp.send_message msg, from, to
49
51
  end
50
52
  end
51
-
52
- def method_missing(id, *args, &block)
53
- if @dispatch
54
- @dispatch.instance_eval { __send__(id, *args, &block) }
55
- else
56
- super
57
- end
58
- end
59
53
  end
60
54
 
61
55
  def mail(&block)
62
- Mail.new(self, &block)
56
+ Mail.new(&block)
63
57
  end
64
58
 
65
59
  def prompt
@@ -67,10 +61,11 @@ def prompt
67
61
  STDIN.gets.strip
68
62
  end
69
63
 
70
- rcpt = prompt
71
64
  m = mail do
72
65
  subject subject + ': Hi!'
73
- to rcpt unless rcpt.empty?
66
+ if rcpt = prompt
67
+ to rcpt
68
+ end
74
69
  body "Hello, world!\n"
75
70
  end
76
71
  m.send
data/examples/mm.rb CHANGED
@@ -44,7 +44,7 @@ module MM
44
44
  if name[-1] == ?=
45
45
  name = name[0..-2].intern
46
46
  value >= 0 or raise InterpreterError,
47
- "only non-negative numbers can be stored in registers"
47
+ "only non-negative numbers can be stored in register #{name}"
48
48
  @registers[name] = value
49
49
  else
50
50
  super
@@ -137,7 +137,7 @@ module MM
137
137
  end
138
138
  end
139
139
 
140
- #if $0 == __FILE__
140
+ if $0 == __FILE__
141
141
  if ARGV.empty?
142
142
  MM::Interpreter.new(STDIN.read).run
143
143
  else
@@ -145,4 +145,4 @@ end
145
145
  interpreter.stepping = !ARGV.empty?
146
146
  interpreter.run
147
147
  end
148
- #end
148
+ end
@@ -95,5 +95,3 @@ class Unit
95
95
  "#@n #{name}#{@n > 1 ? 's' : ''}"
96
96
  end
97
97
  end
98
-
99
-
data/install.rb CHANGED
@@ -9,9 +9,10 @@ include Config
9
9
  src = File.join(*%w[lib dslkit])
10
10
  dst = File.join(CONFIG["sitelibdir"], 'dslkit')
11
11
  mkdir_p dst
12
- for file in %w[polite.rb rude.rb]
13
- install File.join(src, file), File.join(dst, file)
12
+ cd src do
13
+ for file in Dir['*.rb']
14
+ install file, File.join(dst, file)
15
+ end
14
16
  end
15
- install File.join('lib', 'dslkit.rb'), File.join(CONFIG["sitelibdir"], file)
16
-
17
- # vim: set et sw=4 ts=4:
17
+ install File.join('lib', 'dslkit.rb'), File.join(CONFIG["sitelibdir"], 'dslkit.rb')
18
+ # vim: set et sw=2 ts=2:
data/lib/dslkit/polite.rb CHANGED
@@ -31,6 +31,8 @@
31
31
  # subdirectory of this library.
32
32
  #
33
33
  module DSLKit
34
+ require 'dslkit/version'
35
+
34
36
  # This module contains some handy methods to deal with eigenclasses. Those
35
37
  # are also known as virtual classes, singleton classes, metaclasses, plus all
36
38
  # the other names Matz doesn't like enough to actually accept one of the
@@ -578,4 +580,53 @@ module DSLKit
578
580
  end
579
581
  end
580
582
  end
583
+
584
+ # This module includes the block_self module_function.
585
+ module BlockSelf
586
+ module_function
587
+
588
+ # This method returns the receiver _self_ of the context in which _block_
589
+ # was created.
590
+ def block_self(&block)
591
+ eval 'self', block.binding
592
+ end
593
+ end
594
+
595
+ # This module contains a configurable method missing delegator and can be
596
+ # mixed into a module/class.
597
+ module MethodMissingDelegator
598
+
599
+ # Including this module in your classes makes an _initialize_ method
600
+ # available, whose first argument is used as method_missing_delegator
601
+ # attribute. If a superior _initialize_ method was defined it is called
602
+ # with all arguments but the first.
603
+ module DelegatorModule
604
+ include DSLKit::MethodMissingDelegator
605
+
606
+ def initialize(delegator, *a, &b)
607
+ self.method_missing_delegator = delegator
608
+ super(*a, &b) if defined? super
609
+ end
610
+ end
611
+
612
+ # This class includes DelegatorModule and can be used as a superclass
613
+ # instead of including DelegatorModule.
614
+ class DelegatorClass
615
+ include DelegatorModule
616
+ end
617
+
618
+ # This object will be the receiver of all missing method calls, if it has a
619
+ # value other than nil.
620
+ attr_accessor :method_missing_delegator
621
+
622
+ # Delegates all missing method calls to _method_missing_delegator_ if this
623
+ # attribute has been set. Otherwise it will call super.
624
+ def method_missing(id, *a, &b)
625
+ unless method_missing_delegator.nil?
626
+ method_missing_delegator.__send__(id, *a, &b)
627
+ else
628
+ super
629
+ end
630
+ end
631
+ end
581
632
  end
data/lib/dslkit/rude.rb CHANGED
@@ -20,5 +20,6 @@ module DSLKit
20
20
  include DSLKit::Deflect
21
21
  include DSLKit::ThreadLocal
22
22
  include DSLKit::Eigenclass
23
+ include DSLKit::BlockSelf
23
24
  end
24
25
  end
@@ -0,0 +1,8 @@
1
+ module DSLKit
2
+ # DSLKit version
3
+ VERSION = '0.2.2'
4
+ VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
7
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
8
+ end
data/tests/test_common.rb CHANGED
@@ -185,4 +185,17 @@ class TC_DSLKit < Test::Unit::TestCase
185
185
  assert_equal 3, d.size
186
186
  assert_equal 3, d.length
187
187
  end
188
+
189
+ if defined? D3
190
+ def test_delegate
191
+ d = D3.new []
192
+ assert_equal 0, d.size
193
+ d.push 1
194
+ assert_equal [1], d.map { |x| x }
195
+ d.push 2
196
+ assert_equal [1, 2], d.map { |x| x }
197
+ d.push 3
198
+ assert_equal [1, 2, 3], d.map { |x| x }
199
+ end
200
+ end
188
201
  end
data/tests/test_polite.rb CHANGED
@@ -102,4 +102,7 @@ class D2
102
102
  delegate :length, :ary
103
103
  end
104
104
 
105
+ class D3 < DSLKit::MethodMissingDelegator::DelegatorClass
106
+ end
107
+
105
108
  require 'test_common'
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: dslkit
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2007-02-22 00:00:00 +01:00
6
+ version: 0.2.2
7
+ date: 2007-04-13 00:00:00 +02:00
8
8
  summary: Kit for building DSLs in Ruby
9
9
  require_paths:
10
10
  - lib
@@ -29,31 +29,33 @@ post_install_message:
29
29
  authors:
30
30
  - Florian Frank
31
31
  files:
32
+ - VERSION
33
+ - TODO
32
34
  - tests
33
- - lib
34
- - examples
35
- - Rakefile
36
35
  - GPL
37
- - TODO
38
- - VERSION
39
- - CHANGES
40
36
  - install.rb
37
+ - Rakefile
38
+ - examples
39
+ - lib
40
+ - CHANGES
41
+ - tests/runner.rb
41
42
  - tests/test_common.rb
42
43
  - tests/test_rude.rb
43
44
  - tests/test_polite.rb
44
- - tests/runner.rb
45
- - lib/dslkit
46
- - lib/dslkit.rb
47
- - lib/dslkit/polite.rb
48
- - lib/dslkit/rude.rb
49
45
  - examples/recipe_common.rb
50
- - examples/subtract.reg
46
+ - examples/multiply.reg
51
47
  - examples/mail.rb
48
+ - examples/recipe2.rb
52
49
  - examples/null_pattern.rb
50
+ - examples/subtract.reg
53
51
  - examples/recipe.rb
52
+ - examples/let.rb
54
53
  - examples/mm.rb
55
- - examples/multiply.reg
56
- - examples/recipe2.rb
54
+ - lib/dslkit
55
+ - lib/dslkit.rb
56
+ - lib/dslkit/rude.rb
57
+ - lib/dslkit/polite.rb
58
+ - lib/dslkit/version.rb
57
59
  test_files:
58
60
  - tests/runner.rb
59
61
  rdoc_options: []