drewolson-defmacro 0.0.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/History.txt ADDED
@@ -0,0 +1,12 @@
1
+ === 0.0.2 / 2008-06-11
2
+
3
+ * 1 minor enhancement
4
+
5
+ * macros can be defined on objects or on main
6
+
7
+ === 0.0.1 / 2008-06-09
8
+
9
+ * 1 major enhancement
10
+
11
+ * macros!
12
+
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ README.txt
5
+ Rakefile
6
+ lib/def_macro.rb
7
+ lib/defmacro.rb
8
+ test/test_def_macro.rb
data/README.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = def_macro
2
+
3
+ * http://github.com/drewolson/def_macro/tree/master
4
+
5
+ == DESCRIPTION:
6
+
7
+ Macros for ruby
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * A few features, probably lots of problems
12
+
13
+ == SYNOPSIS:
14
+
15
+ The classic with macro:
16
+
17
+ def_macro :with do |args,body|
18
+ eval args.first
19
+ eval body
20
+ end
21
+
22
+ with proc { a = 1; b = 2 } do
23
+ puts a + b
24
+ end # => 3
25
+
26
+ The classic loop macro:
27
+
28
+ def_macro :loop do |args,body|
29
+ (eval args.first).times do
30
+ eval body
31
+ end
32
+ end
33
+
34
+ loop 2 do
35
+ puts "hi"
36
+ end # => "hi" "hi"
37
+
38
+ == REQUIREMENTS:
39
+
40
+ * ruby2ruby 1.1.9 or later
41
+
42
+ == INSTALL:
43
+
44
+ * sudo gem install drewolson-defmacro --source=http://gems.github.com
45
+ * sudo gem install defmacro
46
+
47
+ == LICENSE:
48
+
49
+ (The MIT License)
50
+
51
+ Copyright (c) 2008 Drew Olson
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining
54
+ a copy of this software and associated documentation files (the
55
+ 'Software'), to deal in the Software without restriction, including
56
+ without limitation the rights to use, copy, modify, merge, publish,
57
+ distribute, sublicense, and/or sell copies of the Software, and to
58
+ permit persons to whom the Software is furnished to do so, subject to
59
+ the following conditions:
60
+
61
+ The above copyright notice and this permission notice shall be
62
+ included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
65
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
66
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
67
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
68
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
69
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
70
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/def_macro.rb'
6
+
7
+ Hoe.new('defmacro', DefMacro::VERSION) do |p|
8
+ p.rubyforge_name = 'defmacro'
9
+ p.developer('Drew Olson', 'drew@drewolson.org')
10
+ p.extra_deps << ['ruby2ruby', '>= 1.1.9']
11
+ p.extra_deps << ['need', '>= 1.1.0']
12
+ p.remote_rdoc_dir = ''
13
+ end
14
+
15
+ # vim: syntax=Ruby
data/lib/def_macro.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'ruby2ruby'
3
+
4
+ module DefMacro
5
+ VERSION = "0.0.2"
6
+
7
+ def self.cleanse_body(body)
8
+ body = body.split("\n")
9
+ if body.size > 1
10
+ body[1...-1].join("\n")
11
+ else
12
+ cleanse_arg(body.first)
13
+ end
14
+ end
15
+
16
+ def self.cleanse_arg(arg)
17
+ arg[/\{([^}]+)\}/,1]
18
+ end
19
+ end
20
+
21
+ class Object
22
+ def def_macro(name,&body)
23
+ context = (self.kind_of?(Class) ? self : self.class)
24
+ context.class_eval <<-EOS
25
+ def #{name}(*args,&block)
26
+ mac_body = DefMacro.cleanse_body(block.to_ruby)
27
+ args.map! do |arg|
28
+ if arg.kind_of? Proc
29
+ DefMacro.cleanse_arg(arg.to_ruby)
30
+ else
31
+ arg.to_s
32
+ end
33
+ end
34
+ (eval "#{body.to_ruby}").call(args,mac_body)
35
+ end
36
+ EOS
37
+ end
38
+ end
data/lib/defmacro.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'need'
3
+ need { "def_macro" }
@@ -0,0 +1,36 @@
1
+ require "test/unit"
2
+ require 'rubygems'
3
+ require 'need'
4
+ need { "../lib/def_macro" }
5
+
6
+ class Foo
7
+ def_macro :with do |args,body|
8
+ eval args.first
9
+ eval body
10
+ end
11
+ end
12
+
13
+ def_macro :with do |args,body|
14
+ eval args.first
15
+ eval body
16
+ end
17
+
18
+ class TestDefMacro < Test::Unit::TestCase
19
+ def test_with_macro_in_class_def
20
+ foo = Foo.new
21
+
22
+ result = foo.with proc { a = 1; b = 3 } do
23
+ a + b
24
+ end
25
+
26
+ assert_equal(4, result)
27
+ end
28
+
29
+ def test_with_macro_in_main
30
+ result = with proc { a = 1; b = 3 } do
31
+ a + b
32
+ end
33
+
34
+ assert_equal(4, result)
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drewolson-defmacro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Drew Olson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-02 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby2ruby
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.9
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: need
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.1.0
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: hoe
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.2
41
+ version:
42
+ description: Macros for ruby
43
+ email:
44
+ - drew@drewolson.org
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ extra_rdoc_files:
50
+ - History.txt
51
+ - Manifest.txt
52
+ - README.txt
53
+ files:
54
+ - History.txt
55
+ - Manifest.txt
56
+ - README.rdoc
57
+ - README.txt
58
+ - Rakefile
59
+ - lib/def_macro.rb
60
+ - lib/defmacro.rb
61
+ - test/test_def_macro.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/dfg59/def_macro/tree/master
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README.txt
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: defmacro
85
+ rubygems_version: 1.2.0
86
+ signing_key:
87
+ specification_version: 2
88
+ summary: Macros for ruby
89
+ test_files:
90
+ - test/test_def_macro.rb