RubySmart::Namespace
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'
namespace = Admin::UsersController.namespace
namespace.scope
namespace.resource
namespace.concept
require 'namespace'
My::Membership::Operation::Index.namespace.info
require 'namespace'
MyApplication::Commands::ImportUsers rescue nil
mod = Namespace.build("MyApplication::Commands::ImportUsers")
mod.namespace.components
gem requirement & compatibility-mode
Initialize the namespace by a simple require:
require 'namespace'
Kernel.namespace
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'
Kernel.namespace
RubySmart::Namespace.build("MyApplication::Commands::ImportUsers")
Namespace Usage
components
returns all components as array
My::Membership::Operation::Index.namespace.components
Admin::UsersController.namespace.components
modules
returns all modules as array
My::Membership::Operation::Index.namespace.modules
Admin::UsersController.namespace.modules
sections
returns all sections as array
My::Membership::Operation::Index.namespace.sections
Admin::UsersController.namespace.sections
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
Admin::UsersController.namespace.scope
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
Admin::UsersController.namespace.concept
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
Admin::UsersController.namespace.resource
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
Admin::UsersController.namespace.service
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::Membership::Operation::Index.namespace.section(2)
Admin::UsersController.namespace.section(1)
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
My::Membership::Operation::Index.namespace.handle
Admin::UsersController.namespace.handle
info
Prints a info string for each namespace method.
just for debugging
My::Membership::Operation::Index.namespace.info
Admin::UsersController.namespace.info
Namespace module
resolve
resolves a object by provided names
require 'namespace'
::Namespace.resolve(:users,'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')
build
builds & resolves a new module by provided module names.
Only builds, if not exists previously!
::Namespace.build(:user,'cell','index')
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__])
::Namespace.transform(Admin::UsersController, [:__scope__, :home_controller])
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.