class RailsModuleUnification::ResourceParts

Constants

QUALIFIED_NAME_SPLIT
RESOURCE_SUFFIX_NAMES

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/rails_module_unification/resource_parts.rb, line 24
def call(name)
  resource = new(name)
  resource.call
  resource
end
from_name(name) click to toggle source
# File lib/rails_module_unification/resource_parts.rb, line 12
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/rails_module_unification/resource_parts.rb, line 31
def initialize(name)
  @qualified_name = name
end

Public Instance Methods

call() click to toggle source
# File lib/rails_module_unification/resource_parts.rb, line 35
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/rails_module_unification/resource_parts.rb, line 93
def index_of_resource_type
  @index_of_resource_type ||= qualified_parts.index { |x| RESOURCE_SUFFIX_NAMES.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/rails_module_unification/resource_parts.rb, line 82
def qualified_parts
  @qualified_parts ||= @qualified_name.split(QUALIFIED_NAME_SPLIT).reject(&:blank?)
end