reactive-ruby 0.7.3 → 0.7.4

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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/README.md +67 -229
  4. data/example/tutorial/.gitignore +17 -0
  5. data/example/tutorial/Gemfile +51 -0
  6. data/example/tutorial/Gemfile.lock +211 -0
  7. data/example/tutorial/Gemfile.xlock +211 -0
  8. data/example/tutorial/README.rdoc +28 -0
  9. data/example/tutorial/Rakefile +6 -0
  10. data/example/tutorial/app/assets/images/.keep +0 -0
  11. data/example/tutorial/app/assets/javascripts/application.rb +10 -0
  12. data/example/tutorial/app/assets/stylesheets/application.css +15 -0
  13. data/example/tutorial/app/controllers/application_controller.rb +6 -0
  14. data/example/tutorial/app/controllers/concerns/.keep +0 -0
  15. data/example/tutorial/app/controllers/home_controller.rb +6 -0
  16. data/example/tutorial/app/helpers/application_helper.rb +2 -0
  17. data/example/tutorial/app/mailers/.keep +0 -0
  18. data/example/tutorial/app/models/.keep +0 -0
  19. data/example/tutorial/app/models/concerns/.keep +0 -0
  20. data/example/tutorial/app/views/components.rb +3 -0
  21. data/example/tutorial/app/views/components/home/show.rb +18 -0
  22. data/example/tutorial/app/views/layouts/application.html.erb +14 -0
  23. data/example/tutorial/bin/bundle +3 -0
  24. data/example/tutorial/bin/rails +8 -0
  25. data/example/tutorial/bin/rake +8 -0
  26. data/example/tutorial/bin/setup +29 -0
  27. data/example/tutorial/bin/spring +15 -0
  28. data/example/tutorial/config.ru +4 -0
  29. data/example/tutorial/config/application.rb +26 -0
  30. data/example/tutorial/config/boot.rb +3 -0
  31. data/example/tutorial/config/database.yml +25 -0
  32. data/example/tutorial/config/environment.rb +5 -0
  33. data/example/tutorial/config/environments/development.rb +41 -0
  34. data/example/tutorial/config/environments/production.rb +79 -0
  35. data/example/tutorial/config/environments/test.rb +42 -0
  36. data/example/tutorial/config/initializers/assets.rb +11 -0
  37. data/example/tutorial/config/initializers/backtrace_silencers.rb +7 -0
  38. data/example/tutorial/config/initializers/cookies_serializer.rb +3 -0
  39. data/example/tutorial/config/initializers/filter_parameter_logging.rb +4 -0
  40. data/example/tutorial/config/initializers/inflections.rb +16 -0
  41. data/example/tutorial/config/initializers/mime_types.rb +4 -0
  42. data/example/tutorial/config/initializers/session_store.rb +3 -0
  43. data/example/tutorial/config/initializers/wrap_parameters.rb +14 -0
  44. data/example/tutorial/config/locales/en.yml +23 -0
  45. data/example/tutorial/config/routes.rb +59 -0
  46. data/example/tutorial/config/secrets.yml +22 -0
  47. data/example/tutorial/db/seeds.rb +7 -0
  48. data/example/tutorial/lib/assets/.keep +0 -0
  49. data/example/tutorial/lib/tasks/.keep +0 -0
  50. data/example/tutorial/log/.keep +0 -0
  51. data/example/tutorial/public/404.html +67 -0
  52. data/example/tutorial/public/422.html +67 -0
  53. data/example/tutorial/public/500.html +66 -0
  54. data/example/tutorial/public/favicon.ico +0 -0
  55. data/example/tutorial/public/robots.txt +5 -0
  56. data/example/tutorial/test/controllers/.keep +0 -0
  57. data/example/tutorial/test/fixtures/.keep +0 -0
  58. data/example/tutorial/test/helpers/.keep +0 -0
  59. data/example/tutorial/test/integration/.keep +0 -0
  60. data/example/tutorial/test/mailers/.keep +0 -0
  61. data/example/tutorial/test/models/.keep +0 -0
  62. data/example/tutorial/test/test_helper.rb +10 -0
  63. data/example/tutorial/vendor/assets/javascripts/.keep +0 -0
  64. data/example/tutorial/vendor/assets/stylesheets/.keep +0 -0
  65. data/lib/rails-helpers/react_component.rb +19 -1
  66. data/lib/reactive-ruby.rb +9 -0
  67. data/lib/reactive-ruby/version.rb +1 -1
  68. data/old-readme +220 -0
  69. data/reactive-ruby.gemspec +5 -2
  70. metadata +87 -12
  71. data/Gemfile.lock +0 -53
