Class: Cuprum::Rails::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/rails/routes.rb

Overview

Represent the routes available for a given resource.

Defined Under Namespace

Classes: MissingWildcardError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path:, parent_path: nil, &block) ⇒ Routes

Returns a new instance of Routes.

Parameters:

  • base_path (String)

    the relative path of the resource.

  • parent_path (String) (defaults to: nil)

    the path to the parent resource, if any.



80
81
82
83
84
85
86
# File 'lib/cuprum/rails/routes.rb', line 80

def initialize(base_path:, parent_path: nil, &block)
  @base_path   = base_path
  @parent_path = parent_path
  @wildcards   = {}

  singleton_class.instance_exec(&block) if block_given?
end

Instance Attribute Details

#base_pathString (readonly)

Returns the relative path of the resource.

Returns:

  • (String)

    the relative path of the resource.



89
90
91
# File 'lib/cuprum/rails/routes.rb', line 89

def base_path
  @base_path
end

#wildcardsString (readonly)

Returns the url wildcards for the resource.

Returns:

  • (String)

    the url wildcards for the resource.



92
93
94
# File 'lib/cuprum/rails/routes.rb', line 92

def wildcards
  @wildcards
end

Class Method Details

.route(action_name, path) ⇒ Object

Defines a route for the resource routes.

Each defined route creates a helper method on the routes, which returns the full path of the resource route. A member action helper (if the path ends in an :id wildcard) will require passing in either the primary key of the member or the member entity.

If the base path includes wildcards, then the wildcards must be set (using #with_wildcards) before calling a route helper.

Parameters:

  • action_name (String, Symbol)

    The name of the action.

  • path (String)

    The path of the action relative to the resource root. If the path has a leading slash, the path is treated as an absolute path and does not include the routes base path.



26
27
28
29
30
31
32
33
34
35
# File 'lib/cuprum/rails/routes.rb', line 26

def route(action_name, path)
  validate_action_name!(action_name)
  validate_path!(path)

  if member_route?(path)
    define_member_route(action_name, path)
  else
    define_collection_route(action_name, path)
  end
end

Instance Method Details

#parent_pathString

Returns the path to the parent resource index.

Returns:

  • (String)

    the path to the parent resource index.



95
96
97
98
99
# File 'lib/cuprum/rails/routes.rb', line 95

def parent_path
  return '/' if @parent_path.nil?

  insert_wildcards(@parent_path)
end

#root_pathString

Returns the root path for the application.

Returns:

  • (String)

    the root path for the application.



102
103
104
# File 'lib/cuprum/rails/routes.rb', line 102

def root_path
  '/'
end

#with_wildcards(wildcards) ⇒ Cuprum::Rails::Routes

Returns a copy of the routes with the wildcards.

Parameters:

  • wildcards (Hash)

    The wildcards to use with the routes.

Returns:



109
110
111
112
113
114
115
# File 'lib/cuprum/rails/routes.rb', line 109

def with_wildcards(wildcards)
  unless wildcards.is_a?(Hash)
    raise ArgumentError, 'wildcards must be a Hash'
  end

  clone.apply_wildcards(wildcards)
end