node_module 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # node_module
2
2
 
3
+ > **I'll often drop down to node.js if I really need to be close to the metal**
4
+ >
5
+ > &mdash; <cite>https://twitter.com/shit_hn_says/status/234856345579446272</cite>
6
+
3
7
  Ruby is a pretty high level language, so isn't perfectly suited to
4
8
  things such as:
5
9
 
@@ -28,6 +32,20 @@ suited to things such as:
28
32
  1. Systems programming
29
33
  2. Being web-scale
30
34
 
35
+ ## How to install
36
+
37
+ If you use Bundler, add it to your `Gemfile`, then run `bundle install`.
38
+
39
+ ```ruby
40
+ gem 'node_module'
41
+ ```
42
+
43
+ If you don't, install the gem manually.
44
+
45
+ ```shell
46
+ gem install node_module
47
+ ```
48
+
31
49
  ## How to use
32
50
 
33
51
  ```ruby
@@ -35,6 +53,8 @@ require 'node_module'
35
53
 
36
54
  class AbstractConcepts
37
55
 
56
+ include NodeModule
57
+
38
58
  def existentialism
39
59
  self.name = "me"
40
60
  end
@@ -45,8 +65,7 @@ class AbstractConcepts
45
65
 
46
66
  node_module
47
67
 
48
- def pythagoras_theorm
49
- a, b, c = [3, 4, 5]
68
+ def pythagorean_triplet?(a, b, c)
50
69
  a**2 + b**2 == c**2
51
70
  end
52
71
  end
@@ -57,27 +76,21 @@ executed.
57
76
 
58
77
  This is a ridiculous proof of concept, so there are a few issues...
59
78
 
60
- ## Caveats
79
+ ## Current limitations
80
+
81
+ - Things might break if your methods aren't fully self-contained. This
82
+ is because they're currently compiled outside of the scope of the
83
+ class they were defined in. (Although there's nothing preventing an entire class
84
+ being parsed as javascript in future).
61
85
 
62
- - Only the body of a method is used, so there is no access to the name
63
- or arguments passed.
86
+ - It doesn't actually use Node yet
64
87
 
65
- - References to the rest of the class will fail, because it all falls
66
- out of scope when converted.
88
+ - It buggers up IRB
67
89
 
68
- - Node isn't actually being used yet, so calls to `puts` will blow up
69
- as V8 doesn't have the `console` object.
90
+ - It probably can't handle anything too clever.
70
91
 
71
- - Probably lots of other stuff I don't know about, because I haven't
72
- felt insane enough to check everything out
92
+ - [You can't use 1.9 syntax](https://github.com/quix/live_ast#description)
73
93
 
74
94
  ## What Ruby code will work?
75
95
 
76
96
  Check [Opal](http://opalrb.org) for that. It's what does all the hard work.
77
-
78
- ## WHY!??
79
-
80
- > **I'll often drop down to node.js if I really need to be close to the metal**
81
- > https://twitter.com/shit_hn_says/status/234856345579446272
82
-
83
- 'nuff said.
data/lib/node_module.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "node_module/version"
2
2
  require 'live_ast/to_ruby'
3
3
  require 'opal'
4
+ require 'json'
4
5
  require 'v8'
5
6
 
6
7
  module NodeModule
@@ -21,21 +22,21 @@ module NodeModule
21
22
 
22
23
  module_function
23
24
 
24
- def self.eval_js(js)
25
+ def self.eval_js(name, fn, args)
25
26
  @ctx ||= V8::Context.new do |ctx|
26
27
  ctx.eval Opal::Builder.build('opal')
27
28
  end
28
29
 
29
- @ctx.eval Opal.parse(js)
30
+ @ctx.eval Opal.parse(fn)
31
+ @ctx.eval "Opal.Object.$#{name}.apply(this, #{args.to_json})"
30
32
  end
31
33
 
32
34
  def self.execute_methods_as_javascript!(methods, receiver)
33
35
  methods.each do |name|
34
- meth = receiver.instance_method(name)
35
- body = meth.to_ruby.split("\n")[1..-2].join.strip
36
+ fn = receiver.instance_method(name).to_ruby
36
37
 
37
- receiver.send :define_method, name do
38
- NodeModule.eval_js(body)
38
+ receiver.send :define_method, name do |*args|
39
+ NodeModule.eval_js(name, fn, args)
39
40
  end
40
41
  end
41
42
  end
@@ -1,3 +1,3 @@
1
1
  module NodeModule
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/node_module.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["lee@new-bamboo.co.uk"]
11
11
  spec.description = %q{Evaluate methods in Ruby as Javascript instead}
12
12
  spec.summary = %q{Get really close to the metal in Ruby}
13
- spec.homepage = ""
13
+ spec.homepage = "http://github.com/leemachin/node_module"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe NodeModule do
4
+
5
+ class TestClass
6
+ include NodeModule
7
+
8
+ node_module
9
+
10
+ def hello_you(*names)
11
+ "hello #{names.join(', ')}"
12
+ end
13
+ end
14
+
15
+ describe "#hello_you" do
16
+ let(:test_class) { TestClass.new }
17
+
18
+ it "should say 'hello' to all the people" do
19
+ test_class.hello_you("Sarah", "Jess").must_equal "hello Sarah, Jess"
20
+ end
21
+ end
22
+ 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.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-17 00:00:00.000000000 Z
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: live_ast
@@ -101,14 +101,15 @@ files:
101
101
  - .gitignore
102
102
  - .ruby-version
103
103
  - Gemfile
104
- - LICENSE.txt
104
+ - LICENSE
105
105
  - README.md
106
106
  - Rakefile
107
107
  - lib/node_module.rb
108
108
  - lib/node_module/version.rb
109
109
  - node_module.gemspec
110
+ - spec/lib/node_module_spec.rb
110
111
  - spec/spec_helper.rb
111
- homepage: ''
112
+ homepage: http://github.com/leemachin/node_module
112
113
  licenses:
113
114
  - MIT
114
115
  post_install_message:
@@ -123,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
124
  version: '0'
124
125
  segments:
125
126
  - 0
126
- hash: -3867090681638509308
127
+ hash: -2290275566948015265
127
128
  required_rubygems_version: !ruby/object:Gem::Requirement
128
129
  none: false
129
130
  requirements:
@@ -132,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
133
  version: '0'
133
134
  segments:
134
135
  - 0
135
- hash: -3867090681638509308
136
+ hash: -2290275566948015265
136
137
  requirements: []
137
138
  rubyforge_project:
138
139
  rubygems_version: 1.8.23
@@ -140,4 +141,5 @@ signing_key:
140
141
  specification_version: 3
141
142
  summary: Get really close to the metal in Ruby
142
143
  test_files:
144
+ - spec/lib/node_module_spec.rb
143
145
  - spec/spec_helper.rb