Class: Pakyow::Routing::Expansion Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pakyow/routing/expansion.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Expands a route template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_name, controller, options, &template_block) ⇒ Expansion

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Expansion.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
86
87
88
89
90
91
92
93
# File 'lib/pakyow/routing/expansion.rb', line 17

def initialize(template_name, controller, options, &template_block)
  @controller = controller

  # Create the controller that stores available routes, groups, and namespaces.
  #
  @expander = Controller.make(set_const: false)

  # Evaluate the template to define available routes, groups, and namespaces.
  #
  instance_exec(**options, &template_block)

  # Define helper methods for routes
  #
  local_expander = @expander
  @expander.routes.each do |method, routes|
    routes.each do |route|
      unless @controller.singleton_class.instance_methods(false).include?(route.name)
        @controller.define_singleton_method route.name do |*args, &block|
          # Handle template parts named `new` by determining if we're calling `new` to expand
          # part of a template, or if we're intending to create a new controller instance.
          #
          # If args are empty we can be sure that we're creating a route.
          #
          if args.any?
            super(*args)
          else
            build_route(method, route.name, route.path || route.matcher, &block).tap do
              # Make sure the route was inserted in the same order as found in the template.
              #
              index_of_last_insert = local_expander.routes[method].index { |expander_route|
                expander_route.name == @routes[method].last.name
              }

              insert_before_this_index = @routes[method].select { |each_route|
                local_expander.routes[method].any? { |expander_route|
                  each_route.name == expander_route.name
                }
              }.map { |each_route|
                local_expander.routes[method].index { |expander_route|
                  expander_route.name == each_route.name
                }
              }.select { |index|
                index > index_of_last_insert
              }.first

              if insert_before_this_index
                @routes[method].insert(
                  @routes[method].index { |each_route|
                    each_route.name == local_expander.routes[method][insert_before_this_index].name
                  }, @routes[method].delete_at(index_of_last_insert)
                )
              end
            end
          end
        end
      end
    end
  end

  # Define helper methods for groups and namespaces
  #
  @expander.children.each do |child|
    unless @controller.singleton_class.instance_methods(false).include?(child.__object_name.name)
      @controller.define_singleton_method child.__object_name.name do |&block|
        if child.path.nil?
          group(child.__object_name.name, &block)
        else
          namespace(child.__object_name.name, child.path || child.matcher, &block)
        end
      end
    end
  end

  # Set the expansion on the controller.
  #
  @controller.expansions << template_name
end

Instance Attribute Details

#controllerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/pakyow/routing/expansion.rb', line 11

def controller
  @controller
end

#expanderObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/pakyow/routing/expansion.rb', line 11

def expander
  @expander
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/pakyow/routing/expansion.rb', line 11

def name
  @name
end

Instance Method Details

#group(*args, **kwargs, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/pakyow/routing/expansion.rb', line 95

def group(*args, **kwargs, &block)
  @expander.send(:group, *args, set_const: false, **kwargs, &block)
end

#namespace(*args, **kwargs, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
# File 'lib/pakyow/routing/expansion.rb', line 99

def namespace(*args, **kwargs, &block)
  @expander.send(:namespace, *args, set_const: false, **kwargs, &block)
end