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:
“by 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,buildor re-patha object
Examples
“by require ‘namespace’
build new namespace instance
namespace = Admin::UsersController.namespace
namespace.scope
> :admin
namespace.resource
> :user
namespace.concept
> :controller
“
“by 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
> ———————————————————————————————–
“
“by 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:
“by 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:
“by 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
“by 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
“by My::Membership::Operation::Index.namespace.modules
> [My, Membership, Operation, Index]
Admin::UsersController.namespace.modules
> [Admin, UsersController]
“
sections
returns all sections as array
“by 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
“by 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.
“by 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.
“by 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.
“by 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.
“by 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.
“by 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
“by 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
“by 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
- “by
- :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!
- “by
- :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__
- “by
- :Namespace.transform(User::Cell::Index, [:__resource__, :endpoint, :__handle__])
> User::Endpoint::Index
::Namespace.transform(Admin::UsersController, [:__scope__, :home_controller])
> Admin::HomeController
“
Docs
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.