class Drawers::ResourceParts

Attributes

class_path[R]
named_resource_type[R]
namespace[R]
resource_name[R]
resource_type[R]

Public Class Methods

call(name) click to toggle source
# File lib/drawers/resource_parts.rb, line 21
def call(name)
  resource = new(name)
  resource.call
  resource
end
from_name(name) click to toggle source
# File lib/drawers/resource_parts.rb, line 9
def from_name(name)
  resource = call(name)

  [
    resource.namespace,
    resource.resource_name,
    resource.resource_type,
    resource.named_resource_type,
    resource.class_path
  ]
end
new(name) click to toggle source
# File lib/drawers/resource_parts.rb, line 28
def initialize(name)
  @qualified_name = name
end

Public Instance Methods

call() click to toggle source
# File lib/drawers/resource_parts.rb, line 32
def call
  # if this is not part of a resource, don't even bother
  return unless index_of_resource_type

  # Api, V2, Post, Operations, Update
  # => Operations
  @resource_type = qualified_parts[index_of_resource_type]

  # Api, V2, Post, Operations, Update
  # => Posts
  #
  # Posts, Controller
  # => Posts
  original_resource_name = qualified_parts[index_of_resource_type - 1]
  @resource_name = original_resource_name.pluralize

  # Posts_Controller
  # Post_Operations
  @named_resource_type = "#{original_resource_name}_#{@resource_type}"

  # Api, V2, Post, Operations, Update
  # => Api, V2
  namespace_index = index_of_resource_type - 1
  @namespace = namespace_index < 1 ? '' : qualified_parts.take(namespace_index)

  # Api, V2, Post, Operations, Update
  # => Update
  class_index = index_of_resource_type + 1
  @class_path = class_index < 1 ? '' : qualified_parts.drop(class_index)
end

Private Instance Methods

index_of_resource_type() click to toggle source

based on the position of of the resource type name, anything to the left will be the namespace, and anything to the right will be the file path within the namespace (may be obvious, but basically, we're 'pivoting' on RESOURCE_SUFFIX_NAMES)

Given: Api, V2, Post, Operations, Update

^ index_of_resource_type (3)
# File lib/drawers/resource_parts.rb, line 92
def index_of_resource_type
  @index_of_resource_type ||= qualified_parts.index { |x| Drawers.resource_suffixes.include?(x) }
end
qualified_parts() click to toggle source
  1. break apart the qualified name into pieces that can easily be manipulated

Api::Posts

> Api, Posts

Api::PostOperations::Create

> Api, Post, Operations, Create

Api::PostsController

> Api, Posts, Controller

Api::V2::PostOperations::Update

> Api, V2, Post, Operations, Update

# File lib/drawers/resource_parts.rb, line 79
def qualified_parts
  @qualified_parts ||= @qualified_name
                       .split(Drawers.qualified_name_split)
                       .reject(&:blank?)
end