node_module 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/node_module.rb +17 -11
- data/lib/node_module/version.rb +1 -1
- data/spec/lib/node_module_spec.rb +37 -2
- metadata +3 -3
data/lib/node_module.rb
CHANGED
@@ -10,6 +10,20 @@ module NodeModule
|
|
10
10
|
base.extend ClassMethods
|
11
11
|
end
|
12
12
|
|
13
|
+
def self.js_context
|
14
|
+
@ctx ||= V8::Context.new do |ctx|
|
15
|
+
ctx.eval Opal::Builder.build('opal')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.convert_method_to_javascript(fn)
|
20
|
+
NodeModule.js_context.eval Opal.parse(fn)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.call_javascript_function(name, args = [])
|
24
|
+
NodeModule.js_context.eval "Opal.Object['$#{name}'].apply(this, #{args.to_json})"
|
25
|
+
end
|
26
|
+
|
13
27
|
module ClassMethods
|
14
28
|
def node_module(*methods)
|
15
29
|
if methods.empty?
|
@@ -22,21 +36,13 @@ module NodeModule
|
|
22
36
|
|
23
37
|
module_function
|
24
38
|
|
25
|
-
def self.eval_js(name, fn, args)
|
26
|
-
@ctx ||= V8::Context.new do |ctx|
|
27
|
-
ctx.eval Opal::Builder.build('opal')
|
28
|
-
end
|
29
|
-
|
30
|
-
@ctx.eval Opal.parse(fn)
|
31
|
-
@ctx.eval "Opal.Object.$#{name}.apply(this, #{args.to_json})"
|
32
|
-
end
|
33
|
-
|
34
39
|
def self.execute_methods_as_javascript!(methods, receiver)
|
35
40
|
methods.each do |name|
|
36
|
-
|
41
|
+
meth = receiver.instance_method(name).to_ruby
|
42
|
+
NodeModule.convert_method_to_javascript(meth)
|
37
43
|
|
38
44
|
receiver.send :define_method, name do |*args|
|
39
|
-
NodeModule.
|
45
|
+
NodeModule.call_javascript_function(__method__, args)
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|
data/lib/node_module/version.rb
CHANGED
@@ -12,11 +12,46 @@ describe NodeModule do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
|
16
|
+
|
17
|
+
describe "integration" do
|
16
18
|
let(:test_class) { TestClass.new }
|
17
19
|
|
18
|
-
it "
|
20
|
+
it "says 'hello' to all the people" do
|
19
21
|
test_class.hello_you("Sarah", "Jess").must_equal "hello Sarah, Jess"
|
20
22
|
end
|
21
23
|
end
|
24
|
+
|
25
|
+
describe "#convert_method_to_javascript" do
|
26
|
+
let(:test_method) { "def equal?(a, b); a == b; end;" }
|
27
|
+
|
28
|
+
it "successfully turns Ruby into Opal-style Javascript" do
|
29
|
+
js = NodeModule.convert_method_to_javascript(test_method)
|
30
|
+
js.to_s.must_match /function \(a, b\) \{\s+ return a\['\$=='\]\(b\);\s+\}/
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#call_javascript_function" do
|
35
|
+
let(:predicate_method) { "def equal?(a, b); a == b; end;" }
|
36
|
+
let(:bang_method) { "def boom!; 'BOOM!'; end;" }
|
37
|
+
|
38
|
+
before do
|
39
|
+
NodeModule.convert_method_to_javascript(predicate_method)
|
40
|
+
NodeModule.convert_method_to_javascript(bang_method)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "handles Ruby style predicate methods" do
|
44
|
+
result = NodeModule.call_javascript_function('equal?', [1, 1])
|
45
|
+
result.must_equal true
|
46
|
+
|
47
|
+
result = NodeModule.call_javascript_function('equal?', [1, 2])
|
48
|
+
result.must_equal false
|
49
|
+
end
|
50
|
+
|
51
|
+
it "handles Ruby style bang methods" do
|
52
|
+
result = NodeModule.call_javascript_function('boom!')
|
53
|
+
result.must_equal 'BOOM!'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
22
57
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: node_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -124,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
segments:
|
126
126
|
- 0
|
127
|
-
hash:
|
127
|
+
hash: 3868735360214896728
|
128
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
129
|
none: false
|
130
130
|
requirements:
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
segments:
|
135
135
|
- 0
|
136
|
-
hash:
|
136
|
+
hash: 3868735360214896728
|
137
137
|
requirements: []
|
138
138
|
rubyforge_project:
|
139
139
|
rubygems_version: 1.8.23
|