dslkit 0.2.9 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .*.sw[pon]
2
2
  .rvmrc
3
3
  Gemfile.lock
4
+ coverage
4
5
  pkg
@@ -1,6 +1,7 @@
1
1
  rvm:
2
2
  - 1.8.7
3
3
  - 1.9.2
4
+ - 1.9.3
4
5
  - ruby-head
5
6
  - ree
6
7
  - rbx
data/CHANGES CHANGED
@@ -1,3 +1,5 @@
1
+ 2012-01-12 (0.2.10)
2
+ * Add module for rails-style concerns.
1
3
  2011-10-07 (0.2.9)
2
4
  * Add ParameterizedModule and FromModule.
3
5
  2011-09-26 (0.2.8)
data/Gemfile CHANGED
@@ -3,3 +3,7 @@
3
3
  source :rubygems
4
4
 
5
5
  gemspec
6
+
7
+ group :test do
8
+ gem 'simplecov', :platform => :mri_19
9
+ end
@@ -9,7 +9,7 @@ internal Domain Specific Languages (DSL) in Ruby.
9
9
 
10
10
  Florian Frank <mailto:flori@ping.de>
11
11
 
12
- == License
12
+ == License
13
13
 
14
14
  This is free software; you can redistribute it and/or modify it under the
15
15
  terms of the GNU General Public License Version 2 as published by the Free
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', '.rvmrc'
14
+ ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', 'coverage'
15
15
  readme 'README.rdoc'
16
16
 
17
17
  dependency 'term-ansicolor', '~>1.0'
@@ -19,7 +19,7 @@ GemHadar do
19
19
 
20
20
  install_library do
21
21
  libdir = CONFIG["sitelibdir"]
22
- cd 'lib' do
22
+ cd 'lib' do
23
23
  dst = File.join(libdir, 'dslkit')
24
24
  mkdir_p dst
25
25
  cd 'dslkit' do
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.9
1
+ 0.2.10
@@ -2,36 +2,36 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "dslkit"
5
- s.version = "0.2.9"
5
+ s.version = "0.2.10"
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-10-07"
9
+ s.date = "2012-01-12"
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", ".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"]
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/concern_test.rb", "tests/dynamic_scope_test.rb", "tests/from_module_test.rb", "tests/polite_test.rb", "tests/scope_test.rb", "tests/test_helper.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
- s.rubygems_version = "1.8.10"
17
+ s.rubygems_version = "1.8.13"
18
18
  s.summary = "Kit for building DSLs in Ruby"
19
- s.test_files = ["tests/from_module_test.rb", "tests/polite_test.rb"]
19
+ s.test_files = ["tests/concern_test.rb", "tests/from_module_test.rb", "tests/dynamic_scope_test.rb", "tests/scope_test.rb", "tests/test_helper.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.1"])
25
+ s.add_development_dependency(%q<gem_hadar>, ["~> 0.1.4"])
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.1"])
29
+ s.add_dependency(%q<gem_hadar>, ["~> 0.1.4"])
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.1"])
34
+ s.add_dependency(%q<gem_hadar>, ["~> 0.1.4"])
35
35
  s.add_dependency(%q<term-ansicolor>, ["~> 1.0"])
36
36
  s.add_dependency(%q<tins>, ["~> 0.3"])
37
37
  end
@@ -6,7 +6,7 @@
6
6
 
7
7
  # state A
8
8
  1. cond :if => 'B', :then => 2, :else => 4
9
- 2. write :symbol => '1', :goto => 3
9
+ 2. write :symbol => '1', :goto => 3
10
10
  3. right :goto => 6
11
11
  4. write :symbol => '1', :goto => 5
12
12
  5. right :goto => 16
@@ -6,7 +6,7 @@
6
6
 
7
7
  # state A
8
8
  1. cond if: 'B', then: 2, else: 4
9
- 2. write symbol: '1', goto: 3
9
+ 2. write symbol: '1', goto: 3
10
10
  3. right goto: 6
11
11
  4. write symbol: '1', goto: 5
12
12
  5. right goto: 16
@@ -1,6 +1,6 @@
1
1
  # vim: ts=2 sw=2 et ft=ruby
2
2
  # Call with two [ab]* strings on the tapes:
3
- # $ turing.rb prefix-equals-suffix-reversed-with-infix.stm ab ababab
3
+ # $ turing.rb prefix-equals-suffix-reversed-with-infix.stm ab ababab
4
4
 
