Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit e214732

Browse files
committed
closes #93, cleans up some duplicate code problems
1 parent 207012f commit e214732

8 files changed

Lines changed: 34 additions & 31 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
reactive-ruby (0.7.31)
4+
reactive-ruby (0.7.33)
55
opal
66
opal-activesupport (>= 0.2.0)
77
opal-browser

lib/react/component.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def self.included(base)
1616
base.include(React::Callbacks)
1717
base.instance_eval do
1818

19-
class PropsWrapper
19+
class PropsWrapper < BasicObject
2020

2121
def initialize(props_hash)
2222
@props_hash = props_hash
@@ -102,11 +102,7 @@ def method_missing(n, *args, &block)
102102
unless name && name.method_defined?(:render)
103103
return super
104104
end
105-
if node_only
106-
React::RenderingContext.build { React::RenderingContext.render(name, *args, &block) }.to_n
107-
else
108-
React::RenderingContext.render(name, *args, &block)
109-
end
105+
React::RenderingContext.build_or_render(node_only, name, *args, &block)
110106
end
111107

112108
end
@@ -264,11 +260,7 @@ def method_missing(n, *args, &block)
264260
name = "p"
265261
end
266262

267-
if node_only
268-
React::RenderingContext.build { React::RenderingContext.render(name, *args, &block) }.to_n
269-
else
270-
React::RenderingContext.render(name, *args, &block)
271-
end
263+
React::RenderingContext.build_or_render(node_only, name, *args, &block)
272264
end
273265

274266
def watch(value, &on_change)

lib/react/native_library.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ def self.method_missing(n, *args, &block)
3434
unless name = const_get(name)
3535
return super
3636
end
37-
if node_only
38-
React::RenderingContext.build { React::RenderingContext.render(name, *args, &block) }.to_n
39-
else
40-
React::RenderingContext.render(name, *args, &block)
41-
end
37+
React::RenderingContext.build_or_render(node_only, name, *args, &block)
4238
rescue
4339
end
4440

lib/react/rendering_context.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ class << self
44
attr_accessor :waiting_on_resources
55
end
66

7+
def self.build_or_render(node_only, name, *args, &block)
8+
if node_only
9+
React::RenderingContext.build { React::RenderingContext.render(name, *args, &block) }.to_n
10+
else
11+
React::RenderingContext.render(name, *args, &block)
12+
end
13+
end
14+
715
def self.render(name, *args, &block)
816
remove_nodes_from_args(args)
917
@buffer = [] unless @buffer

lib/react/state.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module React
2-
class StateWrapper
2+
3+
class StateWrapper < BasicObject
34

45
def initialize(native, from)
56
@state_hash = Hash.new(`#{native}.state`)
@@ -130,17 +131,13 @@ def states
130131
@states ||= Hash.new { |h, k| h[k] = {} }
131132
end
132133

133-
def new_observers
134-
@new_observers ||= Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } }
135-
end
136-
137-
def current_observers
138-
@current_observers ||= Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } }
134+
[:new_observers, :current_observers, :observers_by_name].each do |method_name|
135+
define_method(method_name) do
136+
instance_variable_get("@#{method_name}") or
137+
instance_variable_set("@#{method_name}", Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } })
138+
end
139139
end
140140

141-
def observers_by_name
142-
@observers_by_name ||= Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = [] } }
143-
end
144141
end
145142
end
146143
end

lib/reactive-ruby/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module React
2-
VERSION = "0.7.32"
2+
VERSION = "0.7.33"
33
end

reactive-ruby.gemspec

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ Gem::Specification.new do |s|
99

1010
s.author = 'David Chang'
1111
s.email = 'zeta11235813@gmail.com'
12-
s.homepage = 'https://github.com/zetachang/react.rb'
12+
s.homepage = 'https://reactive-ruby.github.io'
1313
s.summary = 'Opal Ruby wrapper of React.js library.'
1414
s.license = 'MIT'
15-
s.description = "Write reactive UI component with Ruby's elegancy and compiled to run in Javascript."
16-
15+
s.description = "Write React UI components in pure Ruby."
1716
s.files = `git ls-files`.split("\n")
1817
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
1918
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")

spec/react/component_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ def render
123123
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>bar</div>')
124124
end
125125

126+
it 'allows kernal method names like "format" to be used as state variable names' do
127+
Foo.class_eval do
128+
before_mount do
129+
state.format! 'yes'
130+
state.foo! state.format
131+
end
132+
end
133+
134+
expect(React.render_to_static_markup(React.create_element(Foo))).to eq('<div>yes</div>')
135+
end
136+
126137
it 'returns an observer with the bang method and no arguments' do
127138
Foo.class_eval do
128139
before_mount do

0 commit comments

Comments
 (0)