react_ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +5 -0
- data/lib/react_ruby/jsx.rb +13 -0
- data/lib/react_ruby/version.rb +3 -0
- data/lib/react_ruby.rb +60 -0
- data/react_ruby.gemspec +26 -0
- data/test/test_react_ruby.rb +77 -0
- data/vendor/assets/javascripts/JSXTransformer.js +13858 -0
- data/vendor/assets/javascripts/react.min.js +16 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 83f10f6453a3dbe6df111d3eddcba17aa9a8f8c6
|
4
|
+
data.tar.gz: 02d148bde0fad00d7500fa098bf247d5468e61fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25dfea8a82af1463bddd1a35c7c9eb74358d1e267c17db6310d5e0682be64dfb57a5d0ad4b40a348c331558bf315e62ce81ed24be4bf7a547420c610b37184ff
|
7
|
+
data.tar.gz: 9fa379df5ce76bad61db34b383a65196993d1e9a54ebe7add0c68ed539126a94a9e85d9097119dedc0233074ca6c7103838ccfdbeca6388f5321c60f5f522f71
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 minori tokuda
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# ReactRuby
|
2
|
+
|
3
|
+
ReactRuby is a template engine compiling react.js template
|
4
|
+
on ruby servers.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'react_ruby'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install react_ruby
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
if you wrote react templates to view.jsx:
|
25
|
+
```jsx
|
26
|
+
Post = React.createClass({
|
27
|
+
render: function(){
|
28
|
+
return <div>{this.props.body}</div>;
|
29
|
+
}
|
30
|
+
});
|
31
|
+
```
|
32
|
+
|
33
|
+
in ruby:
|
34
|
+
```ruby
|
35
|
+
require 'react_ruby'
|
36
|
+
|
37
|
+
template = File.read('view.jsx')
|
38
|
+
ReactRuby.compile(jsx: template)
|
39
|
+
ReactRuby.render('<Post body="Hello, world!" />')
|
40
|
+
#=> "<div data-reactid=\".n7s1kw8feo\" data-react-checksum=\"-972549822\">Hello, world!</div>"
|
41
|
+
```
|
42
|
+
|
43
|
+
Note: at first, you must compile context once, then you can invoke #render method at any time.
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it ( https://github.com/minoritea/react_ruby/fork )
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'execjs'
|
2
|
+
module ReactRuby
|
3
|
+
module JSX
|
4
|
+
def self.context
|
5
|
+
@context||=ExecJS.compile ('var global = global || this;' + JSCode.jsx)
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.transform(jsx)
|
9
|
+
result = context.call('JSXTransformer.transform', jsx)
|
10
|
+
result["code"]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/react_ruby.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'execjs'
|
2
|
+
require 'react_ruby/jsx'
|
3
|
+
|
4
|
+
module ReactRuby
|
5
|
+
module JSCode
|
6
|
+
|
7
|
+
def self.read(filename)
|
8
|
+
path = File.expand_path(
|
9
|
+
File.join('../../vendor/assets/javascripts', filename),
|
10
|
+
__FILE__)
|
11
|
+
::IO.read(path)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.react
|
15
|
+
@react ||= read('react.min.js')
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.jsx
|
19
|
+
@jsx ||= read('JSXTransformer.js')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Renderer
|
24
|
+
def initialize(config = {})
|
25
|
+
compile(config)
|
26
|
+
end
|
27
|
+
|
28
|
+
def compile(config = {})
|
29
|
+
@config = ReactRuby.config.merge(config)
|
30
|
+
jsx = @config[:jsx]
|
31
|
+
jsx = JSX.transform(jsx) if jsx
|
32
|
+
@context = ExecJS.compile(<<-JS)
|
33
|
+
var global = global || this;
|
34
|
+
#{@config[:react]}
|
35
|
+
#{jsx}
|
36
|
+
#{@config[:js]}
|
37
|
+
JS
|
38
|
+
end
|
39
|
+
|
40
|
+
def render(jsx)
|
41
|
+
@context.eval("React.renderToString(#{JSX.transform(jsx)})")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class << self
|
46
|
+
attr_accessor :config, :renderer
|
47
|
+
|
48
|
+
def compile(params = {})
|
49
|
+
@renderer = Renderer.new(params)
|
50
|
+
end
|
51
|
+
|
52
|
+
def render(jsx)
|
53
|
+
raise 'It must compile context bofore rendering.' unless @renderer
|
54
|
+
@renderer.render(jsx)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
self.config ||= {
|
58
|
+
react: JSCode.react
|
59
|
+
}
|
60
|
+
end
|
data/react_ruby.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'react_ruby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "react_ruby"
|
8
|
+
spec.version = ReactRuby::VERSION
|
9
|
+
spec.authors = ["minoritea"]
|
10
|
+
spec.email = ["minorityland@gmail.com"]
|
11
|
+
spec.summary = %q{ A React(react.js) template engine for ruby }
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/minoritea/react_ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "test-unit", "~> 3.0"
|
24
|
+
|
25
|
+
spec.add_runtime_dependency 'execjs', '~> 2.0'
|
26
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'react_ruby'
|
3
|
+
|
4
|
+
class JSXTest < Test::Unit::TestCase
|
5
|
+
def test_react_method
|
6
|
+
jsx = '<div>hello</div>'
|
7
|
+
assert_equal('React.createElement("div", null, "hello")',
|
8
|
+
ReactRuby::JSX.transform(jsx))
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_react_method_with_multi_line_jsx
|
12
|
+
jsx = <<-JSX
|
13
|
+
this.Post = React.createClass({
|
14
|
+
render: function(){
|
15
|
+
return <div>
|
16
|
+
<Body message={"hello"}/>
|
17
|
+
Hello
|
18
|
+
</div>;
|
19
|
+
}
|
20
|
+
});
|
21
|
+
JSX
|
22
|
+
assert_equal(<<-JSX, ReactRuby::JSX.transform(jsx))
|
23
|
+
this.Post = React.createClass({displayName: 'Post',
|
24
|
+
render: function(){
|
25
|
+
return React.createElement("div", null,
|
26
|
+
React.createElement(Body, {message: "hello"}),
|
27
|
+
"Hello"
|
28
|
+
);
|
29
|
+
}
|
30
|
+
});
|
31
|
+
JSX
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class RendererTest < Test::Unit::TestCase
|
36
|
+
def test_render_with_jsx
|
37
|
+
renderer = ReactRuby::Renderer.new(jsx: <<-JSX)
|
38
|
+
Test = React.createClass({
|
39
|
+
render: function(){
|
40
|
+
return <div>{this.props.testMessage}</div>;
|
41
|
+
}
|
42
|
+
});
|
43
|
+
JSX
|
44
|
+
assert_match(/<div [^>]+>test<\/div>/,
|
45
|
+
renderer.render('<Test testMessage="test"/>'))
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_render_with_js
|
49
|
+
renderer = ReactRuby::Renderer.new(js: <<-JS)
|
50
|
+
Test = React.createClass({displayName: 'Test',
|
51
|
+
render: function(){
|
52
|
+
return React.createElement("div", null, this.props.testMessage);
|
53
|
+
}
|
54
|
+
});
|
55
|
+
JS
|
56
|
+
assert_match(/<div [^>]+>test<\/div>/,
|
57
|
+
renderer.render('<Test testMessage="test"/>'))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class ReactRubyTest < Test::Unit::TestCase
|
62
|
+
def setup
|
63
|
+
ReactRuby.renderer = nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_renderer
|
67
|
+
assert_nil(ReactRuby.renderer)
|
68
|
+
ReactRuby.compile
|
69
|
+
assert(ReactRuby.renderer)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_render_before_compile
|
73
|
+
assert_raise{ReactRuby.render('<div></div>')}
|
74
|
+
ReactRuby.compile
|
75
|
+
assert_nothing_thrown{ReactRuby.render('<div></div>')}
|
76
|
+
end
|
77
|
+
end
|