Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ruby_smart-namespace

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ruby_smart-namespace

  • 1.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

RubySmart::Namespace

GitHub Documentation

Gem Version License

Coverage Status Tests

Unified namespace for Ruby applications

RubySmart::Namespace is a simple Ruby extension to provide generic namespace methods for each Object. This simplifies the handling of loading & accessing other classes.


Installation

Add this line to your application's Gemfile:

gem 'ruby_smart-namespace'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install ruby_smart-namespace

Features

  • Unified namespace methods (like: components, modules, sections) to simplify the use of modules & sections.
  • Additional methods to transform, build or re-path a object

Examples

require 'namespace'

# build new namespace instance
namespace = Admin::UsersController.namespace

namespace.scope
# > :admin

namespace.resource
# > :user

namespace.concept
# > :controller
require 'namespace'

# print info about the namespace
My::Membership::Operation::Index.namespace.info
# > -----------------------------------------------------------------------------------------------
# > => My::Membership::Operation::Index <=
# > components: [My, My::Membership, My::Membership::Operation, My::Membership::Operation::Index]
# > modules   : ["My", "Membership", "Operation", "Index"]
# > sections  : [:my, :membership, :operation, :index]
# > scope     : my
# > concept   :
# > resource  : membership
# > service   : operation
# > handle    : index
# > -----------------------------------------------------------------------------------------------
require 'namespace'

# the following object does NOT exist
MyApplication::Commands::ImportUsers rescue nil
#> nil

# create new module from namespace
mod = Namespace.build("MyApplication::Commands::ImportUsers")
# > MyApplication::Commands::ImportUsers

mod.namespace.components
# > [MyApplication, MyApplication::Commands, MyApplication::Commands::ImportUser]

gem requirement & compatibility-mode

Initialize the namespace by a simple require:

require 'namespace'

# access any object's namespace through #namespace
# e.g.
Kernel.namespace

# access resolve, path, transform & build methods through RubySmart::Namespace::Base
# e.g.
RubySmart::Namespace::Base.resolve(:a,:b,:c)

If Namespace conflicts with other gems, then you can simply require & use it without the 'core' inflection:

require 'ruby_smart/namespace'

# access any object's namespace through #namespace
# e.g.
Kernel.namespace

# create new module from namespace (this time through RubySmart module)
RubySmart::Namespace.build("MyApplication::Commands::ImportUsers")

Namespace Usage

components

returns all components as array

My::Membership::Operation::Index.namespace.components
# > [My, My::Membership, My::Membership::Operation, My::Membership::Operation::Index]

Admin::UsersController.namespace.components
# > [Admin, Admin::UsersController]

modules

returns all modules as array

My::Membership::Operation::Index.namespace.modules
# > ["My", "Membership", "Operation", "Index"]

Admin::UsersController.namespace.modules
# > ["Admin", "UsersController"]

sections

returns all sections as array

My::Membership::Operation::Index.namespace.sections
# > [:my, :membership, :operation, :index]

Admin::UsersController.namespace.sections
# > [:admin, :users_controller]

scope

returns the scope of a provided klass.

PLEASE NOTE: There is no scope for a class with a single module

My::Membership::Operation::Index.namespace.scope
# > :my

Admin::UsersController.namespace.scope
# > :admin

concept

Returns the concept name of a provided klass. It detects the first camel-case module and returns its concept name.

My::Membership::Operation::Index.namespace.concept
# > nil

Admin::UsersController.namespace.concept
# > :controller

resource

Returns the resource name of a provided klass. It checks for at least three modules and returns the first module name. If there is more or less than three modules it detects the first camel-cased module and returns its resource name (all camelcase token, except the last one - then singularize). As last fallback it uses the first module.

My::Membership::Operation::Index.namespace.resource
# > :membership

Admin::UsersController.namespace.resource
# > :user

service

Returns the service name of a provided klass. It checks for at least three modules and returns the penultimate service.

My::Membership::Operation::Index.namespace.service
# > :operation

Admin::UsersController.namespace.service
# > nil

section(pos = 0)

Returns the (first) section name of a provided klass (by default). If a pos was provided it'll return the pos section.

My::Membership::Operation::Index.namespace.section
# > :my

My::Membership::Operation::Index.namespace.section(2)
# > :operation

Admin::UsersController.namespace.section(1)
# > :users_controller

handle

Returns the handle name of a provided klass. It checks for at least three modules and returns the last module name.

My::Membership::Operation::Index.namespace.handle
# > :index

My::Membership::Operation::Index.namespace.handle
# > :index

Admin::UsersController.namespace.handle
# > nil

info

Prints a info string for each namespace method. just for debugging

My::Membership::Operation::Index.namespace.info
# -----------------------------------------------------------------------------------------------
# => My::Membership::Operation::Index <=
# components -> [My, My::Membership, My::Membership::Operation, My::Membership::Operation::Index]
# modules    -> ["My", "Membership", "Operation", "Index"]
# sections   -> [:my, :membership, :operation, :index]
# scope      -> my
# concept    ->
# resource   -> membership
# service    -> operation
# handle     -> index
# -----------------------------------------------------------------------------------------------

Admin::UsersController.namespace.info
# -----------------------------------------------------------------------------------------------
# => Admin::UsersController <=
# components -> [Admin, Admin::UsersController]
# modules    -> ["Admin", "UsersController"]
# sections   -> [:admin, :users_controller]
# scope      -> admin
# concept    -> controller
# resource   -> user
# service    ->
# handle     ->
#  -----------------------------------------------------------------------------------------------

Namespace module

resolve

resolves a object by provided names

require 'namespace'


::Namespace.resolve(:users,'cell','indEx')
# > ::User::Cell::Index

path

returns the full object name as string. Please note: it's always a classify version of each name

::Namespace.path(:user, 'Models',:open_tags, 'find')
# > "User::Model::OpenTag::Find"

build

builds & resolves a new module by provided module names. Only builds, if not exists previously!

::Namespace.build(:user,'cell','index')
# > ::User::Cell::Index
# > ::User::Cell::Index.class == Module

transform

converts a provided module to a totally new one.

Shorts can be used, to access the namespace methods:

  • :__scope__
  • :__concept__
  • :__resource__
  • :__section__
  • :__service__
  • :__handle__
::Namespace.transform(User::Cell::Index, [:__resource__, :endpoint, :__handle__])
# > User::Endpoint::Index

::Namespace.transform(Admin::UsersController, [:__scope__, :home_controller])
# > Admin::HomeController

Docs

CHANGELOG

Contributing

Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

A copy of the LICENSE can be found @ the docs.

Code of Conduct

Everyone interacting in the project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the CODE OF CONDUCT.

FAQs

Package last updated on 24 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc