dslkit 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .*.sw[pon]
2
+ .rvmrc
2
3
  Gemfile.lock
3
4
  pkg
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm use ruby-1.9.3-rc1
2
+ rvm gemset create dslkit
3
+ rvm gemset use dslkit
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ 2011-10-07 (0.2.9)
2
+ * Add ParameterizedModule and FromModule.
3
+ 2011-09-26 (0.2.8)
4
+ * Depend on tins instead of spruz.
1
5
  2011-07-16 (0.2.7)
2
6
  * Added turing machines example.
3
7
  2009-07-20 (0.2.6)
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ GemHadar do
11
11
  summary 'Kit for building DSLs in Ruby'
12
12
  description 'This library contains recurring patterns, that are useful in the creation of internal Domain Specific Languages (DSL) in Ruby.'
13
13
  test_dir 'tests'
14
- ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock'
14
+ ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc'
15
15
  readme 'README.rdoc'
16
16
 
17
17
  dependency 'term-ansicolor', '~>1.0'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.8
1
+ 0.2.9
data/dslkit.gemspec CHANGED
@@ -2,36 +2,36 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "dslkit"
5
- s.version = "0.2.8"
5
+ s.version = "0.2.9"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Florian Frank"]
9
- s.date = "2011-09-25"
9
+ s.date = "2011-10-07"
10
10
  s.description = "This library contains recurring patterns, that are useful in the creation of internal Domain Specific Languages (DSL) in Ruby."
11
11
  s.email = "flori@ping.de"
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/dslkit/version.rb", "lib/dslkit/rude.rb", "lib/dslkit/polite.rb", "lib/dslkit.rb"]
13
- s.files = [".gitignore", ".travis.yml", "CHANGES", "COPYING", "Gemfile", "README.rdoc", "Rakefile", "TODO", "VERSION", "dslkit.gemspec", "examples/add_one.pdf", "examples/add_one.stm", "examples/bb3.pdf", "examples/bb3.stm", "examples/bb3_19.stm", "examples/concatenate_compare.mtm", "examples/concatenate_compare.pdf", "examples/concatenate_compare_19.mtm", "examples/length_difference.mtm", "examples/length_difference.pdf", "examples/length_difference_19.mtm", "examples/let.rb", "examples/mail.rb", "examples/minsky.rb", "examples/multiply.reg", "examples/null_pattern.rb", "examples/ones_difference-mtm.pdf", "examples/ones_difference-stm.pdf", "examples/ones_difference.mtm", "examples/ones_difference.stm", "examples/ones_difference_19.mtm", "examples/ones_difference_19.stm", "examples/prefix-equals-suffix-reversed-with-infix.pdf", "examples/prefix-equals-suffix-reversed-with-infix.stm", "examples/prefix-equals-suffix-reversed-with-infix_19.stm", "examples/recipe.rb", "examples/recipe2.rb", "examples/recipe_common.rb", "examples/subtract.reg", "examples/turing-graph.rb", "examples/turing.rb", "lib/dslkit.rb", "lib/dslkit/polite.rb", "lib/dslkit/rude.rb", "lib/dslkit/version.rb", "tests/test_polite.rb"]
13
+ s.files = [".gitignore", ".rvmrc", ".travis.yml", "CHANGES", "COPYING", "Gemfile", "README.rdoc", "Rakefile", "TODO", "VERSION", "dslkit.gemspec", "examples/add_one.pdf", "examples/add_one.stm", "examples/bb3.pdf", "examples/bb3.stm", "examples/bb3_19.stm", "examples/concatenate_compare.mtm", "examples/concatenate_compare.pdf", "examples/concatenate_compare_19.mtm", "examples/length_difference.mtm", "examples/length_difference.pdf", "examples/length_difference_19.mtm", "examples/let.rb", "examples/mail.rb", "examples/minsky.rb", "examples/multiply.reg", "examples/null_pattern.rb", "examples/ones_difference-mtm.pdf", "examples/ones_difference-stm.pdf", "examples/ones_difference.mtm", "examples/ones_difference.stm", "examples/ones_difference_19.mtm", "examples/ones_difference_19.stm", "examples/prefix-equals-suffix-reversed-with-infix.pdf", "examples/prefix-equals-suffix-reversed-with-infix.stm", "examples/prefix-equals-suffix-reversed-with-infix_19.stm", "examples/recipe.rb", "examples/recipe2.rb", "examples/recipe_common.rb", "examples/subtract.reg", "examples/turing-graph.rb", "examples/turing.rb", "lib/dslkit.rb", "lib/dslkit/polite.rb", "lib/dslkit/rude.rb", "lib/dslkit/version.rb", "tests/from_module_test.rb", "tests/polite_test.rb"]
14
14
  s.homepage = "http://flori.github.com/dslkit"
