block-chainable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/Manifest.txt +7 -0
- data/README.txt +141 -0
- data/Rakefile +25 -0
- data/lib/block_chainable.rb +40 -0
- data/spec/block_chainable_spec.rb +38 -0
- data/test/test_block_chainable.rb +40 -0
- metadata +88 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
= BlockChainable
|
2
|
+
|
3
|
+
* http://block-chainable.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
BlockChainable is a module to aid in the creation of Domain Specific Languages using block structure.
|
8
|
+
By including BlockChainable into your classes, you will be able to instantiate that class using the
|
9
|
+
class name itself, followed by any parameters to be passed to initialize, followed by a block to be
|
10
|
+
executed within the instantiated class.
|
11
|
+
|
12
|
+
BlockChainable also allows methods to search up the chain of classes, meaning that although a block
|
13
|
+
is executed in the scope of the instantiated class, any methods not found in the class but found in a
|
14
|
+
class "up-scope" will be called successfully on the up-scope class. This chaining of method calls allows
|
15
|
+
you to assert values within the blocks as well as calling any other methods from "up-scope" classes.
|
16
|
+
|
17
|
+
== FEATURES/PROBLEMS:
|
18
|
+
|
19
|
+
* 2 major features
|
20
|
+
* Block instantiation of classes
|
21
|
+
* Method calls will look "up-scope" for receives when called inside BlockChainable blocks
|
22
|
+
* 1 minor problem
|
23
|
+
* Stack trace should be simplified on errors within BlockChainable blocks
|
24
|
+
|
25
|
+
== SYNOPSIS:
|
26
|
+
|
27
|
+
# a simple dsl for creating classroom rosters and printing them out. code can be found in the
|
28
|
+
# example directory.
|
29
|
+
|
30
|
+
# first, define our roster
|
31
|
+
class Roster
|
32
|
+
include BlockChainable
|
33
|
+
|
34
|
+
def initialize(subject)
|
35
|
+
@subject = subject
|
36
|
+
@students = []
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_student_to_roster student
|
40
|
+
@students << student
|
41
|
+
end
|
42
|
+
|
43
|
+
def print_roster
|
44
|
+
puts "Roster for #{@subject}:"
|
45
|
+
@students.each{|s| puts " #{s}"}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# next, define our student. the only tricky part here is add_to_roster, which calls
|
50
|
+
# the method add_student_to_class, which is actually defined on the Classroom class.
|
51
|
+
# BlockChainable will automatically send this method to the Classroom class with the
|
52
|
+
# Student instance as a parameter.
|
53
|
+
class Student
|
54
|
+
include BlockChainable
|
55
|
+
|
56
|
+
def add_to_roster
|
57
|
+
add_student_to_class self
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_s
|
61
|
+
"#{@last_name}, #{@first_name} - age #{@age}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def first_name name
|
65
|
+
@first_name = name
|
66
|
+
end
|
67
|
+
|
68
|
+
def last_name name
|
69
|
+
@last_name = name
|
70
|
+
end
|
71
|
+
|
72
|
+
def age years
|
73
|
+
@age = years
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# we now have all the pieces we need for creating a simple dsl for building
|
78
|
+
# and printing a class roster
|
79
|
+
require 'block_chainable'
|
80
|
+
|
81
|
+
Roster :Math do
|
82
|
+
Student do
|
83
|
+
first_name "Drew"
|
84
|
+
last_name "Olson"
|
85
|
+
age 25
|
86
|
+
|
87
|
+
add_to_roster
|
88
|
+
end
|
89
|
+
|
90
|
+
Student do
|
91
|
+
first_name "John"
|
92
|
+
last_name "Doe"
|
93
|
+
age 17
|
94
|
+
|
95
|
+
add_to_roster
|
96
|
+
end
|
97
|
+
|
98
|
+
Student do
|
99
|
+
first_name "Jane"
|
100
|
+
last_name "Doe"
|
101
|
+
age 19
|
102
|
+
|
103
|
+
add_to_roster
|
104
|
+
end
|
105
|
+
|
106
|
+
print_roster
|
107
|
+
end
|
108
|
+
|
109
|
+
== REQUIREMENTS:
|
110
|
+
|
111
|
+
* need
|
112
|
+
* rspec
|
113
|
+
|
114
|
+
== INSTALL:
|
115
|
+
|
116
|
+
* sudo gem install block-chainable
|
117
|
+
|
118
|
+
== LICENSE:
|
119
|
+
|
120
|
+
(The MIT License)
|
121
|
+
|
122
|
+
Copyright (c) 2008 Drew Olson
|
123
|
+
|
124
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
125
|
+
a copy of this software and associated documentation files (the
|
126
|
+
'Software'), to deal in the Software without restriction, including
|
127
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
128
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
129
|
+
permit persons to whom the Software is furnished to do so, subject to
|
130
|
+
the following conditions:
|
131
|
+
|
132
|
+
The above copyright notice and this permission notice shall be
|
133
|
+
included in all copies or substantial portions of the Software.
|
134
|
+
|
135
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
136
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
137
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
138
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
139
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
140
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
141
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require 'need'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
need{'./lib/block_chainable.rb'}
|
8
|
+
|
9
|
+
Hoe.new('block-chainable', '0.0.1') do |p|
|
10
|
+
p.rubyforge_name = 'block-chainable'
|
11
|
+
p.author = 'Drew Olson'
|
12
|
+
p.email = 'olsonas@gmail.com'
|
13
|
+
# p.summary = 'FIX'
|
14
|
+
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
15
|
+
p.url = "http://block-chainable.rubyforge.org"
|
16
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
17
|
+
p.extra_deps << ['need', '>= 1.0.1']
|
18
|
+
p.extra_deps << ['rspec', '>= 1.1.3']
|
19
|
+
end
|
20
|
+
|
21
|
+
Spec::Rake::SpecTask.new do |t|
|
22
|
+
t.warning = true
|
23
|
+
end
|
24
|
+
|
25
|
+
# vim: syntax=Ruby
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module BlockChainable
|
2
|
+
module ClassMethods
|
3
|
+
def start(*args,&block)
|
4
|
+
klass_instance = self.new(*args)
|
5
|
+
|
6
|
+
klass_instance.instance_eval do
|
7
|
+
@__parent = eval("self",block.binding)
|
8
|
+
end
|
9
|
+
|
10
|
+
class << klass_instance
|
11
|
+
def method_missing(name,*args)
|
12
|
+
if chain_respond_to?(name)
|
13
|
+
@__parent.send(name,*args)
|
14
|
+
else
|
15
|
+
super(name,*args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
klass_instance.instance_eval(&block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included(klass)
|
25
|
+
klass.extend(ClassMethods)
|
26
|
+
|
27
|
+
Object.class_eval <<-EOS
|
28
|
+
def #{klass}(*args,&block)
|
29
|
+
#{klass}.start(*args,&block)
|
30
|
+
end
|
31
|
+
EOS
|
32
|
+
end
|
33
|
+
|
34
|
+
def chain_respond_to?(name)
|
35
|
+
self.respond_to?(name) ||
|
36
|
+
(@__parent &&
|
37
|
+
(@__parent.respond_to?(name) ||
|
38
|
+
(@__parent.respond_to?(:chain_respond_to?) && @__parent.chain_respond_to?(name))))
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'need'
|
3
|
+
need{"foo"}
|
4
|
+
need{"bar"}
|
5
|
+
|
6
|
+
describe "test block chainable" do
|
7
|
+
it "should work on standard method calls" do
|
8
|
+
Foo do
|
9
|
+
is_foo?.should == true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should work for standard method calls on nested blocks" do
|
14
|
+
Foo do
|
15
|
+
Bar do
|
16
|
+
is_bar?.should == true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "it should allow calls to outer block methods from inner blocks" do
|
22
|
+
Foo do
|
23
|
+
Bar do
|
24
|
+
is_foo?.should == true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not allow method calls to inner blocks from outer blocks" do
|
30
|
+
lambda do
|
31
|
+
Foo do
|
32
|
+
is_bar?
|
33
|
+
Bar do
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end.should raise_error(NoMethodError)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'need'
|
3
|
+
need{"foo"}
|
4
|
+
need{"bar"}
|
5
|
+
|
6
|
+
require "test/unit"
|
7
|
+
|
8
|
+
class TestBlockChainable < Test::Unit::TestCase
|
9
|
+
def test_standard_method_call
|
10
|
+
Foo do
|
11
|
+
assert is_foo?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_standard_method_call_inner_block
|
16
|
+
Foo do
|
17
|
+
Bar do
|
18
|
+
assert is_bar?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_outer_block_method_call_from_inner_block
|
24
|
+
Foo do
|
25
|
+
Bar do
|
26
|
+
assert is_foo?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_inner_block_method_call_from_outer_block_fails
|
32
|
+
assert_raises NoMethodError do
|
33
|
+
Foo do
|
34
|
+
is_bar?
|
35
|
+
Bar do
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: block-chainable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Drew Olson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-02-29 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: need
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.1
|
23
|
+
version:
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: rspec
|
26
|
+
version_requirement:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 1.1.3
|
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.5.0
|
41
|
+
version:
|
42
|
+
description: "== DESCRIPTION: BlockChainable is a module to aid in the creation of Domain Specific Languages using block structure. By including BlockChainable into your classes, you will be able to instantiate that class using the class name itself, followed by any parameters to be passed to initialize, followed by a block to be executed within the instantiated class. BlockChainable also allows methods to search up the chain of classes, meaning that although a block is executed in the scope of the instantiated class, any methods not found in the class but found in a class \"up-scope\" will be called successfully on the up-scope class. This chaining of method calls allows you to assert values within the blocks as well as calling any other methods from \"up-scope\" classes. == FEATURES/PROBLEMS:"
|
43
|
+
email: olsonas@gmail.com
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
extra_rdoc_files:
|
49
|
+
- History.txt
|
50
|
+
- Manifest.txt
|
51
|
+
- README.txt
|
52
|
+
files:
|
53
|
+
- History.txt
|
54
|
+
- Manifest.txt
|
55
|
+
- README.txt
|
56
|
+
- Rakefile
|
57
|
+
- lib/block_chainable.rb
|
58
|
+
- test/test_block_chainable.rb
|
59
|
+
- spec/block_chainable_spec.rb
|
60
|
+
has_rdoc: true
|
61
|
+
homepage: http://block-chainable.rubyforge.org
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --main
|
65
|
+
- README.txt
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: block-chainable
|
83
|
+
rubygems_version: 1.0.1
|
84
|
+
signing_key:
|
85
|
+
specification_version: 2
|
86
|
+
summary: BlockChainable is a module to aid in the creation of Domain Specific Languages using block structure
|
87
|
+
test_files:
|
88
|
+
- test/test_block_chainable.rb
|