Class: RSpec::Authorization::Adapters::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/authorization/adapters/route.rb

Overview

Generate route that defines RESTful method using provided action. It is used primarily in creating a new request and infered automatically to an array using the defined #to_a method.

Route generation has 3 primary part, which are:

  • verb

  • action

  • params

The first part, verb refers to RESTful method, which is described in DICTIONARIES, action refers to controller action name, and params refers to parameters used in a request. This object can be automatically inferred as an array.

Creating a route will generate a route object:

route = Route.new(:index)
route # => #<Route:...>

This will infer the object as an array:

route = Route.new(:show)
send *route # => [:get, :show, { id: 1 }]

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ Route

Initializing a route requires a RESTful action name that the route want to generate. The action that get passed is assigned to action attributes and is publicly accessible.

route = Route.new(:index)
route.action # => :index

Currently Route only support RESTful action, passing non-RESTful action name is possible, but will result in unexpected result.

Parameters:

  • action (Symbol)

    RESTful action name

See Also:



44
45
46
# File 'lib/rspec/authorization/adapters/route.rb', line 44

def initialize(action)
  @action = action
end

Instance Attribute Details

#actionSymbol (readonly)

Returns route action name

Returns:

  • (Symbol)

    returns route action name



30
31
32
# File 'lib/rspec/authorization/adapters/route.rb', line 30

def action
  @action
end

Instance Method Details

#paramsHash?

This method is used to retrieve a dummy params that’s used for an action. Tipically an action for a resource member requires a param, while action for resource collection doesn’t need any param.

This will return a dummy params:

route = Route.new(:show)
route.params # => { id: 1 }

This will return nil for a collection resource action:

route = Route.new(:index)
route.params # => nil

Returns:

  • (Hash, nil)

    a dummy hash params



63
64
65
# File 'lib/rspec/authorization/adapters/route.rb', line 63

def params
  PARAMS[action]
end

#to_aArray

This method is used to convert Route object into an array. This method also used to automatically infer Route as an array on method that uses to_a, therefore we don’t need to manually invoke #to_a on every call that requires an array object.

This will return an array:

route = Route.new(:show)
route.to_a # => [:get, :show, { id: 1 }]

This will also return an array inferred automatically

send *route # => [:get, :show, { id: 1 }]

Returns:

  • (Array)

    an array of verb, action and params



99
100
101
# File 'lib/rspec/authorization/adapters/route.rb', line 99

def to_a
  [verb, action, params]
end

#verbSymbol

Return verb used for the RESTful action. The verb uses DICTIONARIES to retrieve a valid method for an action. This method intentionally throws error if a non-RESTful action is used.

route = Route.new(:index)
route.verb # => :get

This will throw error:

route = Route.new(:an_action)
route.verb # => KeyError: key not found: :an_action

Returns:

  • (Symbol)

    RESTful verb to be used for an action



80
81
82
# File 'lib/rspec/authorization/adapters/route.rb', line 80

def verb
  DICTIONARIES.fetch(action)
end