15
15
  s.rdoc_options = ["--title", "Dslkit - Kit for building DSLs in Ruby", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubygems_version = "1.8.10"
18
18
  s.summary = "Kit for building DSLs in Ruby"
19
- s.test_files = ["tests/test_polite.rb"]
19
+ s.test_files = ["tests/from_module_test.rb", "tests/polite_test.rb"]
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  s.specification_version = 3
23
23
 
24
24
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- s.add_development_dependency(%q<gem_hadar>, ["~> 0.1.0"])
25
+ s.add_development_dependency(%q<gem_hadar>, ["~> 0.1.1"])
26
26
  s.add_runtime_dependency(%q<term-ansicolor>, ["~> 1.0"])
27
27
  s.add_runtime_dependency(%q<tins>, ["~> 0.3"])
28
28
  else
29
- s.add_dependency(%q<gem_hadar>, ["~> 0.1.0"])
29
+ s.add_dependency(%q<gem_hadar>, ["~> 0.1.1"])
30
30
  s.add_dependency(%q<term-ansicolor>, ["~> 1.0"])
31
31
  s.add_dependency(%q<tins>, ["~> 0.3"])
32
32
  end
33
33
  else
34
- s.add_dependency(%q<gem_hadar>, ["~> 0.1.0"])
34
+ s.add_dependency(%q<gem_hadar>, ["~> 0.1.1"])
35
35
  s.add_dependency(%q<term-ansicolor>, ["~> 1.0"])
36
36
  s.add_dependency(%q<tins>, ["~> 0.3"])
37
37
  end
data/examples/let.rb CHANGED
@@ -50,7 +50,7 @@ if $0 == __FILE__
50
50
  end
51
51
 
52
52
  def test
53
- let :x => 1, :y => twice(1) do
53
+ let :x => 1, :y => twice(1) do
54
54
  let :z => twice(x) do
55
55
  puts "#{x} * #{y} == #{z} # => #{x * y == twice(x)}"
56
56
  end
data/examples/mail.rb CHANGED
@@ -29,7 +29,7 @@ class Mail
29
29
 
30
30
  dsl_accessor :date do Time.now.rfc2822 end
31
31
 
32
- def message_id
32
+ def message_id
33
33
  key = [ ENV['HOSTNAME'] || 'localhost', $$ , Time.now ].join
34
34
  (::Digest::MD5.new << key).to_s
35
35
  end
data/examples/minsky.rb CHANGED
@@ -10,7 +10,7 @@ module Minsky
10
10
  attr_accessor :interpreter
11
11
 
12
12
  attr_accessor :name
13
-
13
+
14
14
  def execute
15
15
  interpreter.display_registers(self)
16
16
  call
@@ -70,7 +70,7 @@ module Minsky
70
70
  end
71
71
 
72
72
  def display_registers(label)
73
- @format ||= "%#{@labels.map { |l| l.name.to_s.size }.max}s"
73
+ @format ||= "%#{@labels.map { |l| l.name.to_s.size }.max}s"
74
74
  STDOUT.puts "#{@format % label.name}: #{@registers}"
75
75
  if @stepping
76
76
  STDOUT.print "? "
@@ -107,7 +107,7 @@ module Minsky
107
107
 
108
108
  def increment(register, label)
109
109
  label = label_fetch label
110
- register_increment(register)
110
+ register_increment(register)
111
111
  label
112
112
  end
113
113
 
@@ -117,8 +117,8 @@ module Minsky
117
117
  else_label = label_fetch else_label
118
118
  if register_value.zero?
119
119
  zero_label
120
- else
121
- register_decrement(register)
120
+ else
121
+ register_decrement(register)
122
122
  else_label
123
123
  end
124
124
  end
data/examples/recipe2.rb CHANGED
@@ -61,7 +61,7 @@ end
61
61
  recipe_source = <<EOT
62
62
  pancakes = recipe do
63
63
  2.cups. flour
64
- 2.5.teaspoons. baking_powder
64
+ 2.5.teaspoons. baking_powder
65
65
  0.5.teaspoon. salt
66
66
  1. egg
67
67
  1.5.cups. milk
@@ -58,7 +58,7 @@ class Unit
58
58
  end
59
59
 
60
60
  attr_reader :n
61
-
61
+
62
62
  def to_s
63
63
  "#@n #{name}#{@n > 1 ? 's' : ''}"
64
64
  end
@@ -90,7 +90,7 @@ class Unit
90
90
  end
91
91
 
92
92
  attr_reader :n
93
-
93
+
94
94
  def to_s
95
95
  "#@n #{name}#{@n > 1 ? 's' : ''}"
96
96
  end
data/examples/turing.rb CHANGED
@@ -56,7 +56,7 @@ module Turing
56
56
  def to_s
57
57
  "#{@left.join}#{Term::ANSIColor.red(@head)}#{@right.join.reverse}"
58
58
  end
59
-
59
+
60
60
  alias inspect to_s
61
61
  end
62
62
 
data/lib/dslkit/polite.rb CHANGED
@@ -63,7 +63,7 @@ module DSLKit
63
63
 
64
64
  # Define a thread local variable named _name_ in this module/class. If the
65
65
  # value _value_ is given, it is used to initialize the variable.
66
- def thread_local(name, value = nil)
66
+ def thread_local(name, default_value = nil)
67
67
  is_a?(Module) or raise TypeError, "receiver has to be a Module"
68
68
 
69
69
  name = name.to_s
@@ -81,9 +81,9 @@ module DSLKit
81
81
  Thread.current[my_id][name] = value
82
82
  end
83
83
 
84
- if value
84
+ if default_value
85
85
  Thread.current[my_id] = {}
86
- Thread.current[my_id][name] = value
86
+ Thread.current[my_id][name] = default_value
87
87
  end
88
88
  self
89
89
  end
@@ -103,9 +103,9 @@ module DSLKit
103
103
  module ThreadGlobal
104
104
  # Define a thread global variable named _name_ in this module/class. If the
105
105
  # value _value_ is given, it is used to initialize the variable.
106
- def thread_global(name, value = nil)
106
+ def thread_global(name, default_value = nil)
107
107
  is_a?(Module) or raise TypeError, "receiver has to be a Module"
108
-
108
+
109
109
  name = name.to_s
110
110
  var_name = "@__#{name}_#{__id__.abs}__"
111
111
 
@@ -120,7 +120,7 @@ module DSLKit
120
120
  lock.synchronize { modul.instance_variable_set var_name, value }
121
121
  end
122
122
 
123
- modul.instance_variable_set var_name, value if value
123
+ modul.instance_variable_set var_name, default_value if default_value
124
124
  self
125
125
  end
126
126
 
@@ -199,7 +199,7 @@ module DSLKit
199
199
  # 3
200
200
  # end
201
201
  # end
202
- #
202
+ #
203
203
  # A.new.interpret('|a,b| a + b + c', 1, 2) # => 6
204
204
  #
205
205
  # To use a specified binding see #interpret_with_binding.
@@ -218,7 +218,7 @@ module DSLKit
218
218
  # 3
219
219
  # end
220
220
  # def foo
221
- # b = 2
221
+ # b = 2
222
222
  # interpret_with_binding('|a| a + b + c', binding, 1) # => 6
223
223
  # end
224
224
  # end
@@ -243,7 +243,7 @@ module DSLKit
243
243
  # frozen, if this is possible. If you want to modify/exchange a value use
244
244
  # DSLAccessor#dsl_reader/DSLAccessor#dsl_accessor instead.
245
245
  def constant(name, value = name)
246
- value = value.freeze rescue value
246
+ value = value.freeze rescue value
247
247
  define_method(name) { value }
248
248
  end
249
249
  end
@@ -251,7 +251,7 @@ module DSLKit
251
251
  # The DSLAccessor module contains some methods, that can be used to make
252
252
  # simple accessors for a DSL.
253
253
  #
254
- #
254
+ #
255
255
  # class CoffeeMaker
256
256
  # extend DSLKit::Constant
257
257
  #
@@ -598,4 +598,33 @@ module DSLKit
598
598
  end
599
599
  end
600
600
  end
601
+
602
+ module ParameterizedModule
603
+ # Pass _args_ and _block_ to configure the module and then return it after
604
+ # calling the parameterize method has been called with these arguments. The
605
+ # _parameterize_ method should return a configured module.
606
+ def parameterize_for(*args, &block)
607
+ respond_to?(:parameterize) ? parameterize(*args, &block) : self
608
+ end
609
+ end
610
+
611
+ module FromModule
612
+ include ParameterizedModule
613
+
614
+ alias from parameterize_for
615
+
616
+ def parameterize(opts = {})
617
+ modul = opts[:module] or raise ArgumentError, 'option :module is required'
618
+ import_methods = Array(opts[:methods])
619
+ result = modul.dup
620
+ remove_methods = modul.instance_methods.map(&:to_sym) - import_methods.map(&:to_sym)
621
+ remove_methods.each do |m|
622
+ begin
623
+ result.__send__ :remove_method, m
624
+ rescue NameError
625
+ end
626
+ end
627
+ result
628
+ end
629
+ end
601
630
  end
data/lib/dslkit/rude.rb CHANGED
@@ -10,6 +10,8 @@ module DSLKit
10
10
  include DSLKit::DSLAccessor
11
11
  include DSLKit::ClassMethod
12
12
  include DSLKit::Delegate
13
+ include DSLKit::ParameterizedModule
14
+ include DSLKit::FromModule
13
15
  end
14
16
 
15
17
  class ::Object
@@ -1,6 +1,6 @@
1
1
  module DSLKit
2
2
  # DSLKit version
3
- VERSION = '0.2.8'
3
+ VERSION = '0.2.9'
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:
@@ -0,0 +1,61 @@
1
+ require 'dslkit/polite'
2
+ require 'test/unit'
3
+
4
+ class FromModuleTest < Test::Unit::TestCase
5
+ module MyIncludedModule
6
+ def foo
7
+ :foo
8
+ end
9
+
10
+ def bar
11
+ :bar
12
+ end
13
+ end
14
+
15
+ class MyKlass
16
+ def foo
17
+ :original_foo
18
+ end
19
+
20
+ def bar
21
+ :original_bar
22
+ end
23
+ end
24
+
25
+ class DerivedKlass < MyKlass
26
+ extend DSLKit::FromModule
27
+
28
+ include from :module => MyIncludedModule, :methods => [ :foo ]
29
+ end
30
+
31
+ module MyModule
32
+ def foo
33
+ :original_foo
34
+ end
35
+
36
+ def bar
37
+ :original_bar
38
+ end
39
+ include MyIncludedModule
40
+ end
41
+
42
+ class AnotherDerivedKlass
43
+ include MyModule
44
+
45
+ extend DSLKit::FromModule
46
+
47
+ include from :module => MyIncludedModule, :methods => :foo
48
+ end
49
+
50
+ def test_derived_klass
51
+ c = DerivedKlass.new
52
+ assert_equal :foo, c.foo
53
+ assert_equal :original_bar, c.bar
54
+ end
55
+
56
+ def test_another_derived_klass
57
+ c = AnotherDerivedKlass.new
58
+ assert_equal :foo, c.foo
59
+ assert_equal :original_bar, c.bar
60
+ end
61
+ end
@@ -52,7 +52,7 @@ class DA
52
52
 
53
53
  dsl_accessor :quux, :qu, :ux
54
54
 
55
- dsl_reader :on, true
55
+ dsl_reader :on, true
56
56
 
57
57
  dsl_reader :off, false
58
58
 
@@ -69,7 +69,7 @@ class I
69
69
  def foo
70
70
  :foo
71
71
  end
72
-
72
+
73
73
  def y
74
74
  2
75
75
  end
@@ -108,7 +108,7 @@ end
108
108
  require 'test/unit'
109
109
  require 'tempfile'
110
110
 
111
- class TC_DSLKit < Test::Unit::TestCase
111
+ class PoliteTest < Test::Unit::TestCase
112
112
  def setup
113
113
  @tl = TL.new
114
114
  @tl2 = TL.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dslkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-25 00:00:00.000000000Z
12
+ date: 2011-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gem_hadar
16
- requirement: &2156319460 !ruby/object:Gem::Requirement
16
+ requirement: &70131132995660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.1.0
21
+ version: 0.1.1
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2156319460
24
+ version_requirements: *70131132995660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: term-ansicolor
27
- requirement: &2156318700 !ruby/object:Gem::Requirement
27
+ requirement: &70131132994340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2156318700
35
+ version_requirements: *70131132994340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: tins
38
- requirement: &2156317900 !ruby/object:Gem::Requirement
38
+ requirement: &70131132993400 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0.3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2156317900
46
+ version_requirements: *70131132993400
47
47
  description: This library contains recurring patterns, that are useful in the creation
48
48
  of internal Domain Specific Languages (DSL) in Ruby.
49
49
  email: flori@ping.de
@@ -57,6 +57,7 @@ extra_rdoc_files:
57
57
  - lib/dslkit.rb
58
58
  files:
59
59
  - .gitignore
60
+ - .rvmrc
60
61
  - .travis.yml
61
62
  - CHANGES
62
63
  - COPYING
@@ -101,7 +102,8 @@ files:
101
102
  - lib/dslkit/polite.rb
102
103
  - lib/dslkit/rude.rb
103
104
  - lib/dslkit/version.rb
104
- - tests/test_polite.rb
105
+ - tests/from_module_test.rb
106
+ - tests/polite_test.rb
105
107
  homepage: http://flori.github.com/dslkit
106
108
  licenses: []
107
109
  post_install_message:
@@ -131,4 +133,5 @@ signing_key:
131
133
  specification_version: 3
132
134
  summary: Kit for building DSLs in Ruby
133
135
  test_files:
134
- - tests/test_polite.rb
136
+ - tests/from_module_test.rb
137
+ - tests/polite_test.rb