class Hanami::Routing::Resource::Action

Action for RESTful resource

@since 0.1.0

@api private

@see Hanami::Router#resource

Constants

NESTED_ROUTES_SEPARATOR

Nested routes separator

@api private @since 0.4.0

Public Class Methods

generate(router, action, options = {}, resource = nil) click to toggle source

Generate an action for the given router

@param router [Hanami::Router] @param action [Hanami::Routing::Resource::Action] @param options [Hash] @param resource [Hanami::Routing::Resource, Hanami::Routing::Resources]

@api private

@since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 56
def self.generate(router, action, options = {}, resource = nil)
  class_for(action).new(router, options, resource)
end
new(router, options = {}, resource = nil, &blk) click to toggle source

Initialize an action

@param router [Hanami::Router] @param options [Hash] @param resource [Hanami::Routing::Resource, Hanami::Routing::Resources] @param blk [Proc]

@api private

@since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 70
def initialize(router, options = {}, resource = nil, &blk)
  @router = router
  @options = options
  @resource = resource
  generate(&blk)
end

Private Class Methods

class_for(action) click to toggle source

Load a subclass, according to the given action name

@param action [String] the action name

@example

Hanami::Routing::Resource::Action.send(:class_for, 'New') # =>
  Hanami::Routing::Resource::New

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 127
def self.class_for(action)
  Utils::Class.load!(Utils::String.classify(action), namespace)
end

Public Instance Methods

generate(&blk) click to toggle source

Generate an action for the given router

@param blk [Proc]

@api private

@since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 84
def generate(&blk)
  @router.send verb, path, to: endpoint, as: as
  instance_eval(&blk) if block_given?
end
namespace() click to toggle source

Namespace

@api private @since 0.2.0

# File lib/hanami/routing/resource/action.rb, line 112
def namespace
  @namespace ||= Utils::PathPrefix.new @options[:namespace]
end
resource_name() click to toggle source

Resource name

@return [String]

@api private @since 0.1.0

@example

require 'hanami/router'

Hanami::Router.new do
  resource 'identity'
end

# 'identity' is the name passed in the @options
# File lib/hanami/routing/resource/action.rb, line 104
def resource_name
  @resource_name ||= @options[:name].to_s
end

Private Instance Methods

_nested_rest_path() click to toggle source

Create nested rest path

@api private @since 0.4.0

# File lib/hanami/routing/resource/action.rb, line 271
def _nested_rest_path
  Nested.new(resource_name, @resource).to_path
end
_singularized_as() click to toggle source

Singularize as (helper route)

@api private @since 0.4.0

# File lib/hanami/routing/resource/action.rb, line 260
def _singularized_as
  name = @options[:as] ? @options[:as].to_s : resource_name
  name.split(NESTED_ROUTES_SEPARATOR).map do |n|
    Hanami::Utils::String.singularize(n)
  end
end
action_name() click to toggle source

The name of the RESTful action.

@example

'index'
'new'
'create'

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 207
def action_name
  Utils::String.transform(self.class.name, :demodulize, :downcase)
end
as() click to toggle source

The namespaced name of the action within the whole context of the router.

@example

require 'hanami/router'

Hanami::Router.new do
  resources 'flowers'

  namespace 'animals' do
    resources 'mammals'
  end
end

# It will generate named routes like :flowers, :new_flowers ..
# It will generate named routes like :animals_mammals, :animals_new_mammals ..

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 194
def as
  namespace.relative_join(_singularized_as, self.class.named_route_separator).to_sym
end
controller_name() click to toggle source

Resource controller name

@example

Hanami::Router.new do
  resources 'flowers', controller: 'rocks'
end

# It will mount path 'flowers/new' to Rocks::New instead of Flowers::New
# Same for other action names

@api private @since 0.4.0

# File lib/hanami/routing/resource/action.rb, line 250
def controller_name
  @options[:controller] || resource_name
end
endpoint() click to toggle source

A string that represents the endpoint to be loaded. It is composed by controller and action name.

@see Hanami::Routing::Resource::Action#separator

@example

'flowers#index'

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 221
def endpoint
  [ controller_name, action_name ].join separator
end
path() click to toggle source

The namespaced URL relative path

@example

require 'hanami/router'

Hanami::Router.new do
  resources 'flowers'

  namespace 'animals' do
    resources 'mammals'
  end
end

# It will generate paths like '/flowers', '/flowers/:id' ..
# It will generate paths like '/animals/mammals', '/animals/mammals/:id' ..

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 159
def path
  rest_path
end
rest_path() click to toggle source

The URL relative path

@example

'/flowers'
'/flowers/new'
'/flowers/:id'

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 172
def rest_path
  namespace.join(_nested_rest_path || resource_name.to_s)
end
separator() click to toggle source

Separator between controller and action name

@see Hanami::Routing::EndpointResolver#separator

@example

'#' # default

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 234
def separator
  @options[:separator]
end
verb() click to toggle source

Accepted HTTP verb

@see Hanami::Routing::Resource::Action.verb

@api private @since 0.1.0

# File lib/hanami/routing/resource/action.rb, line 137
def verb
  self.class.verb
end