5
5
  1. right 1, :goto => 2
6
6
  2. right 2, :goto => 3
@@ -1,6 +1,6 @@
1
1
  # vim: ts=2 sw=2 et ft=ruby
2
2
  # Call with two [ab]* strings on the tapes:
3
- # $ turing.rb prefix-equals-suffix-reversed-with-infix.stm ab ababab
3
+ # $ turing.rb prefix-equals-suffix-reversed-with-infix.stm ab ababab
4
4
 
5
5
  1. right 1, goto: 2
6
6
  2. right 2, goto: 3
@@ -627,4 +627,101 @@ module DSLKit
627
627
  result
628
628
  end
629
629
  end
630
+
631
+ module Scope
632
+ def scope_push(scope_frame, name = :default)
633
+ scope_get(name).push scope_frame
634
+ self
635
+ end
636
+
637
+ def scope_pop(name = :default)
638
+ scope_get(name).pop
639
+ scope_get(name).empty? and Thread.current[name] = nil
640
+ self
641
+ end
642
+
643
+ def scope_top(name = :default)
644
+ scope_get(name).last
645
+ end
646
+
647
+ def scope_reverse(name = :default, &block)
648
+ scope_get(name).reverse_each(&block)
649
+ end
650
+
651
+ def scope_block(scope_frame, name = :default)
652
+ scope_push(scope_frame, name)
653
+ yield
654
+ self
655
+ ensure
656
+ scope_pop(name)
657
+ end
658
+
659
+ def scope_get(name = :default)
660
+ Thread.current[name] ||= []
661
+ end
662
+
663
+ def scope(name = :default)
664
+ scope_get(name).dup
665
+ end
666
+ end
667
+
668
+ module DynamicScope
669
+ class Context < Hash
670
+ def [](name)
671
+ super name.to_sym
672
+ end
673
+
674
+ def []=(name, value)
675
+ super name.to_sym, value
676
+ end
677
+ end
678
+
679
+ include Scope
680
+
681
+ attr_accessor :dynamic_scope_name
682
+
683
+ def dynamic_scope(&block)
684
+ self.dynamic_scope_name ||= :variables
685
+ scope_block(Context.new, dynamic_scope_name, &block)
686
+ end
687
+
688
+ def method_missing(id, *args)
689
+ self.dynamic_scope_name ||= :variables
690
+ if args.empty? and scope_reverse(dynamic_scope_name) { |c| c.key?(id) and return c[id] }
691
+ super
692
+ elsif args.size == 1 and id.to_s =~ /(.*?)=\Z/
693
+ c = scope_top(dynamic_scope_name) or super
694
+ c[$1.to_sym] = args.first
695
+ else
696
+ super
697
+ end
698
+ end
699
+ end
700
+
701
+ module Concern
702
+ def self.extended(base)
703
+ base.instance_variable_set("@_dependencies", [])
704
+ end
705
+
706
+ def append_features(base)
707
+ if base.instance_variable_defined?("@_dependencies")
708
+ base.instance_variable_get("@_dependencies") << self
709
+ return false
710
+ else
711
+ return false if base < self
712
+ @_dependencies.each { |dep| base.send(:include, dep) }
713
+ super
714
+ base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
715
+ base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
716
+ end
717
+ end
718
+
719
+ def included(base = nil, &block)
720
+ if base.nil?
721
+ @_included_block = block
722
+ else
723
+ super
724
+ end
725
+ end
726
+ end
630
727
  end
@@ -1,6 +1,6 @@
1
1
  module DSLKit
2
2
  # DSLKit version