@@ -1,3 +1,3 @@
1
1
  module React
2
- VERSION = "0.7.3"
2
+ VERSION = "0.7.4"
3
3
  end
@@ -0,0 +1,220 @@
1
+ and in your Opal application,
2
+
3
+ ```ruby
4
+ require "opal-react"
5
+ require "react"
6
+
7
+ React.render(React.create_element('h1'){ "Hello World!" }, `document.body`)
8
+ ```
9
+
10
+ For a complete example covering most key features, as well as integration with a server (Sinatra, etc), see setup of [Examples](example/tutorial). For additional information on integrating Opal with a server see the [official docs](http://opalrb.org/docs/) of Opal.
11
+
12
+ ## React Overview
13
+
14
+ ### Basics
15
+
16
+ The biggest problem with react is that its almost too simple.
17
+
18
+ In react you define components. Components are simply classes that have a "render" method. The render method "draws" a chunk of
19
+ HTML.
20
+
21
+ Here is a very simple component:
22
+
23
+ ```ruby
24
+
25
+ require 'opal'
26
+ require 'opal-react'
27
+
28
+ class Hello
29
+ def render
30
+ "hello world"
31
+ end
32
+ end
33
+
34
+ # to use the component we first create an instance o
35
+
36
+ Include the `React::Component` mixin in a class to turn it into a react component
37
+
38
+ ```ruby
39
+ require 'opal'
40
+ require 'opal-react'
41
+
42
+ class HelloMessage
43
+
44
+ include React::Component # will create a new component named HelloMessage
45
+
46
+ MSG = {great: 'Cool!', bad: 'Cheer up!'}
47
+
48
+ optional_param :mood
49
+ required_param :name
50
+ define_state :foo, "Default greeting"
51
+
52
+ before_mount do # you can define life cycle callbacks inline
53
+ foo! "#{name}: #{MSG[mood]}" if mood # change the state of foo using foo!, read the state using foo
54
+ end
55
+
56
+ after_mount :log # you can also define life cycle callbacks by reference to a method
57
+
58
+ def log
59
+ puts "mounted!"
60
+ end
61
+
62
+ def render # render method MUST return just one component
63
+ div do # basic dsl syntax component_name(options) { ...children... }
64
+ span { "#{foo} #{name}!" } # all html5 components are defined with lower case text
65
+ end
66
+ end
67
+ end
68
+
69
+ class App
70
+ include React::Component
71
+
72
+ def render
73
+ HelloMessage name: 'John', mood: :great # new components are accessed via the class name
74
+ end
75
+ end
76
+
77
+ # later we will talk about nicer ways to do this: For now wait till doc is loaded
78
+ # then tell React to create an "App" and render it into the document body.
79
+
80
+ `window.onload = #{lambda {React.render(React.create_element(App), `document.body`)}}`
81
+
82
+ # -> console says: mounted!
83
+ ```
84
+
85
+ * Callback of life cycle could be created through helpers `before_mount`, `after_mount`, etc
86
+ * `this.props` is accessed through method `self.params`
87
+ * Use helper method `define_state` to create setter & getter of `this.state` for you
88
+ * For the detailed mapping to the original API, see [this issue](https://github.com/zetachang/react.rb/issues/2) for reference. Complete reference will come soon.
89
+
90
+ ### Element Building DSL
91
+
92
+ As a replacement of JSX, include `React::Component` and you can build `React.Element` hierarchy without all the `React.create_element` noises.
93
+
94
+ ```ruby
95
+ def render
96
+ div do
97
+ h1 { "Title" }
98
+ h2 { "subtitle"}
99
+ div(class_name: 'fancy', id: 'foo') { span { "some text #{interpolation}"} }
100
+ present FancyElement, fancy_props: '10'
101
+ end
102
+ end
103
+ ```
104
+
105
+ ### Props validation
106
+
107
+ How about props validation? Inspired by [Grape API](https://github.com/intridea/grape), props validation rule could be created easily through `params` class method as below,
108
+
109
+ ```ruby
110
+ class App
111
+ include React::Component
112
+
113
+ params do
114
+ requires :username, type: String
115
+ requires :enum, values: ['foo', 'bar', 'awesome']
116
+ requires :payload, type: Todo # yeah, a plain Ruby class
117
+ optional :filters, type: Array[String]
118
+ optional :flash_message, type: String, default: 'Welcome!' # no need to feed through `getDefaultProps`
119
+ end
120
+
121
+ def render
122
+ div
123
+ end
124
+ end
125
+ ```
126
+
127
+ ### Mixins
128
+
129
+ Simply create a Ruby module to encapsulate the behavior. Example below is modified from the original [React.js Exmaple on Mixin](http://facebook.github.io/react/docs/reusable-components.html#mixins). [Opal Browser](https://github.com/opal/opal-browser) syntax are used here to make it cleaner.
130
+
131
+ ```ruby
132
+ module SetInterval
133
+ def self.included(base)
134
+ base.class_eval do
135
+ before_mount { @interval = [] }
136
+ before_unmount do
137
+ # abort associated timer of a component right before unmount
138
+ @interval.each { |i| i.abort }
139
+ end
140
+ end
141
+ end
142
+
143
+ def set_interval(seconds, &block)
144
+ @interval << $window.every(seconds, &block)
145
+ end
146
+ end
147
+
148
+ class TickTock
149
+ include React::Component
150
+ include SetInterval
151
+
152
+ define_state(:seconds) { 0 }
153
+
154
+ before_mount do
155
+ set_interval(1) { self.seconds = self.seconds + 1 }
156
+ set_interval(1) { puts "Tick!" }
157
+ end
158
+
159
+ def render
160
+ span do
161
+ "React has been running for: #{self.seconds}"
162
+ end
163
+ end
164
+ end
165
+
166
+ React.render(React.create_element(TickTock), $document.body.to_n)
167
+
168
+ $window.after(5) do
169
+ React.unmount_component_at_node($document.body.to_n)
170
+ end
171
+
172
+ # => Tick!
173
+ # => ... for 5 times then stop ticking after 5 seconds
174
+ ```
175
+
176
+
177
+ ### A Simple Component
178
+
179
+ A ruby class which define method `render` is a valid component.
180
+
181
+ ```ruby
182
+ class HelloMessage
183
+ def render
184
+ React.create_element("div") { "Hello World!" }
185
+ end
186
+ end
187
+
188
+ puts React.render_to_static_markup(React.create_element(HelloMessage))
189
+
190
+ # => '<div>Hello World!</div>'
191
+ ```
192
+
193
+ ### More complicated one
194
+
195
+ To hook into native ReactComponent life cycle, the native `this` will be passed to the class's initializer. And all corresponding life cycle methods (`componentDidMount`, etc) will be invoked on the instance using the snake-case method name.
196
+
197
+ ```ruby
198
+ class HelloMessage
199
+ def initialize(native)
200
+ @native = Native(native)
201
+ end
202
+
203
+ def component_will_mount
204
+ puts "will mount!"
205
+ end
206
+
207
+ def render
208
+ React.create_element("div") { "Hello #{@native[:props][:name]}!" }
209
+ end
210
+ end
211
+
212
+ puts React.render_to_static_markup(React.create_element(HelloMessage, name: 'John'))
213
+
214
+ # => will_mount!
215
+ # => '<div>Hello John!</div>'
216
+ ```
217
+ ## Example
218
+
219
+ * React Tutorial: see [example/react-tutorial](example/react-tutorial), the original CommentBox example.
220
+ * TodoMVC: see [example/todos](example/todos), your beloved TodoMVC <3.
@@ -1,5 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/reactive-ruby/version', __FILE__)
2
+ $:.push File.expand_path('../lib/', __FILE__)
3
+
4
+ require 'reactive-ruby/version'
3
5
 
4
6
  Gem::Specification.new do |s|
5
7
  s.name = 'reactive-ruby'
@@ -18,8 +20,9 @@ Gem::Specification.new do |s|
18
20
 
19
21
  s.add_runtime_dependency 'opal'#, '~> 0.7.0'
20
22
  s.add_runtime_dependency 'opal-activesupport'
23
+ s.add_runtime_dependency 'opal-jquery'
24
+ s.add_runtime_dependency 'opal-browser'
21
25
  s.add_development_dependency 'react-source', '~> 0.12'
22
26
  s.add_development_dependency 'opal-rspec'
23
27
  s.add_development_dependency 'sinatra'
24
- s.add_development_dependency 'opal-jquery'
25
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reactive-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Chang
@@ -39,27 +39,27 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: react-source
42
+ name: opal-jquery
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '0.12'
48
- type: :development
47
+ version: '0'
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '0.12'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: opal-rspec
56
+ name: opal-browser
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- type: :development
62
+ type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
@@ -67,7 +67,21 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: sinatra
70
+ name: react-source
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: opal-rspec
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
@@ -81,7 +95,7 @@ dependencies:
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
- name: opal-jquery
98
+ name: sinatra
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - ">="
@@ -103,7 +117,6 @@ extra_rdoc_files: []
103
117
  files:
104
118
  - ".gitignore"
105
119
  - Gemfile
106
- - Gemfile.lock
107
120
  - LICENSE
108
121
  - README.md
109
122
  - config.ru
@@ -134,6 +147,67 @@ files:
134
147
  - example/todos/vendor/base.css
135
148
  - example/todos/vendor/bg.png
136
149
  - example/todos/vendor/jquery.js
150
+ - example/tutorial/.gitignore
151
+ - example/tutorial/Gemfile
152
+ - example/tutorial/Gemfile.lock
153
+ - example/tutorial/Gemfile.xlock
154
+ - example/tutorial/README.rdoc
155
+ - example/tutorial/Rakefile
156
+ - example/tutorial/app/assets/images/.keep
157
+ - example/tutorial/app/assets/javascripts/application.rb
158
+ - example/tutorial/app/assets/stylesheets/application.css
159
+ - example/tutorial/app/controllers/application_controller.rb
160
+ - example/tutorial/app/controllers/concerns/.keep
161
+ - example/tutorial/app/controllers/home_controller.rb
162
+ - example/tutorial/app/helpers/application_helper.rb
163
+ - example/tutorial/app/mailers/.keep
164
+ - example/tutorial/app/models/.keep
165
+ - example/tutorial/app/models/concerns/.keep
166
+ - example/tutorial/app/views/components.rb
167
+ - example/tutorial/app/views/components/home/show.rb
168
+ - example/tutorial/app/views/layouts/application.html.erb
169
+ - example/tutorial/bin/bundle
170
+ - example/tutorial/bin/rails
171
+ - example/tutorial/bin/rake
172
+ - example/tutorial/bin/setup
173
+ - example/tutorial/bin/spring
174
+ - example/tutorial/config.ru
175
+ - example/tutorial/config/application.rb
176
+ - example/tutorial/config/boot.rb
177
+ - example/tutorial/config/database.yml
178
+ - example/tutorial/config/environment.rb
179
+ - example/tutorial/config/environments/development.rb
180
+ - example/tutorial/config/environments/production.rb
181
+ - example/tutorial/config/environments/test.rb
182
+ - example/tutorial/config/initializers/assets.rb
183
+ - example/tutorial/config/initializers/backtrace_silencers.rb
184
+ - example/tutorial/config/initializers/cookies_serializer.rb
185
+ - example/tutorial/config/initializers/filter_parameter_logging.rb
186
+ - example/tutorial/config/initializers/inflections.rb
187
+ - example/tutorial/config/initializers/mime_types.rb
188
+ - example/tutorial/config/initializers/session_store.rb
189
+ - example/tutorial/config/initializers/wrap_parameters.rb
190
+ - example/tutorial/config/locales/en.yml
191
+ - example/tutorial/config/routes.rb
192
+ - example/tutorial/config/secrets.yml
193
+ - example/tutorial/db/seeds.rb
194
+ - example/tutorial/lib/assets/.keep
195
+ - example/tutorial/lib/tasks/.keep
196
+ - example/tutorial/log/.keep
197
+ - example/tutorial/public/404.html
198
+ - example/tutorial/public/422.html
199
+ - example/tutorial/public/500.html
200
+ - example/tutorial/public/favicon.ico
201
+ - example/tutorial/public/robots.txt
202
+ - example/tutorial/test/controllers/.keep
203
+ - example/tutorial/test/fixtures/.keep
204
+ - example/tutorial/test/helpers/.keep
205
+ - example/tutorial/test/integration/.keep
206
+ - example/tutorial/test/mailers/.keep
207
+ - example/tutorial/test/models/.keep
208
+ - example/tutorial/test/test_helper.rb
209
+ - example/tutorial/vendor/assets/javascripts/.keep
210
+ - example/tutorial/vendor/assets/stylesheets/.keep
137
211
  - lib/rails-helpers/react_component.rb
138
212
  - lib/reactive-ruby.rb
139
213
  - lib/reactive-ruby/api.rb
@@ -154,6 +228,7 @@ files:
154
228
  - logo1.png
155
229
  - logo2.png
156
230
  - logo3.png
231
+ - old-readme
157
232
  - reactive-ruby.gemspec
158
233
  - spec/callbacks_spec.rb
159
234
  - spec/component_spec.rb
@@ -1,53 +0,0 @@
1
- GIT
2
- remote: https://github.com/catprintlabs/opal.git
3
- revision: 3682db6f2b23a584787a68d5338a7ee9cb35565a
4
- specs:
5
- opal (0.7.1)
6
- hike (~> 1.2)
7
- sourcemap (~> 0.1.0)
8
- sprockets (>= 2.2.3, < 4.0.0)
9
- tilt (~> 1.4)
10
-
11
- PATH
12
- remote: .
13
- specs:
14
- reactive-ruby (0.7.3)
15
- opal
16
- opal-activesupport
17
-
18
- GEM
19
- remote: https://rubygems.org/
20
- specs:
21
- hike (1.2.3)
22
- opal-activesupport (0.1.0)
23
- opal (>= 0.5.0, < 1.0.0)
24
- opal-jquery (0.4.0)
25
- opal (>= 0.7.0, < 0.9.0)
26
- opal-rspec (0.4.3)
27
- opal (>= 0.7.0, < 0.9)
28
- rack (1.6.4)
29
- rack-protection (1.5.3)
30
- rack
31
- react-source (0.13.3)
32
- sinatra (1.4.6)
33
- rack (~> 1.4)
34
- rack-protection (~> 1.4)
35
- tilt (>= 1.3, < 3)
36
- sourcemap (0.1.1)
37
- sprockets (3.3.2)
38
- rack (~> 1.0)
39
- tilt (1.4.1)
40
-
41
- PLATFORMS
42
- ruby
43
-
44
- DEPENDENCIES
45
- opal!
46
- opal-jquery
47
- opal-rspec
48
- react-source (~> 0.12)
49
- reactive-ruby!
50
- sinatra
51
-
52
- BUNDLED WITH
53
- 1.10.6