Skip to content

Commit ef14fe5

Browse files
committed
converting specs to new style param access
1 parent 74245ae commit ef14fe5

File tree

8 files changed

+76
-351
lines changed

8 files changed

+76
-351
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def component_will_receive_props(next_props)
8383
def component_will_update(next_props, next_state)
8484
observing { run_callback(:before_update, next_props, next_state) }
8585
if @__hyperstack_component_receiving_props
86-
params._reset_all_others_cache
8786
@__hyperstack_component_params_wrapper.reload(next_props)
8887
end
8988
@__hyperstack_component_receiving_props = false

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def hyper_component?
1212
true
1313
end
1414

15+
def param_accessor_style(*args)
16+
props_wrapper.param_accessor_style(*args)
17+
end
18+
1519
def backtrace(*args)
1620
@__hyperstack_component_dont_catch_exceptions = (args[0] == :none)
1721
@__hyperstack_component_backtrace_off = @__hyperstack_component_dont_catch_exceptions || (args[0] == :off)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ def children
99
end
1010

1111
def params
12+
if @__hyperstack_component_params_wrapper.param_accessor_style == :hyperstack
13+
debugger
14+
raise "params are now directly accessible via instance variables.\n"\
15+
' to access the legacy behavior add `param_accessor_style = :legacy` '\
16+
"to your component class\n"\
17+
' to access both behaviors add `param_accessor_style = :both` '\
18+
'to your component class'
19+
end
1220
@__hyperstack_component_params_wrapper
1321
end
1422

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

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,70 @@ module Component
44
class PropsWrapper
55
attr_reader :component
66

7-
def self.param_definitions
8-
@param_definitions ||=
9-
if superclass.respond_to? :param_definitions
10-
superclass.param_definitions.dup
7+
class << self
8+
9+
def param_accessor_style(*args)
10+
@param_accessor_style = args[0] if args.length > 0
11+
@param_accessor_style ||=
12+
if superclass.respond_to? :param_accessor_style
13+
superclass.param_accessor_style
14+
else
15+
:hyperstack
16+
end
17+
end
18+
19+
def param_definitions
20+
@param_definitions ||=
21+
if superclass.respond_to? :param_definitions
22+
superclass.param_definitions.dup
23+
else
24+
Hash.new
25+
end
26+
end
27+
28+
def define_param(name, param_type, aka = nil)
29+
aka ||= name
30+
param_definitions[name] = ->(props) { @component.instance_variable_set :"@#{aka}", fetch_from_cache(name, param_type, props) } #[param_type, aka || name]
31+
if param_type == Proc
32+
define_method(aka.to_sym) do |*args, &block|
33+
props[name].call(*args, &block) if props[name]
34+
end
1135
else
12-
Hash.new
36+
define_method(aka.to_sym) do
37+
fetch_from_cache(name, param_type, props)
38+
end
1339
end
14-
end
40+
end
1541

16-
def self.define_param(name, param_type, aka = nil)
17-
aka ||= name
18-
param_definitions[name] = ->(props) { @component.instance_variable_set :"@#{aka}", fetch_from_cache(name, param_type, props) } #[param_type, aka || name]
19-
if param_type == Proc
20-
define_method(aka.to_sym) do |*args, &block|
21-
props[name].call(*args, &block) if props[name]
22-
end
23-
else
24-
define_method(aka.to_sym) do
25-
fetch_from_cache(name, param_type, props)
42+
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]
44+
define_method(name.to_sym) do
45+
@_all_others_cache ||= yield(props)
2646
end
2747
end
2848
end
2949

30-
def self.define_all_others(name)
31-
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]
32-
define_method(name.to_sym) do
33-
puts "calling good ole params.#{name} props = #{props} self = #{self}"
34-
@_all_others_cache ||= yield(props)
35-
end
50+
def param_accessor_style
51+
self.class.param_accessor_style
3652
end
3753

3854
def initialize(component, incoming = nil)
3955
@component = component
56+
return if self.class.param_accessor_style == :legacy
4057
self.class.param_definitions.each_value do |initializer|
4158
instance_exec(incoming || props, &initializer)
4259
end
4360
end
4461

4562
def reload(next_props)
63+
@_all_others_cache = nil # needed for legacy params wrapper
4664
initialize(@component, next_props)
4765
end
4866

4967
def [](prop)
5068
props[prop]
5169
end
5270

53-
def _reset_all_others_cache
54-
@_all_others_cache = nil
55-
end
56-
5771
private
5872

5973
def fetch_from_cache(name, param_type, props)

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def render
2323
def top_level_render
2424
paths_searched = []
2525
component = nil
26-
if params.component_name.start_with?('::')
26+
if @component_name.start_with?('::')
2727
# if absolute path of component is given, look it up and fail if not found
28-
paths_searched << params.component_name
28+
paths_searched << @component_name
2929
component = begin
30-
Object.const_get(params.component_name)
30+
Object.const_get(@component_name)
3131
rescue NameError
3232
nil
3333
end
@@ -40,28 +40,28 @@ def top_level_render
4040
# ::Foo::Bar will only resolve to some component named ::Foo::Bar
4141
# but Foo::Bar will check (in this order) ::Home::Foo::Bar, ::Components::Home::Foo::Bar, ::Foo::Bar, ::Components::Foo::Bar
4242
self.class.search_path.each do |scope|
43-
paths_searched << "#{scope.name}::#{params.controller}::#{params.component_name}"
43+
paths_searched << "#{scope.name}::#{@controller}::#{@component_name}"
4444
component = begin
45-
scope.const_get(params.controller, false).const_get(params.component_name, false)
45+
scope.const_get(@controller, false).const_get(@component_name, false)
4646
rescue NameError
4747
nil
4848
end
4949
break if component != nil
5050
end
5151
unless component
5252
self.class.search_path.each do |scope|
53-
paths_searched << "#{scope.name}::#{params.component_name}"
53+
paths_searched << "#{scope.name}::#{@component_name}"
5454
component = begin
55-
scope.const_get(params.component_name, false)
55+
scope.const_get(@component_name, false)
5656
rescue NameError
5757
nil
5858
end
5959
break if component != nil
6060
end
6161
end
6262
end
63-
return RenderingContext.render(component, params.render_params) if component && component.method_defined?(:render)
64-
raise "Could not find component class '#{params.component_name}' for params.controller '#{params.controller}' in any component directory. Tried [#{paths_searched.join(", ")}]"
63+
return RenderingContext.render(component, @render_params) if component && component.method_defined?(:render)
64+
raise "Could not find component class '#{@component_name}' for @controller '#{@controller}' in any component directory. Tried [#{paths_searched.join(", ")}]"
6565
end
6666
end
6767
end

0 commit comments

Comments
 (0)