Class: React::Router::DSL::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/react/router/dsl/route.rb,
lib/react/router/dsl/route/hooks.rb,
lib/react/router/dsl/route/wrappers.rb

Direct Known Subclasses

Index

Instance Method Summary collapse

Constructor Details

#initialize(*args, &children) ⇒ Route

Returns a new instance of Route.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/react/router/dsl/route.rb', line 8

def initialize(*args, &children)
  path =
    if args[0].is_a? Hash
      nil
    else
      args[0]
    end
  opts =
    if args[0].is_a? Hash
      args[0]
    else
      args[1] || {}
    end
  unless opts.is_a? Hash
    raise 'Route expects an optional path followed by an options hash, '\
          "instead we got route(#{'"' + path + '", ' if path} #{opts})"
  end
  @children, @index = DSL.evaluate_children do
    yield if children && children.arity == 0
    Index.new(mounts: opts[:index]) if opts[:index]
  end
  opts.delete(:index)
  @get_children = children if children && children.arity > 0
  @path = path
  if opts[:mounts].is_a? Hash
    @components = opts[:mounts]
  else
    @component = opts[:mounts]
  end
  opts.delete(:mounts)
  @opts = opts
  save_element
end

Instance Method Details

#get_child_routes_wrapperObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/react/router/dsl/route/wrappers.rb', line 5

def get_child_routes_wrapper
  lambda do |location, callBack|
    children, index, promise =
      React::Router::DSL.evaluate_children(TransitionContext.new(location: location),
                                           &@get_children)
    if promise.class < Promise || promise.is_a?(Promise)

      promise.then do |children|
        callBack.call(nil.to_n, React::Router::DSL.children_to_n(children))
      end.fail { |err_object| callBack.call(err_object, nil.to_n) }
    else
      callBack.call(nil.to_n, DSL.children_to_n(children))
    end
  end
end

#get_component_wrapperObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/react/router/dsl/route/wrappers.rb', line 43

def get_component_wrapper
  lambda do |nextState, callBack|
    comp = @component.call(TransitionContext.new(next_state: nextState))
    if comp.class < Promise || comp.is_a?(Promise)
      comp.then do |component|
        component = React::API.create_native_react_class(component)
        `callBack(null, component)`
      end.fail { |err_object| `callBack(#{err_object}, null)` }
    else
      comp = React::API.create_native_react_class(comp)
      `callBack(null, comp)`
    end
  end.to_n
end

#get_components_wrapperObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/react/router/dsl/route/wrappers.rb', line 20

def get_components_wrapper
  lambda do |nextState, callBack|
    result_hash = {}
    promises = []
    @components.each do |name, proc_or_comp|
      if proc_or_comp.respond_to? :call
        comp = proc.call(TransitionContext.new(next_state: nextState))
        if comp.class < Promise || comp.is_a?(Promise)
          promises << comp
          comp.then do |component|
            result_hash[name] = React::API.create_native_react_class(component)
          end.fail { |err_object| `callBack(#{err_object}, null)` }
        else
          result_hash[name] = React::API.create_native_react_class(comp)
        end
      else
        result_hash[name] = React::API.create_native_react_class(proc_or_comp)
      end
    end
    Promise.when(*promises).then { `callBack(null, #{result_hash.to_n})` }
  end.to_n
end

#get_index_route_wrapperObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/react/router/dsl/route/wrappers.rb', line 90

def get_index_route_wrapper
  lambda do |location, callBack|
    comp = @opts[:index].call(TransitionContext.new(location: location))
    if comp.class < Promise || comp.is_a?(Promise)
      comp.then { |component| `callBack(null, {component: #{component}})` }
          .fail { |err_object| `callBack(#{err_object}, null)` }
    else
      `callBack(null, {component: #{comp}})`
    end
  end.to_n
end

#mounts(name = nil, &block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/react/router/dsl/route/hooks.rb', line 10

def mounts(name = nil, &block)
  if name
    @components ||= {}
    @components[name] = block
  else
    @component = block
  end
end

#on(hook, &block) ⇒ Object



5
6
7
8
# File 'lib/react/router/dsl/route/hooks.rb', line 5

def on(hook, &block)
  @opts["on_#{hook}"] = block
  self
end

#on_change_wrapper(proc) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/react/router/dsl/route/wrappers.rb', line 71

def on_change_wrapper(proc)
  lambda do |prevState, nextState, replace, callBack|
    comp = @opts[:on_change].call(TransitionContext.new(prev_state: prevState,
                                                        next_state: nextState,
                                                        replace: replace))
    if comp.class < Promise || comp.is_a?(Promise)
      comp.then { `callBack()` }
    else
      `callBack()`
    end
  end.to_n
end

#on_enter_wrapperObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/react/router/dsl/route/wrappers.rb', line 58

def on_enter_wrapper
  lambda do |nextState, replace, callBack|
    comp =
      @opts[:on_enter].call(TransitionContext.new(next_state: nextState,
                                                  replace: replace))
    if comp.class < Promise || comp.is_a?(Promise)
      comp.then { `callBack()` }
    else
      `callBack()`
    end
  end.to_n
end

#on_leave_wrapper(proc) ⇒ Object



84
85
86
87
88
# File 'lib/react/router/dsl/route/wrappers.rb', line 84

def on_leave_wrapper(proc)
  lambda do
    @opts[:on_leave].call(TransitionContext.new)
  end.to_n
end

#save_elementObject



42
43
44
# File 'lib/react/router/dsl/route.rb', line 42

def save_element
  DSL.add_element(self)
end

#to_jsonObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/react/router/dsl/route.rb', line 46

def to_json
  hash = {}
  hash[:path] = @path if @path

  if @get_children
    hash[:getChildRoutes] = get_child_routes_wrapper
  else
    hash[:childRoutes] = @children.map(&:to_json)
  end

  if @components
    if @components.detect { |_k, v| v.respond_to? :call }
      hash[:getComponents] = get_components_wrapper
    else
      hash[:components] = @components
    end
  elsif @component.respond_to? :call
    hash[:getComponent] = get_component_wrapper
  elsif @component
    hash[:component] = React::API.create_native_react_class(@component)
  else
    hash[:component] = DSL.router.lookup_component(@path)
  end

  %w(enter change leave).each do |hook|
    hash["on#{hook.camelcase}"] = send("on_#{hook}_wrapper") if @opts["on_#{hook}"]
  end

  if @index.respond_to? :call
    hash[:getIndexRoute] = get_index_route_wrapper
  elsif @index
    hash[:indexRoute] = @index.to_json
  end

  @opts.each do |key, value|
    hash[key] = value unless %w(on_enter on_change on_leave).include?(key)
  end

  hash
end