module Behaveable::RouteExtractor

Public Instance Methods

extract(behaveable = nil, resource = nil) click to toggle source

Generate url location.

Parameters

  • behaveable - Behaveable object.

  • resource - Resource object. (member routes).

Returns

  • Route - Url location.

# File lib/behaveable/route_extractor.rb, line 12
def extract(behaveable = nil, resource = nil)
  resource_name   = resource_name_from(params)
  behaveable_name = behaveable_name_from(behaveable)

  location_url = "api_#{resource_name}_url"
  return regular(location_url, resource) unless behaveable

  location_url = "api_#{behaveable_name}_#{resource_name}_url"
  nested(location_url, behaveable, resource)
end

Private Instance Methods

behaveable_name_from(behaveable) click to toggle source

Get behaveable class name.

Parameters

Returns

  • String - Behaveable class snake case name or nil.

# File lib/behaveable/route_extractor.rb, line 73
def behaveable_name_from(behaveable)
  return unless behaveable

  behaveable.class.name.underscore
end
nested(location_url, behaveable, resource) click to toggle source

Handle nested url location.

Parameters

  • location_url - Url route as string.

  • behaveable - Behaveable object.

  • resource - Resource object.

Returns

  • Route - Url location.

# File lib/behaveable/route_extractor.rb, line 48
def nested(location_url, behaveable, resource)
  return send(location_url, behaveable) unless resource

  send(location_url, behaveable, resource)
end
regular(location_url, resource) click to toggle source

Handle non-nested url location.

Parameters

  • location_url - Url route as string.

  • resource - Resource object.

Returns

  • Route - Url location.

# File lib/behaveable/route_extractor.rb, line 33
def regular(location_url, resource)
  return send(location_url) unless resource

  send(location_url, resource)
end
resource_name_from(params) click to toggle source

Get resource name from params.

Parameters

  • params - ApplicationController's params.

Returns

  • String - Resource name (singular or plural).

# File lib/behaveable/route_extractor.rb, line 61
def resource_name_from(params)
  inflection = params[:id].present? ? 'singular' : 'plural'
  params[:controller].split('/').last.send("#{inflection}ize")
end