maroon 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Examples/Dijkstra/calculate_shortest_path.rb +5 -1
- data/Examples/greeter.rb +1 -1
- data/Test/Greeter_test.rb +108 -0
- data/lib/maroon.rb +6 -4
- data/lib/maroon/version.rb +1 -1
- data/maroon.gemspec +1 -1
- metadata +3 -2
@@ -1,4 +1,6 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
require 'live_ast'
|
3
|
+
require 'live_ast/to_ruby'
|
2
4
|
#
|
3
5
|
# Consider street corners on a Manhattan grid. We want to find the
|
4
6
|
# minimal path from the most northeast city to the most
|
@@ -39,7 +41,7 @@
|
|
39
41
|
#
|
40
42
|
# Map is a DCI role. The role in this example is played by an
|
41
43
|
# object representing a particular Manhattan geometry
|
42
|
-
Context::define :CalculateShortestPath do
|
44
|
+
ctx,source = Context::define :CalculateShortestPath do
|
43
45
|
role :distance_labeled_graph_node do
|
44
46
|
# Access to roles and other Context data
|
45
47
|
tentative_distance_values do
|
@@ -350,3 +352,5 @@ class CalculateShortestPath
|
|
350
352
|
end
|
351
353
|
end
|
352
354
|
|
355
|
+
File.open('CalculateShortestPath_generated.rb', 'w') {|f| f.write(source) }
|
356
|
+
|
data/Examples/greeter.rb
CHANGED
@@ -0,0 +1,108 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require '../lib/maroon.rb'
|
3
|
+
require '../lib/maroon/kernel.rb'
|
4
|
+
|
5
|
+
##
|
6
|
+
# General comment
|
7
|
+
# it's hopeless to test for the actual source. It should be the syntax tree rather than the source
|
8
|
+
# it's the semantics that are important not the formatted source!!
|
9
|
+
##
|
10
|
+
|
11
|
+
context :Greet_Someone, :greet do
|
12
|
+
role :greeter do
|
13
|
+
welcome do
|
14
|
+
self.greeting
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
role :greeted do
|
19
|
+
end
|
20
|
+
|
21
|
+
greet do
|
22
|
+
"#{greeter.name}: \"#{greeter.welcome}, #{greeted.name}!\""
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Person
|
27
|
+
attr_accessor :name
|
28
|
+
attr_accessor :greeting
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
class Greet_Someone
|
33
|
+
def initialize(greeter, greeted)
|
34
|
+
@greeter = greeter
|
35
|
+
@greeted = greeted
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class BasicTests < Test::Unit::TestCase
|
40
|
+
|
41
|
+
def test_define_context
|
42
|
+
name = :MyContext
|
43
|
+
ctx,source = Context::define name do end
|
44
|
+
assert_equal(ctx.name, "Kernel::#{name}")
|
45
|
+
assert_equal(source,"class #{name}\r\n\n\n private\n\n\n\r\nend")
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_define_role
|
49
|
+
name,role_name = :MyContextWithRole,:my_role
|
50
|
+
ctx,source = Context::define name do
|
51
|
+
role role_name do
|
52
|
+
role_go_do do
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
assert_not_nil(ctx)
|
58
|
+
assert_equal(ctx.name, "Kernel::#{name}")
|
59
|
+
assert_equal("class #{name}\r\n\n@#{role_name}\n\n private\ndef #{role_name};@#{role_name} end\n\n \ndef self_#{role_name}_role_go_do \n end\n\n\r\nend",source)
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_bind
|
63
|
+
name,role_name,other_name = :MyContextUsingBind,:my_role, :other_role
|
64
|
+
ctx,source = Context::define name do
|
65
|
+
role other_name do
|
66
|
+
plus_one do
|
67
|
+
(self + 1)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
go_do do
|
71
|
+
a = Array.new
|
72
|
+
[1,2].each do |e|
|
73
|
+
bind e => :other_role
|
74
|
+
a << e.plus_one
|
75
|
+
end
|
76
|
+
a
|
77
|
+
end
|
78
|
+
end
|
79
|
+
arr = MyContextUsingBind.new.go_do
|
80
|
+
assert_not_nil(ctx)
|
81
|
+
assert_equal(ctx.name, "Kernel::#{name}")
|
82
|
+
assert_equal("class MyContextUsingBind\r\n \ndef go_do \na = Array.new\n [1, 2].each do |e|\n temp____other_role = @other_role\n @other_role = e\n (a << self_other_role_plus_one)\n @other_role = temp____other_role\n end\n a\n end\n\n@other_role\n\n private\ndef other_role;@other_role end\n\n \ndef self_other_role_plus_one \n(other_role + 1) end\n\n\r\nend",source)
|
83
|
+
assert_equal(2,arr[0])
|
84
|
+
assert_equal(3,arr[1])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class TestExamples < Test::Unit::TestCase
|
89
|
+
def test_greeter
|
90
|
+
p1 = Person.new
|
91
|
+
p1.name = 'Bob'
|
92
|
+
p1.greeting = 'Hello'
|
93
|
+
|
94
|
+
p2 = Person.new
|
95
|
+
p2.name = 'World!'
|
96
|
+
p2.greeting = 'Greetings'
|
97
|
+
|
98
|
+
#Execute is automagically created for the default interaction (specified by the second argument in context :Greet_Someone, :greet do)
|
99
|
+
#Executes construc a context object and calls the default interaction on this object
|
100
|
+
res1 = Greet_Someone.execute p1, p2
|
101
|
+
res2 = Greet_Someone.new(p2, p1).greet
|
102
|
+
assert_equal(res1,"#{p1.name}: \"#{p1.greeting}, #{p2.name}!\"")
|
103
|
+
assert_equal(res1,Greet_Someone.new(p1, p2).greet) #verifies default action
|
104
|
+
#constructs a Greet_Someone context object and executes greet.
|
105
|
+
assert_equal(res2,"#{p2.name}: \"#{p2.greeting}, #{p1.name}!\"")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
data/lib/maroon.rb
CHANGED
@@ -151,7 +151,8 @@ class Context
|
|
151
151
|
code << "#{interactions}\n#{fields}\n private\n#{getters}\n#{impl}\n"
|
152
152
|
|
153
153
|
complete = "class #{name}\r\n#{code}\r\nend"
|
154
|
-
|
154
|
+
temp = c.class_eval(code)
|
155
|
+
return (temp ||c),complete
|
155
156
|
end
|
156
157
|
|
157
158
|
def role_or_interaction_method(method_name,*args, &b)
|
@@ -243,9 +244,10 @@ class Context
|
|
243
244
|
def rewrite_bind(aliased_role, local, block)
|
244
245
|
raise 'aliased_role must be a Symbol' unless aliased_role.instance_of? Symbol
|
245
246
|
raise 'local must be a Symbol' unless local.instance_of? Symbol
|
247
|
+
aliased_field = "@#{aliased_role}".to_sym
|
246
248
|
assignment = Sexp.new
|
247
249
|
assignment[0] = :iasgn
|
248
|
-
assignment[1] =
|
250
|
+
assignment[1] = aliased_field
|
249
251
|
load_arg = Sexp.new
|
250
252
|
load_arg[0] = :lvar
|
251
253
|
load_arg[1] = local
|
@@ -259,14 +261,14 @@ class Context
|
|
259
261
|
assignment[1] = temp_symbol
|
260
262
|
load_field = Sexp.new
|
261
263
|
load_field[0] = :ivar
|
262
|
-
load_field[1] =
|
264
|
+
load_field[1] = aliased_field
|
263
265
|
assignment[2] = load_field
|
264
266
|
block.insert 1, assignment
|
265
267
|
|
266
268
|
# reassign original player
|
267
269
|
assignment = Sexp.new
|
268
270
|
assignment[0] = :iasgn
|
269
|
-
assignment[1] =
|
271
|
+
assignment[1] = aliased_field
|
270
272
|
load_temp = Sexp.new
|
271
273
|
load_temp[0] = :lvar
|
272
274
|
load_temp[1] = temp_symbol
|
data/lib/maroon/version.rb
CHANGED
data/maroon.gemspec
CHANGED
@@ -13,7 +13,7 @@ the first language to support injectionless DCI.
|
|
13
13
|
|
14
14
|
The performance of code written using maroon is on par with code using regular method invocation.
|
15
15
|
|
16
|
-
For examples on how to use maroon look at the examples}
|
16
|
+
For examples on how to use maroon look at the examples found at the home page}
|
17
17
|
gem.summary = %q{maroon}
|
18
18
|
gem.homepage = 'https://github.com/runefs/maroon'
|
19
19
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maroon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -109,7 +109,7 @@ description: ! 'maroon makes DCI a DSL for Ruby it''s mainly based on the work g
|
|
109
109
|
invocation.
|
110
110
|
|
111
111
|
|
112
|
-
For examples on how to use maroon look at the examples'
|
112
|
+
For examples on how to use maroon look at the examples found at the home page'
|
113
113
|
email:
|
114
114
|
- funchsoltoft@gmail.com
|
115
115
|
executables: []
|
@@ -127,6 +127,7 @@ files:
|
|
127
127
|
- LICENSE.txt
|
128
128
|
- README.md
|
129
129
|
- Rakefile
|
130
|
+
- Test/Greeter_test.rb
|
130
131
|
- lib/maroon.rb
|
131
132
|
- lib/maroon/kernel.rb
|
132
133
|
- lib/maroon/version.rb
|