acts_as_runnable_code 1.0.1 → 1.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/README.txt +19 -19
- data/Rakefile +1 -1
- data/lib/acts_as_runnable_code.rb +30 -3
- data/test/test_acts_as_runnable_code.rb +42 -6
- metadata +3 -3
data/README.txt
CHANGED
@@ -15,25 +15,25 @@ See the example app using this gem: http://tictactoe.mapleton.net
|
|
15
15
|
|
16
16
|
== SYNOPSIS:
|
17
17
|
|
18
|
-
require "sandbox"
|
19
|
-
require "acts_as_runnable_code"
|
20
|
-
|
21
|
-
class Algorithm < ActiveRecord::Base
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
class Board < ActiveRecord::Base
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
# Runs a user uploaded alogrithm in the context of a board
|
35
|
-
board = Board.find(id)
|
36
|
-
board.algorithm.run_code(board, :timeout => 0.5)
|
18
|
+
require "sandbox"
|
19
|
+
require "acts_as_runnable_code"
|
20
|
+
|
21
|
+
class Algorithm < ActiveRecord::Base
|
22
|
+
# "code" is a database attribute in this case, containing user's code
|
23
|
+
acts_as_runnable_code :classes => ["Board"]
|
24
|
+
end
|
25
|
+
|
26
|
+
class Board < ActiveRecord::Base
|
27
|
+
belongs_to :algorithm
|
28
|
+
acts_as_wrapped_class, :methods => [:moves, :make_move!, :turn, :log_info, :log_debug]
|
29
|
+
def moves; end
|
30
|
+
def make_move!(x,y); end;
|
31
|
+
...
|
32
|
+
end
|
33
|
+
|
34
|
+
# Runs a user uploaded alogrithm in the context of a board
|
35
|
+
board = Board.find(id)
|
36
|
+
board.algorithm.run_code(board, :timeout => 0.5)
|
37
37
|
|
38
38
|
== REQUIREMENTS:
|
39
39
|
|
data/Rakefile
CHANGED
@@ -15,7 +15,7 @@ Hoe.new('acts_as_runnable_code', ActsAsRunnableCode::VERSION) do |p|
|
|
15
15
|
p.rubyforge_name = "runnable-code"
|
16
16
|
p.remote_rdoc_dir = ""
|
17
17
|
p.rsync_args << ' --exclude=statsvn/'
|
18
|
-
p.extra_deps << ['acts_as_wrapped_class', '>= 1.0.
|
18
|
+
p.extra_deps << ['acts_as_wrapped_class', '>= 1.0.1']
|
19
19
|
end
|
20
20
|
|
21
21
|
# vim: syntax=Ruby
|
@@ -3,7 +3,7 @@ require "rubygems"
|
|
3
3
|
require "acts_as_wrapped_class"
|
4
4
|
|
5
5
|
module ActsAsRunnableCode
|
6
|
-
VERSION="1.0.
|
6
|
+
VERSION="1.0.2"
|
7
7
|
module InstanceMethods
|
8
8
|
# Run the user's code in the context of a toplevel_object
|
9
9
|
# The toplevel_object will be automatically wrapped and copied into the sandbox
|
@@ -33,8 +33,10 @@ module ActsAsRunnableCode
|
|
33
33
|
end
|
34
34
|
|
35
35
|
classes = self.class.runnable_code_options[:classes] || ActsAsWrappedClass::WRAPPED_CLASSES
|
36
|
+
|
36
37
|
classes.each do |c|
|
37
|
-
|
38
|
+
name = "#{c}Wrapper"
|
39
|
+
s.ref eval(name)
|
38
40
|
end
|
39
41
|
|
40
42
|
self.class.instance_variable_set("@sandbox", s) if self.class.runnable_code_options[:singleton_sandbox]
|
@@ -51,7 +53,32 @@ module ActsAsRunnableCode
|
|
51
53
|
|
52
54
|
def runnable_code?
|
53
55
|
true
|
54
|
-
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# a list of runnable classes in the format ["SomeClassWrapper", "OtherClassWraper"]
|
59
|
+
def runnable_classes
|
60
|
+
(runnable_code_options[:classes] || ActsAsWrappedClass::WRAPPED_CLASSES).collect { |c| "#{c}Wrapper" }
|
61
|
+
end
|
62
|
+
|
63
|
+
# a list of runnable methods in the format {"SomeClassWrapper" => ["method_a", "method_b"], "OtherClassWraper" => ["method_c", "method_d"]]
|
64
|
+
def runnable_methods
|
65
|
+
meths = {}
|
66
|
+
runnable_classes.each { |c|
|
67
|
+
klass = eval("::#{c}")
|
68
|
+
meths[c.to_s] ||= []
|
69
|
+
meths[c.to_s] += klass.allowed_methods if klass.respond_to?(:allowed_methods)
|
70
|
+
}
|
71
|
+
meths
|
72
|
+
end
|
73
|
+
|
74
|
+
# same as runnable methods but excludes all the methods of +Object
|
75
|
+
def runnable_methods_no_object
|
76
|
+
newv = {}
|
77
|
+
runnable_methods.each do |k,v|
|
78
|
+
newv[k] = v - Object.public_instance_methods
|
79
|
+
end
|
80
|
+
newv
|
81
|
+
end
|
55
82
|
end
|
56
83
|
|
57
84
|
# Mark this class as containing a method which returns user generated code.
|
@@ -5,13 +5,28 @@ require File.dirname(__FILE__) + "/../lib/acts_as_runnable_code"
|
|
5
5
|
class SampleClass
|
6
6
|
acts_as_runnable_code
|
7
7
|
|
8
|
+
attr_writer :code
|
9
|
+
|
8
10
|
def code
|
9
|
-
"'hello world'"
|
11
|
+
@code || "'hello world'"
|
10
12
|
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class WrappedClass2
|
17
|
+
acts_as_wrapped_class :methods => [:safeone]
|
18
|
+
|
19
|
+
def safeone
|
20
|
+
777
|
21
|
+
end
|
22
|
+
|
23
|
+
def unsafeone
|
24
|
+
666
|
25
|
+
end
|
11
26
|
end
|
12
27
|
|
13
28
|
class OtherClass
|
14
|
-
acts_as_runnable_code :code_field => :data2, :unsafe_sandbox => true, :singleton_sandbox => true
|
29
|
+
acts_as_runnable_code :classes => ["WrappedClass2"], :code_field => :data2, :unsafe_sandbox => true, :singleton_sandbox => true
|
15
30
|
|
16
31
|
attr :code, true
|
17
32
|
|
@@ -68,12 +83,33 @@ class ActsAsRunnableCodeTest < Test::Unit::TestCase
|
|
68
83
|
end
|
69
84
|
|
70
85
|
def test_run_code
|
71
|
-
w = WrappedClass.new(5.5)
|
72
86
|
assert_equal "hello world", SampleClass.new.run_code
|
73
|
-
|
87
|
+
|
88
|
+
w = WrappedClass.new(5.5)
|
89
|
+
sc = SampleClass.new
|
90
|
+
sc.code = "value"
|
91
|
+
assert_equal 5.5, sc.run_code(w)
|
74
92
|
|
93
|
+
sc = SampleClass.new
|
94
|
+
sc.code = "other"
|
95
|
+
assert_equal WrappedClass, sc.run_code(w).class
|
96
|
+
|
97
|
+
w = WrappedClass2.new
|
75
98
|
oc = OtherClass.new
|
76
|
-
oc.code = "
|
77
|
-
assert_equal
|
99
|
+
oc.code = "safeone"
|
100
|
+
assert_equal 777, oc.run_code(w)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_arunnable_classes
|
104
|
+
assert_equal 2, SampleClass.runnable_classes.length
|
105
|
+
assert_equal ["WrappedClass2Wrapper"], OtherClass.runnable_classes
|
106
|
+
|
107
|
+
assert_equal OtherClass.runnable_methods, {"WrappedClass2Wrapper" => ["safeone"]}
|
108
|
+
assert_contents_same ["other", "value", "safeone"], SampleClass.runnable_methods_no_object.values.flatten.uniq
|
78
109
|
end
|
110
|
+
|
111
|
+
def assert_contents_same(array1, array2)
|
112
|
+
assert_equal array1.length, array2.length, "#{array1.inspect} != #{array2.inspect}"
|
113
|
+
array1.each { |a| assert array2.include?(a), "#{array2.inspect} does not contain #{a.inspect}" }
|
114
|
+
end
|
79
115
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: acts_as_runnable_code
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-10-
|
6
|
+
version: 1.0.2
|
7
|
+
date: 2007-10-25 00:00:00 -07:00
|
8
8
|
summary: Mark a class as containing a method which returns sandbox runnable code. Helps by building the sandbox and setting up the eval for you.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1.0.
|
61
|
+
version: 1.0.1
|
62
62
|
version:
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
64
|
name: hoe
|