class Coach::Router

Constants

ACTION_TRAITS

Public Class Methods

new(mapper) click to toggle source
# File lib/coach/router.rb, line 16
def initialize(mapper)
  @mapper = mapper
end

Public Instance Methods

draw(namespace, base: nil, as: nil, constraints: nil, actions: []) click to toggle source
# File lib/coach/router.rb, line 20
def draw(namespace, base: nil, as: nil, constraints: nil, actions: [])
  action_traits(actions).each do |action, traits|
    handler = build_handler(namespace, action)
    match(action_url(base, traits),
          to: handler,
          via: traits[:method],
          as: as,
          constraints: constraints)
  end
end
match(url, **args) click to toggle source
# File lib/coach/router.rb, line 31
def match(url, **args)
  @mapper.match(url, args)
end

Private Instance Methods

action_traits(list_of_actions) click to toggle source

Receives an array of symbols that represent actions for which the default traits should be used, and then lastly an optional trait configuration.

Example is…

[ :index, :show, { refund: { url: ':id/refund', method: 'post' } } ]

…which will load the default route for ‘show` and `index`, while also configuring a refund route.

# File lib/coach/router.rb, line 60
def action_traits(list_of_actions)
  *list_of_actions, traits = list_of_actions if list_of_actions.last.is_a?(Hash)

  list_of_actions.reduce(traits || {}) do |memo, action|
    trait = ACTION_TRAITS.fetch(action) do
      raise Errors::RouterUnknownDefaultAction, action
    end

    memo.merge(action => trait)
  end
end
action_url(base, traits) click to toggle source

Applies trait url to base, removing duplicate /‘s

# File lib/coach/router.rb, line 73
def action_url(base, traits)
  [base, traits[:url]].compact.join("/").squeeze("/")
end
build_handler(namespace, action) click to toggle source
# File lib/coach/router.rb, line 37
def build_handler(namespace, action)
  action_name = camel(action)

  if namespace.is_a?(String)
    route_name = "#{namespace}::#{action_name}"
    Handler.new(route_name)
  else
    # Passing false to const_get prevents it searching ancestors until a
    # match is found. Without this, globally defined constants such as
    # `Process` will be picked up before consts that need to be autoloaded.
    Handler.new(namespace.const_get(action_name, false))
  end
end
camel(snake_case) click to toggle source

Turns a snake_case string/symbol into a CamelCase

# File lib/coach/router.rb, line 78
def camel(snake_case)
  snake_case.to_s.capitalize.gsub(/_./) { |match| match[1].upcase }
end