Skip to content

Commit 99711d1

Browse files
committed
completed access to params directly via instance vars
1 parent ef14fe5 commit 99711d1

File tree

180 files changed

+503
-2801
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+503
-2801
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ env:
2121
- COMPONENT=hyper-i18n LANGUAGE=ruby LANGUAGE_VERSION=2.5.1
2222
- COMPONENT=hyper-model LANGUAGE=ruby LANGUAGE_VERSION=2.5.1
2323
- COMPONENT=hyper-operation LANGUAGE=ruby LANGUAGE_VERSION=2.5.1
24-
- COMPONENT=hyper-react LANGUAGE=ruby LANGUAGE_VERSION=2.5.1
2524
- COMPONENT=hyper-router LANGUAGE=ruby LANGUAGE_VERSION=2.5.1
2625
- COMPONENT=hyper-spec LANGUAGE=ruby LANGUAGE_VERSION=2.5.1
2726
- COMPONENT=hyper-state LANGUAGE=ruby LANGUAGE_VERSION=2.5.1

ruby/examples/docs/home-page/app/hyperstack/components/native_example.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ class SelectDate < HyperComponent
2929
render(DIV) do
3030
# DatePicker is a JS Component imported with Webpack
3131
# Notice the lambda to pass a Ruby method as a callback
32-
DatePicker(selected: state.date,
32+
DatePicker(selected: @date,
3333
todayButton: "Today",
34-
onChange: ->(date) { mutate.date date }
34+
onChange: ->(date) { mutate @date = date }
3535
)
3636
# see how we use `` and #{} to b ridger JS and Ruby
37-
H3 { `moment(#{state.date}).format('LL')` }
37+
H3 { `moment(#{@date}).format('LL')` }
3838
# or if you prefer..
3939
# H3 { Native(`moment`).call(state.date).format('LL') }
4040
end

ruby/hyper-component/lib/hyperstack/internal/component/props_wrapper.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def param_definitions
2727

2828
def define_param(name, param_type, aka = nil)
2929
aka ||= name
30-
param_definitions[name] = ->(props) { @component.instance_variable_set :"@#{aka}", fetch_from_cache(name, param_type, props) } #[param_type, aka || name]
30+
param_definitions[name] = lambda do |props|
31+
@component.instance_variable_set :"@#{aka}", fetch_from_cache(name, param_type, props)
32+
end
3133
if param_type == Proc
3234
define_method(aka.to_sym) do |*args, &block|
3335
props[name].call(*args, &block) if props[name]
@@ -40,7 +42,9 @@ def define_param(name, param_type, aka = nil)
4042
end
4143

4244
def define_all_others(name)
43-
param_definitions[name] = -> (props) { puts "setting @#{name} props = #{props} self = #{self}"; @component.instance_variable_set :"@#{name}", yield(props) } #[:__hyperstack_component_all_others_flag, name, block]
45+
param_definitions[name] = lambda do |props|
46+
@component.instance_variable_set :"@#{name}", yield(props)
47+
end
4448
define_method(name.to_sym) do
4549
@_all_others_cache ||= yield(props)
4650
end
@@ -53,7 +57,7 @@ def param_accessor_style
5357

5458
def initialize(component, incoming = nil)
5559
@component = component
56-
return if self.class.param_accessor_style == :legacy
60+
return if param_accessor_style == :legacy
5761
self.class.param_definitions.each_value do |initializer|
5862
instance_exec(incoming || props, &initializer)
5963
end
@@ -73,12 +77,12 @@ def [](prop)
7377
def fetch_from_cache(name, param_type, props)
7478
last, cached_value = cache[name]
7579
return cached_value if last.equal?(props[name])
76-
convert_param(name, param_type).tap do |value|
80+
convert_param(name, param_type, props).tap do |value|
7781
cache[name] = [props[name], value]
7882
end
7983
end
8084

81-
def convert_param(name, param_type)
85+
def convert_param(name, param_type, props)
8286
if param_type.respond_to? :_react_param_conversion
8387
param_type._react_param_conversion props[name], nil
8488
elsif param_type.is_a?(Array) &&

ruby/hyper-component/spec/client_features/auto_unmount_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ def timer_went_off!
2323
when :cancel_and_wait
2424
"waiting..."
2525
when :final_result
26-
"works!" if !!Mounter.stayed_running == params.keep_running
26+
"works!" if !!Mounter.stayed_running == @keep_running
2727
else
28-
Mounted(keep_running: params.keep_running)
28+
Mounted(keep_running: @keep_running)
2929
end
3030
end
3131
end
3232
class Mounted < HyperComponent
3333
param :keep_running
3434
after_mount do
35-
if params.keep_running
35+
if @keep_running
3636
Mounted.after(2) { puts "timer is going off!"; Mounter.timer_went_off! }
3737
else
3838
after(2) { puts "timer is going off!"; Mounter.timer_went_off! }
@@ -84,7 +84,7 @@ class Mounter < HyperComponent
8484
class Mounted < HyperComponent
8585
param :observable_object
8686
before_mount do
87-
@observable_object = params.observable_object
87+
@observable_object = @observable_object
8888
end
8989
render do
9090
"Mounted!"

ruby/hyper-component/spec/client_features/component_spec.rb

Lines changed: 3 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -272,75 +272,19 @@ class Foo
272272
end
273273
end
274274

275-
it 'reads from parent passed properties through `params`' do
276-
mount 'Foo', prop: 'foobar' do
277-
Foo.class_eval do
278-
param :prop
279-
def render
280-
Hyperstack::Component::ReactAPI.create_element('div') { params[:prop] }
281-
end
282-
end
283-
end
284-
expect(page.body[-40..-19]).to include("<div>foobar</div>")
285-
end
286-
287275
it 'accesses nested params as orignal Ruby object' do
288276
mount 'Foo', prop: [{foo: 10}] do
289277
Foo.class_eval do
290278
param :prop
291279
def render
292-
Hyperstack::Component::ReactAPI.create_element('div') { params[:prop][0][:foo] }
280+
Hyperstack::Component::ReactAPI.create_element('div') { @prop[0][:foo] }
293281
end
294282
end
295283
end
296284
expect(page.body[-35..-19]).to include("<div>10</div>")
297285
end
298286
end
299287

300-
describe 'Props Updating', v13_only: true do
301-
before do
302-
on_client do
303-
class Foo
304-
include Hyperstack::Component
305-
end
306-
end
307-
end
308-
309-
it '`setProps` as method `set_props` is no longer supported' do
310-
expect_evaluate_ruby do
311-
Foo.class_eval do
312-
param :foo
313-
def render
314-
Hyperstack::Component::ReactAPI.create_element('div') { params[:foo] }
315-
end
316-
end
317-
instance = Hyperstack::Component::ReactTestUtils.render_component_into_document(Foo, foo: 10)
318-
begin
319-
instance.set_props(foo: 20)
320-
rescue
321-
'got risen'
322-
end
323-
end.to eq('got risen')
324-
end
325-
326-
it 'original `replaceProps` as method `set_props!` is no longer supported' do
327-
expect_evaluate_ruby do
328-
Foo.class_eval do
329-
param :foo
330-
def render
331-
Hyperstack::Component::ReactAPI.create_element('div') { params[:foo] ? 'exist' : 'null' }
332-
end
333-
end
334-
instance = Hyperstack::Component::ReactTestUtils.render_component_into_document(Foo, foo: 10)
335-
begin
336-
instance.set_props!(bar: 20)
337-
rescue
338-
'got risen'
339-
end
340-
end.to eq('got risen')
341-
end
342-
end
343-
344288
describe 'Prop validation' do
345289
before do
346290
on_client do
@@ -409,7 +353,7 @@ class Foo
409353
end
410354

411355
def render
412-
DIV { params[:foo] + '-' + params[:bar]}
356+
DIV { @foo + '-' + @bar}
413357
end
414358
end
415359
end
@@ -477,82 +421,6 @@ def render; "hello".span; "goodby".span.delete; end
477421
end
478422
end
479423

480-
describe 'Event handling' do
481-
before do
482-
on_client do
483-
class Foo
484-
include Hyperstack::Component
485-
end
486-
end
487-
end
488-
489-
it 'works in render method' do
490-
491-
expect_evaluate_ruby do
492-
Foo.class_eval do
493-
494-
attr_reader :clicked
495-
496-
def render
497-
Hyperstack::Component::ReactAPI.create_element('div').on(:click) do
498-
@clicked = true
499-
end
500-
end
501-
end
502-
instance = Hyperstack::Component::ReactTestUtils.render_component_into_document(Foo)
503-
Hyperstack::Component::ReactTestUtils.simulate_click(instance)
504-
instance.clicked
505-
end.to eq(true)
506-
end
507-
508-
it 'invokes handler on `this.props` using emit' do
509-
on_client do
510-
Foo.class_eval do
511-
param :on_foo_fubmit, type: Proc
512-
after_mount :setup
513-
514-
def setup
515-
self.emit(:foo_submit, 'bar')
516-
end
517-
518-
def render
519-
Hyperstack::Component::ReactAPI.create_element('div')
520-
end
521-
end
522-
end
523-
evaluate_ruby do
524-
element = Hyperstack::Component::ReactAPI.create_element(Foo).on(:foo_submit) { 'bar' }
525-
Hyperstack::Component::ReactTestUtils.render_into_document(element)
526-
end
527-
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
528-
.to_not match(/Exception raised/)
529-
end
530-
531-
it 'invokes handler with multiple params using emit' do
532-
on_client do
533-
Foo.class_eval do
534-
param :on_foo_invoked, type: Proc
535-
after_mount :setup
536-
537-
def setup
538-
self.emit(:foo_invoked, [1,2,3], 'bar')
539-
end
540-
541-
def render
542-
Hyperstack::Component::ReactAPI.create_element('div')
543-
end
544-
end
545-
end
546-
547-
evaluate_ruby do
548-
element = Hyperstack::Component::ReactAPI.create_element(Foo).on(:foo_invoked) { return [1,2,3], 'bar' }
549-
Hyperstack::Component::ReactTestUtils.render_into_document(element)
550-
end
551-
expect(page.driver.browser.manage.logs.get(:browser).map { |m| m.message.gsub(/\\n/, "\n") }.to_a.join("\n"))
552-
.to_not match(/Exception raised/)
553-
end
554-
end
555-
556424
describe '#render' do
557425
it 'supports element building helpers' do
558426
on_client do
@@ -561,7 +429,7 @@ class Foo
561429
param :foo
562430
def render
563431
DIV do
564-
SPAN { params[:foo] }
432+
SPAN { @foo }
565433
end
566434
end
567435
end

ruby/hyper-component/spec/client_features/dsl_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Foo
3232
on_client do
3333
class Bar < HyperComponent
3434
param :p1
35-
render { "hello #{params.p1}" }
35+
render { "hello #{@p1}" }
3636
end
3737
class Foo < HyperComponent
3838
render Bar, p1: "fred"
@@ -287,8 +287,8 @@ class X2
287287
param :ele
288288
def render
289289
DIV do
290-
params[:ele].render
291-
params[:ele].render
290+
@ele.render
291+
@ele.render
292292
end
293293
end
294294
end

ruby/hyper-component/spec/client_features/element_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def render
3737
it 'will subscribe to a component event param' do
3838
evaluate_ruby do
3939
class Foo < HyperComponent
40-
param :on_event, type: Proc, default: nil, allow_nil: true
40+
raises :event
4141
def render
42-
params.on_event
42+
event!
4343
end
4444
end
4545
Hyperstack::Component::ReactTestUtils.render_into_document(Hyperstack::Component::ReactAPI.create_element(Foo).on(:event) {'works!'})
@@ -50,10 +50,10 @@ def render
5050
it 'will subscribe to multiple component event params' do
5151
evaluate_ruby do
5252
class Foo < HyperComponent
53-
param :on_event1, type: Proc, default: nil, allow_nil: true
54-
param :on_event2, type: Proc, default: nil, allow_nil: true
53+
raises :event1
54+
raises :event2
5555
def render
56-
params.on_event1+params.on_event2
56+
event1! + event2!
5757
end
5858
end
5959
Hyperstack::Component::ReactTestUtils.render_into_document(Hyperstack::Component::ReactAPI.create_element(Foo).on(:event1, :event2) {'works!'})
@@ -87,9 +87,9 @@ class Foo < HyperComponent
8787

8888
evaluate_ruby do
8989
class Foo < HyperComponent
90-
param :my_event, type: Proc, default: nil, allow_nil: true
90+
raises '<my_event>', alias: :my_event!
9191
def render
92-
params.my_event
92+
my_event!
9393
end
9494
end
9595
Hyperstack::Component::ReactTestUtils.render_into_document(Hyperstack::Component::ReactAPI.create_element(Foo).on("<my_event>") {'works!'})

ruby/hyper-component/spec/client_features/native_library_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ module NativeLibraryTestModule
77
class Component < HyperComponent
88
param :time_stamp
99
backtrace :none
10-
render { NativeComponent(name: "There - #{params.time_stamp}") }
10+
render { NativeComponent(name: "There - #{@time_stamp}") }
1111
end
1212

1313
class NestedComponent < HyperComponent
1414
param :time_stamp
1515
backtrace :none
16-
render { NativeLibrary::NativeNestedLibrary::NativeComponent(name: "There - #{params.time_stamp}") }
16+
render { NativeLibrary::NativeNestedLibrary::NativeComponent(name: "There - #{@time_stamp}") }
1717
end
1818
end
1919
end

ruby/hyper-component/spec/client_features/opal_jquery_extensions_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Foo < HyperComponent
88
param :name
99
def render
10-
"hello #{params.name}"
10+
"hello #{@name}"
1111
end
1212
end
1313
test_div = Element.new(:div)
@@ -33,7 +33,7 @@ class Foo < HyperComponent
3333
end
3434

3535
def render
36-
"hello #{params.name} render-count: #{@render_count += 1}"
36+
"hello #{@name} render-count: #{@render_count += 1}"
3737
end
3838

3939
def self.rec_cnt
@@ -55,7 +55,7 @@ def self.rec_cnt
5555
class Foo < HyperComponent
5656
param :name
5757
def render
58-
"hello #{params.name}"
58+
"hello #{@name}"
5959
end
6060
end
6161
test_div = Element.new(:div)

0 commit comments

Comments
 (0)