3
- VERSION = '0.2.9'
3
+ VERSION = '0.2.10'
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,35 @@
1
+ require 'test_helper'
2
+ require 'dslkit/polite'
3
+
4
+ class ConcernTest < Test::Unit::TestCase
5
+ module AC
6
+ extend DSLKit::Concern
7
+
8
+ included do
9
+ $included = self
10
+ end
11
+
12
+ def foo
13
+ :foo
14
+ end
15
+
16
+ module ClassMethods
17
+ def bar
18
+ :bar
19
+ end
20
+ end
21
+ end
22
+
23
+ $included = nil
24
+
25
+ class A
26
+ include AC
27
+ end
28
+
29
+ def test_concern
30
+ a = A.new
31
+ assert_equal A, $included
32
+ assert_equal :foo, a.foo
33
+ assert_equal :bar, A.bar
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+ require 'dslkit/polite'
3
+
4
+ class DynamicScopeTest < Test::Unit::TestCase
5
+ include DSLKit::DynamicScope
6
+
7
+ def test_dynamic_scoping
8
+ assert_raise(NameError) { foo }
9
+ dynamic_scope do
10
+ assert_raise(NameError) { foo }
11
+ self.foo = 1
12
+ assert_equal 1, foo
13
+ dynamic_scope do
14
+ assert_equal 1, foo
15
+ self.foo = 2
16
+ assert_equal 2, foo
17
+ dynamic_scope do
18
+ assert_equal 2, foo
19
+ end
20
+ assert_equal 2, foo
21
+ end
22
+ assert_equal 1, foo
23
+ end
24
+ assert_raise(NameError) { foo }
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
+ require 'test_helper'
1
2
  require 'dslkit/polite'
2
- require 'test/unit'
3
3
 
4
4
  class FromModuleTest < Test::Unit::TestCase
5
5
  module MyIncludedModule
@@ -1,3 +1,4 @@
1
+ require 'test_helper'
1
2
  require 'dslkit/polite'
2
3
 
3
4
  class TL
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+ require 'dslkit/polite'
3
+
4
+ class ScopeTest < Test::Unit::TestCase
5
+ include DSLKit::Scope
6
+
7
+ def test_default_scope
8
+ scope_block(:foo) do
9
+ assert_equal [ :foo ], scope
10
+ scope_block(:bar) do
11
+ assert_equal [ :foo, :bar ], scope
12
+ scope.push :baz
13
+ assert_equal [ :foo, :bar ], scope
14
+ end
15
+ end
16
+ end
17
+
18
+ def test_two_scopes
19
+ scope_block(:foo, :my_scope) do
20
+ assert_equal [ :foo ], scope(:my_scope)
21
+ scope_block(:baz) do
22
+ scope_block(:bar, :my_scope) do
23
+ assert_equal [ :foo, :bar ], scope(:my_scope)
24
+ scope.push(:baz, :my_scope)
25
+ assert_equal [ :foo, :bar ], scope(:my_scope)
26
+ assert_equal [ :baz ], scope
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ if ENV['START_SIMPLECOV'].to_i == 1
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter "#{File.basename(File.dirname(__FILE__))}/"
5
+ end
6
+ end
7
+ require 'test/unit'
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.9
4
+ version: 0.2.10
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-10-07 00:00:00.000000000 Z
12
+ date: 2012-01-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gem_hadar
16
- requirement: &70131132995660 !ruby/object:Gem::Requirement
16
+ requirement: &73147590 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.1.1
21
+ version: 0.1.4
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70131132995660
24
+ version_requirements: *73147590
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: term-ansicolor
27
- requirement: &70131132994340 !ruby/object:Gem::Requirement
27
+ requirement: &73162410 !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: *70131132994340
35
+ version_requirements: *73162410
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: tins
38
- requirement: &70131132993400 !ruby/object:Gem::Requirement
38
+ requirement: &73161340 !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: *70131132993400
46
+ version_requirements: *73161340
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
@@ -102,8 +102,12 @@ files:
102
102
  - lib/dslkit/polite.rb
103
103
  - lib/dslkit/rude.rb
104
104
  - lib/dslkit/version.rb
105
+ - tests/concern_test.rb
106
+ - tests/dynamic_scope_test.rb
105
107
  - tests/from_module_test.rb
106
108
  - tests/polite_test.rb
109
+ - tests/scope_test.rb
110
+ - tests/test_helper.rb
107
111
  homepage: http://flori.github.com/dslkit
108
112
  licenses: []
109
113
  post_install_message:
@@ -128,10 +132,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
132
  version: '0'
129
133
  requirements: []
130
134
  rubyforge_project:
131
- rubygems_version: 1.8.10
135
+ rubygems_version: 1.8.13
132
136
  signing_key:
133
137
  specification_version: 3
134
138
  summary: Kit for building DSLs in Ruby
135
139
  test_files:
140
+ - tests/concern_test.rb
136
141
  - tests/from_module_test.rb
142
+ - tests/dynamic_scope_test.rb
143
+ - tests/scope_test.rb
144
+ - tests/test_helper.rb
137
145
  - tests/polite_test.rb