ruby_smart-namespace 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 460615817b5ac936c125be023dbfb390d044b76beb4848d6695bd8fb8c7d7a0f
4
+ data.tar.gz: 7338b44058f4732cfd400f6c7d8fd101a200547a6d422e775bcba8837952fa7d
5
+ SHA512:
6
+ metadata.gz: bb03b7e08c38cad64981a5db041f415da28103b7879d0c7afa3213dc0704568e0753bf1dc4bb9b45ac6166a5c5e2fdef4c01930e79d900531159935be852931d
7
+ data.tar.gz: 5a3586ee7a611270bf9f67bd72f1e6ca4b49c243f3197188638fcc35e18c3e0c71f3fd4713e9cda2ef8a8d3aa85d637b1da4ab87c0f5209758cf6dce58c9fb5f
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ruby_smart-support.gemspec
6
+ gemspec
7
+
data/README.md ADDED
@@ -0,0 +1,332 @@
1
+ # RubySmart::Namespace
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ruby_smart-namespace.svg)](https://badge.fury.io/rb/ruby_smart-namespace)
4
+ [![Test](https://github.com/ruby-smart/namespace/actions/workflows/ruby.yml/badge.svg)](https://github.com/ruby-smart/namespace/actions/workflows/ruby.yml)
5
+ [![RubySmart::Support Build Status](https://shields.io/github/workflow/status/ruby-smart/namespace/Test)](https://github.com/ruby-smart/namespace/actions)
6
+
7
+ Unified namespace for Ruby applications
8
+
9
+ _RubySmart::Namespace is a simple Ruby extension to provide generic namespace methods for each Object.
10
+ This simplifies the handling of loading & accessing other classes._
11
+
12
+ -----
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'ruby_smart-namespace'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle install
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install ruby_smart-namespace
29
+
30
+ ## Features
31
+ * Unified namespace methods (like: components, modules, sections) to simplify the use of modules & sections.
32
+ * Additional methods to `transform`, `build` or re-`path` a object
33
+
34
+ ## Examples
35
+
36
+ ```ruby
37
+ require 'namespace'
38
+
39
+ # build new namespace instance
40
+ namespace = Admin::UsersController.namespace
41
+
42
+ namespace.scope
43
+ # > :admin
44
+
45
+ namespace.resource
46
+ # > :user
47
+
48
+ namespace.concept
49
+ # > :controller
50
+ ```
51
+
52
+ ```ruby
53
+ require 'namespace'
54
+
55
+ # print info about the namespace
56
+ My::Membership::Operation::Index.namespace.info
57
+ # > -----------------------------------------------------------------------------------------------
58
+ # > => My::Membership::Operation::Index <=
59
+ # > components: [My, My::Membership, My::Membership::Operation, My::Membership::Operation::Index]
60
+ # > modules : ["My", "Membership", "Operation", "Index"]
61
+ # > sections : [:my, :membership, :operation, :index]
62
+ # > scope : my
63
+ # > concept :
64
+ # > resource : membership
65
+ # > service : operation
66
+ # > handle : index
67
+ # > -----------------------------------------------------------------------------------------------
68
+ ```
69
+
70
+ ```ruby
71
+ require 'namespace'
72
+
73
+ # the following object does NOT exist
74
+ MyApplication::Commands::ImportUsers rescue nil
75
+ #> nil
76
+
77
+ # create new module from namespace
78
+ mod = Namespace.build("MyApplication::Commands::ImportUsers")
79
+ # > MyApplication::Commands::ImportUsers
80
+
81
+ mod.namespace.components
82
+ # > [MyApplication, MyApplication::Commands, MyApplication::Commands::ImportUser]
83
+ ```
84
+
85
+ ## gem requirement & compatibility-mode
86
+
87
+ Initialize the namespace by a simple require:
88
+
89
+ ```ruby
90
+ require 'namespace'
91
+
92
+ # access any object's namespace through #namespace
93
+ # e.g.
94
+ Kernel.namespace
95
+
96
+ # access resolve, path, transform & build methods through RubySmart::Namespace::Base
97
+ # e.g.
98
+ RubySmart::Namespace::Base.resolve(:a,:b,:c)
99
+ ```
100
+
101
+ If `Namespace` conflicts with other gems, then you can simply require & use it without the 'core' inflection:
102
+
103
+ ```ruby
104
+ require 'ruby_smart/namespace'
105
+
106
+ # access any object's namespace through #namespace
107
+ # e.g.
108
+ Kernel.namespace
109
+
110
+ # create new module from namespace (this time through RubySmart module)
111
+ RubySmart::Namespace.build("MyApplication::Commands::ImportUsers")
112
+ ```
113
+
114
+ -----
115
+
116
+ ## Namespace Usage
117
+
118
+ ### components
119
+ returns all components as array
120
+
121
+ ```ruby
122
+ My::Membership::Operation::Index.namespace.components
123
+ # > [My, My::Membership, My::Membership::Operation, My::Membership::Operation::Index]
124
+
125
+ Admin::UsersController.namespace.components
126
+ # > [Admin, Admin::UsersController]
127
+ ```
128
+
129
+ ### modules
130
+ returns all modules as array
131
+
132
+ ```ruby
133
+ My::Membership::Operation::Index.namespace.modules
134
+ # > ["My", "Membership", "Operation", "Index"]
135
+
136
+ Admin::UsersController.namespace.modules
137
+ # > ["Admin", "UsersController"]
138
+ ```
139
+
140
+ ### sections
141
+ returns all sections as array
142
+
143
+ ```ruby
144
+ My::Membership::Operation::Index.namespace.sections
145
+ # > [:my, :membership, :operation, :index]
146
+
147
+ Admin::UsersController.namespace.sections
148
+ # > [:admin, :users_controller]
149
+ ```
150
+
151
+ ### scope
152
+ returns the scope of a provided klass.
153
+
154
+ _PLEASE NOTE:_ There is no scope for a class with a single module
155
+
156
+ ```ruby
157
+ My::Membership::Operation::Index.namespace.scope
158
+ # > :my
159
+
160
+ Admin::UsersController.namespace.scope
161
+ # > :admin
162
+ ```
163
+
164
+ ### concept
165
+ Returns the concept name of a provided klass.
166
+ It detects the first camel-case module and returns its concept name.
167
+
168
+ ```ruby
169
+ My::Membership::Operation::Index.namespace.concept
170
+ # > nil
171
+
172
+ Admin::UsersController.namespace.concept
173
+ # > :controller
174
+ ```
175
+
176
+ ### resource
177
+ Returns the resource name of a provided klass.
178
+ It checks for at least three modules and returns the first module name.
179
+ 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).
180
+ As last fallback it uses the first module.
181
+
182
+ ```ruby
183
+ My::Membership::Operation::Index.namespace.resource
184
+ # > :membership
185
+
186
+ Admin::UsersController.namespace.resource
187
+ # > :user
188
+ ```
189
+
190
+ ### service
191
+ Returns the service name of a provided klass.
192
+ It checks for at least three modules and returns the penultimate service.
193
+
194
+ ```ruby
195
+ My::Membership::Operation::Index.namespace.service
196
+ # > :operation
197
+
198
+ Admin::UsersController.namespace.service
199
+ # > nil
200
+ ```
201
+
202
+ ### section(pos = 0)
203
+ Returns the (first) section name of a provided klass (by default).
204
+ If a _pos_ was provided it'll return the _pos_ section.
205
+
206
+ ```ruby
207
+ My::Membership::Operation::Index.namespace.section
208
+ # > :my
209
+
210
+ My::Membership::Operation::Index.namespace.section(2)
211
+ # > :operation
212
+
213
+ Admin::UsersController.namespace.section(1)
214
+ # > :users_controller
215
+ ```
216
+
217
+ ### handle
218
+ Returns the handle name of a provided klass.
219
+ It checks for at least three modules and returns the last module name.
220
+
221
+ ```ruby
222
+ My::Membership::Operation::Index.namespace.handle
223
+ # > :index
224
+
225
+ My::Membership::Operation::Index.namespace.handle
226
+ # > :index
227
+
228
+ Admin::UsersController.namespace.handle
229
+ # > nil
230
+ ```
231
+
232
+ ### info
233
+ Prints a info string for each namespace method.
234
+ just for debugging
235
+
236
+ ```ruby
237
+ My::Membership::Operation::Index.namespace.info
238
+ # -----------------------------------------------------------------------------------------------
239
+ # => My::Membership::Operation::Index <=
240
+ # components -> [My, My::Membership, My::Membership::Operation, My::Membership::Operation::Index]
241
+ # modules -> ["My", "Membership", "Operation", "Index"]
242
+ # sections -> [:my, :membership, :operation, :index]
243
+ # scope -> my
244
+ # concept ->
245
+ # resource -> membership
246
+ # service -> operation
247
+ # handle -> index
248
+ # -----------------------------------------------------------------------------------------------
249
+
250
+ Admin::UsersController.namespace.info
251
+ # -----------------------------------------------------------------------------------------------
252
+ # => Admin::UsersController <=
253
+ # components -> [Admin, Admin::UsersController]
254
+ # modules -> ["Admin", "UsersController"]
255
+ # sections -> [:admin, :users_controller]
256
+ # scope -> admin
257
+ # concept -> controller
258
+ # resource -> user
259
+ # service ->
260
+ # handle ->
261
+ # -----------------------------------------------------------------------------------------------
262
+ ```
263
+
264
+ ## Namespace module
265
+
266
+ ### resolve
267
+ resolves a object by provided names
268
+
269
+ ```ruby
270
+ require 'namespace'
271
+
272
+
273
+ ::Namespace.resolve(:users,'cell','indEx')
274
+ # > ::User::Cell::Index
275
+ ```
276
+
277
+ ### path
278
+ returns the full object name as string.
279
+ _Please note:_ it's always a ```classify``` version of each name
280
+
281
+ ```ruby
282
+ ::Namespace.path(:user, 'Models',:open_tags, 'find')
283
+ # > "User::Model::OpenTag::Find"
284
+ ```
285
+
286
+ ### build
287
+ builds & resolves a new module by provided module names.
288
+ Only builds, if not exists previously!
289
+
290
+ ```ruby
291
+ ::Namespace.build(:user,'cell','index')
292
+ # > ::User::Cell::Index
293
+ # > ::User::Cell::Index.class == Module
294
+ ```
295
+
296
+ ### transform
297
+ converts a provided module to a totally new one.
298
+
299
+ Shorts can be used, to access the namespace methods:
300
+ * :\_\_scope\_\_
301
+ * :\_\_concept\_\_
302
+ * :\_\_resource\_\_
303
+ * :\_\_section\_\_
304
+ * :\_\_service\_\_
305
+ * :\_\_handle\_\_
306
+
307
+ ```ruby
308
+ ::Namespace.transform(User::Cell::Index, [:__resource__, :endpoint, :__handle__])
309
+ # > User::Endpoint::Index
310
+
311
+ ::Namespace.transform(Admin::UsersController, [:__scope__, :home_controller])
312
+ # > Admin::HomeController
313
+ ```
314
+
315
+ ## Docs
316
+
317
+ [CHANGELOG](./docs/CHANGELOG.md)
318
+
319
+ ## Contributing
320
+
321
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/ruby-smart/namespace).
322
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](./docs/CODE_OF_CONDUCT.md).
323
+
324
+ ## License
325
+
326
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
327
+
328
+ A copy of the [LICENSE](./docs/LICENSE.txt) can be found @ the docs.
329
+
330
+ ## Code of Conduct
331
+
332
+ Everyone interacting in the project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [CODE OF CONDUCT](./docs/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ruby_smart/namespace"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
data/docs/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # RubySmart::Namespace - CHANGELOG
2
+
3
+ ## [1.0.0] - 2022-11-12
4
+ * **[ref]** move Namespace from GTC to RubySmart
5
+ * **[ref]** `Namespace.transform` options to also use a double-underscore as suffix (e.g. \_\_scope\_\_)
6
+ * **[rem]** inflections
7
+
8
+ ## [0.1.2] - 2022-07-14
9
+ * **[ref]** ```Object#namespace``` to provide self as 'klass'
10
+ * **[ref]** RubySmart::Namespace::Base#initialize to provide any object & auto-detect it's class
11
+ * **[ref]** docs
12
+ * **[ref]** gemspec
13
+ * **[fix]** specs
14
+
15
+ ## [0.1.1] - 2022-07-13
16
+ * **[add]** docs
17
+ * **[ref]** cleanup
18
+
19
+ ## [0.1.0] - 2022-07-13
20
+ * **[ini]** Initial commit
21
+ * **[ref]** move data from utils into uniq repo
22
+ * **[ref]** docs, version, structure
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tg@reimbursement.institute. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/docs/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ruby Smart
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/lib/namespace.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_smart/namespace'
4
+
5
+ # Alias to <tt>ruby_smart/namespace/base</tt>
6
+ Namespace = RubySmart::Namespace::Base
@@ -0,0 +1,414 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+
5
+ module RubySmart
6
+ module Namespace
7
+ class Base
8
+ # regexp to find a camel-cased module
9
+ MODULE_DETECT_REGEX = /^(?:[A-Z][a-z0-9]+){2,}$/
10
+
11
+ # regexp to clean a module from it's concept
12
+ # MyUsersController
13
+ # > MyUsers
14
+ RESOURCE_CLEAN_REGEX = /^((?:[A-Z][a-z0-9]+)+?)(?:[A-Z][a-z0-9]+)?$/
15
+
16
+ # regexp to clean a module from it's resource
17
+ # MyUsersController
18
+ # > Controller
19
+ CONCEPT_CLEAN_REGEX = /^(?:[A-Z][a-z0-9]+)+([A-Z][a-z0-9]+)$/
20
+
21
+ # stores the calling class as instance
22
+ attr_reader :klass
23
+
24
+ class << self
25
+ # resolves a object by provided names
26
+ #
27
+ # ::RubySmart::Namespace::Base.resolve(:user,'cell','index')
28
+ # > ::User::Cell::Index
29
+ #
30
+ # @param [Array<String, Symbol, Array>] args - array of separated namespace strings
31
+ # @return [Object] module
32
+ def resolve(*args)
33
+ path(*args).constantize
34
+ end
35
+
36
+ # returns the full object name as string.
37
+ #
38
+ # ::RubySmart::Namespace::Base.path(:user, 'Models',:open_tags, 'find')
39
+ # > "User::Model::OpenTag::Find"
40
+ #
41
+ # @param [Array<String, Symbol, Array>] args - array of separated namespace strings
42
+ # @return [String] path
43
+ def path(*args)
44
+ args.map(&:to_s).map(&:classify).join('::')
45
+ end
46
+
47
+ # builds & resolves a new module by provided module names
48
+ #
49
+ # ::RubySmart::Namespace::Base.build(:user,'cell','index')
50
+ # > ::User::Cell::Index
51
+ #
52
+ # @param [Array<String, Symbol, Array>] args - array of separated namespace strings
53
+ # @return [Object] module
54
+ def build(*args)
55
+ current = ''
56
+ path = path(*args)
57
+ path.split('::').each do |mod|
58
+ if current == '' && !Object.const_defined?(mod)
59
+ Object.const_set(mod, Module.new)
60
+ elsif current != '' && !current.constantize.const_defined?(mod)
61
+ current.constantize.const_set(mod, Module.new)
62
+ end
63
+ current = "#{current}::#{mod}"
64
+ end
65
+
66
+ resolve(path)
67
+ end
68
+
69
+ # converts a provided module to a totally new one
70
+ #
71
+ # ::RubySmart::Namespace::Base.transform(User::Cell::Index, [:__resource, :endpoint, :__handle])
72
+ # > User::Endpoint::Index
73
+ #
74
+ # ::RubySmart::Namespace::Base.transform(Admin::UsersController, [:__scope, :home_controller])
75
+ # > Admin::HomeController
76
+ #
77
+ # @param [Object] klass
78
+ # @param [Array] packs - packs used for packing (constant symbols can be used for namespace method invocation)
79
+ # @param [Boolean] resolve - resolve module or return path (default: true)
80
+ # @option packs [Symbol] :__scope__ - uses scope
81
+ # @option packs [Symbol] :__concept__ - uses concept
82
+ # @option packs [Symbol] :__resource__ - uses resource
83
+ # @option packs [Symbol] :__section__ - uses section
84
+ # @option packs [Symbol] :__service__ - uses service
85
+ # @option packs [Symbol] :__handle__ - uses handle
86
+ # @return [Object, String] module or path
87
+ def transform(klass, packs, resolve = true)
88
+ s = packs.map do |pack|
89
+ case pack
90
+ when :__scope__
91
+ scope(klass)
92
+ when :__concept__
93
+ concept(klass)
94
+ when :__resource__
95
+ resource(klass)
96
+ when :__section__
97
+ section(klass)
98
+ when :__service__
99
+ service(klass)
100
+ when :__handle__
101
+ handle(klass)
102
+ else
103
+ pack
104
+ end
105
+ end
106
+
107
+ resolve ? self.resolve(*s) : path(*s)
108
+ end
109
+
110
+ # returns all components as array
111
+ #
112
+ # components(User::Endpoint::Index)
113
+ # > [User, User::Endpoint, User::Endpoint::Index]
114
+ #
115
+ # @param [Object] klass
116
+ # @return [Array<Object>] modules
117
+ def components(klass)
118
+ ary = []
119
+ modules(klass).map do |mod|
120
+ ary << mod
121
+ ary.join('::').constantize
122
+ end
123
+ end
124
+
125
+ # returns all modules as array
126
+ #
127
+ # modules(User::Endpoint::Index)
128
+ # > ['User', 'Endpoint', 'Index']
129
+ #
130
+ # @param [Object] klass
131
+ # @return [Array<String>] modules
132
+ def modules(klass)
133
+ klass.to_s.split('::')
134
+ end
135
+
136
+ # returns all sections as array
137
+ #
138
+ # sections(User::Endpoint::Index)
139
+ # > [:user, :endpoint, :index]
140
+ #
141
+ # @param [Object] klass
142
+ # @return [Array<Symbol>] sections
143
+ def sections(klass)
144
+ modules(klass).map { |mod| mod.underscore.to_sym }
145
+ end
146
+
147
+ # returns the scope of a provided klass.
148
+ # PLEASE NOTE: There is no scope for a class with a single module
149
+ #
150
+ # scope(User::Endpoint::Index)
151
+ # > :user
152
+ #
153
+ # scope(User::Index)
154
+ # > :user
155
+ #
156
+ # scope(Admin::UsersController)
157
+ # > :admin
158
+ #
159
+ # scope(HomeController)
160
+ # > nil
161
+ #
162
+ # scope(Member)
163
+ # > nil
164
+ #
165
+ # @param [Object] klass
166
+ # @return [Symbol, nil] scope symbol
167
+ def scope(klass)
168
+ modules = self.modules(klass)
169
+ return nil if modules.length < 2
170
+ modules[0].underscore.to_sym
171
+ end
172
+
173
+ # returns the concept name of a provided klass.
174
+ # it detects the first camel-case module and returns its concept name (camelcase string, just the last one).
175
+ #
176
+ # it's pendant is accessible through the *.resource* method.
177
+ #
178
+ # concept(User::Endpoint::Index)
179
+ # > nil
180
+ #
181
+ # concept(User::GamesHelper)
182
+ # > :helper
183
+ #
184
+ # concept(Home::CategoriesInitializer::Users)
185
+ # > :initializer
186
+ #
187
+ # concept(Admin::UsersController)
188
+ # > :controller
189
+ #
190
+ # concept(MembersHelper::UserProvider)
191
+ # > :helper
192
+ #
193
+ # concept(Category)
194
+ # > nil
195
+ #
196
+ # @param [Object] klass
197
+ # @param [Regexp] match_regexp
198
+ # @return [Symbol, nil] concept symbol
199
+ def concept(klass, match_regexp = MODULE_DETECT_REGEX)
200
+ modules = self.modules(klass)
201
+ res = modules.detect { |m| m.match(match_regexp) }
202
+ return nil unless res
203
+ res.gsub(CONCEPT_CLEAN_REGEX, '\1').underscore.to_sym
204
+ end
205
+
206
+ # returns the resource name of a provided klass.
207
+ # If there is less or equal than three modules it detects the first camel-cased module and returns its resource name (all camelcase token, except the last one - then singularize).
208
+ # For more than three modules it'll return the
209
+ # As last fallback it uses the first module.
210
+ #
211
+ # resource(User::Endpoint::Index)
212
+ # > :user
213
+ #
214
+ # resource(User::GamesHelper)
215
+ # > :game
216
+ #
217
+ # resource(Home::CategoriesInitializer::Users)
218
+ # > :category
219
+ #
220
+ # resource(Admin::UsersController)
221
+ # > :user
222
+ #
223
+ # resource(MembersHelper)
224
+ # > :member
225
+ #
226
+ # resource(Category)
227
+ # > :category
228
+ #
229
+ # @param [Object] klass
230
+ # @param [Regexp] match_regexp
231
+ # @return [Symbol] resource symbol
232
+ def resource(klass, match_regexp = MODULE_DETECT_REGEX)
233
+ modules = self.modules(klass)
234
+
235
+ res = if modules.length <= 3
236
+ (modules.detect { |m| m.match(match_regexp) } || modules[0]).gsub(RESOURCE_CLEAN_REGEX, '\1')
237
+ else
238
+ modules[-3]
239
+ end
240
+
241
+ res.underscore.singularize.to_sym
242
+ end
243
+
244
+ # returns the service name of a provided klass.
245
+ # It checks for at least three modules and returns the penultimate module.
246
+ #
247
+ # service(User::Cell::Index)
248
+ # > :cell
249
+ #
250
+ # service(User::EndpointHandler::Index)
251
+ # > :endpoint_handler
252
+ #
253
+ # service(Member)
254
+ # > nil
255
+ #
256
+ # service(Admin::Home::Cell::Index)
257
+ # > :cell
258
+ #
259
+ # service(Admin::Home::Cell::Index, 1)
260
+ # > :home
261
+ #
262
+ # @param [Object] klass
263
+ # @return [Symbol, nil] service symbol
264
+ def service(klass)
265
+ modules = self.modules(klass)
266
+ return nil if modules.length < 3
267
+ modules[-2].underscore.to_sym
268
+ end
269
+
270
+ # returns the section name of a provided klass and position (default: 0)
271
+ #
272
+ # section(User::Cell::Index)
273
+ # > :user
274
+ #
275
+ # section(User::EndpointHandler::Index, 1)
276
+ # > :endpoint_handler
277
+ #
278
+ # section(Member)
279
+ # > :member
280
+ #
281
+ # section(Admin::Home::Cell::Index, -2)
282
+ # > :cell
283
+ #
284
+ # section(Admin::Home::Cell::Index, 1)
285
+ # > :home
286
+ #
287
+ # @param [Object] klass
288
+ # @param [Integer] pos - section position
289
+ # @return [Symbol, nil] section symbol
290
+ def section(klass, pos = 0)
291
+ sections(klass)[pos]
292
+ end
293
+
294
+ # returns the handle name of a provided klass.
295
+ # It checks for at least three modules and returns the last module name.
296
+ #
297
+ # handle(User::Endpoint::Index)
298
+ # > :index
299
+ #
300
+ # handle(Member)
301
+ # > nil
302
+ #
303
+ # @param [Object] klass
304
+ # @return [Symbol] handle symbol
305
+ def handle(klass)
306
+ modules = self.modules(klass)
307
+ return nil if modules.length < 3
308
+ modules[-1].underscore.to_sym
309
+ end
310
+
311
+ # prints a info string for each namespace method.
312
+ # just for debugging
313
+ # @param [Object] klass
314
+ def info(klass)
315
+ puts "-----------------------------------------------------------------------------------------------"
316
+ puts "=> #{klass} <="
317
+ %w(components modules sections scope concept resource service handle).each do |m|
318
+ puts "#{m.ljust(11)}-> #{send(m, klass)}"
319
+ end
320
+ puts "-----------------------------------------------------------------------------------------------"
321
+ end
322
+ end
323
+
324
+ def initialize(object)
325
+ klass = object.is_a?(Module) ? object : object.class
326
+ raise "Generating a namespace from a Namespace will break the universe!" if klass <= ::RubySmart::Namespace::Base
327
+
328
+ @klass = klass
329
+ end
330
+
331
+ # returns all components as array
332
+ # See ::RubySmart::Namespace::Base.components
333
+ def components
334
+ self.class.components(klass)
335
+ end
336
+
337
+ # returns all modules as array
338
+ # See ::RubySmart::Namespace::Base.modules
339
+ def modules
340
+ self.class.modules(klass)
341
+ end
342
+
343
+ # returns all sections as array
344
+ # See ::RubySmart::Namespace::Base.sections
345
+ def sections
346
+ self.class.sections(klass)
347
+ end
348
+
349
+ # returns the scope of a provided klass.
350
+ # PLEASE NOTE: There is no scope for a class with a single module
351
+ #
352
+ # See ::RubySmart::Namespace::Base.scope
353
+ def scope
354
+ self.class.scope(klass)
355
+ end
356
+
357
+ # returns the concept name of a provided klass.
358
+ # it detects the first camel-case module and returns its concept name (camelcase string, just the last one).
359
+ #
360
+ # See ::RubySmart::Namespace::Base.concept
361
+ def concept
362
+ self.class.concept(klass)
363
+ end
364
+
365
+ # returns the resource name of a provided klass.
366
+ # It checks for at least three modules and returns the first module name.
367
+ # 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).
368
+ # As last fallback it uses the first module.
369
+ #
370
+ # See ::RubySmart::Namespace::Base.resource
371
+ def resource
372
+ self.class.resource(klass)
373
+ end
374
+
375
+ # returns the service name of a provided klass.
376
+ # It checks for at least three modules and returns the penultimate service.
377
+ #
378
+ # See ::RubySmart::Namespace::Base.service
379
+ def service
380
+ self.class.service(klass)
381
+ end
382
+
383
+ # returns the section name of a provided klass.
384
+ #
385
+ # See ::RubySmart::Namespace::Base.section
386
+ def section(pos = 0)
387
+ self.class.section(klass, pos)
388
+ end
389
+
390
+ # returns the handle name of a provided klass.
391
+ # It checks for at least three modules and returns the last module name.
392
+ #
393
+ # See ::RubySmart::Namespace::Base.handle
394
+ def handle
395
+ self.class.handle(klass)
396
+ end
397
+
398
+ # converts a provided module to a totally new one
399
+ #
400
+ # See ::RubySmart::Namespace::Base.transform
401
+ def transform(packs, resolve = true)
402
+ self.class.transform(klass, packs, resolve)
403
+ end
404
+
405
+ # prints a info string for each namespace method.
406
+ # just for debugging
407
+ #
408
+ # See ::RubySmart::Namespace::Base.info
409
+ def info
410
+ self.class.info(klass)
411
+ end
412
+ end
413
+ end
414
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ruby_smart/namespace/base"
4
+
5
+ # patches the Object class to directly access the namespace
6
+ class Object
7
+ # returns a new namespace object which can be used to resolve namespace-related modules
8
+ # @return [::RubySmart::Namespace::Base] namespace
9
+ def namespace
10
+ ::RubySmart::Namespace::Base.new(self)
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubySmart
4
+ module Namespace
5
+ # Returns the version of the currently loaded module as a <tt>Gem::Version</tt>
6
+ def self.gem_version
7
+ Gem::Version.new VERSION::STRING
8
+ end
9
+
10
+ module VERSION
11
+ MAJOR = 1
12
+ MINOR = 0
13
+ TINY = 0
14
+ PRE = nil
15
+
16
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
17
+
18
+ def self.to_s
19
+ STRING
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'gem_version'
4
+
5
+ module RubySmart
6
+ module Namespace
7
+ # Returns the version of the currently loaded Gem as a <tt>Gem::Version</tt>
8
+ def self.version
9
+ gem_version
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "namespace/version"
4
+ require_relative "namespace/base"
5
+ require_relative "namespace/core_ext/ruby/object"
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_smart/namespace'
4
+ require 'namespace'
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_smart-namespace
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Gonsior
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.21'
69
+ description: |
70
+ RubySmart::Namespace is a simple Ruby extension to provide generic namespace methods for each Object.
71
+ This simplifies the handling of loading & accessing other classes.
72
+ email:
73
+ - info@ruby-smart.org
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".rspec"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - docs/CHANGELOG.md
85
+ - docs/CODE_OF_CONDUCT.md
86
+ - docs/LICENSE.txt
87
+ - lib/namespace.rb
88
+ - lib/ruby_smart-namespace.rb
89
+ - lib/ruby_smart/namespace.rb
90
+ - lib/ruby_smart/namespace/base.rb
91
+ - lib/ruby_smart/namespace/core_ext/ruby/object.rb
92
+ - lib/ruby_smart/namespace/gem_version.rb
93
+ - lib/ruby_smart/namespace/version.rb
94
+ homepage: https://github.com/ruby-smart/namespace
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/ruby-smart/namespace
99
+ source_code_uri: https://github.com/ruby-smart/namespace
100
+ changelog_uri: https://github.com/ruby-smart/namespace/blob/main/docs/CHANGELOG.md
101
+ allowed_push_host: https://rubygems.org
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 2.6.0
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.3.22
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Unified namespace for Ruby applications
121
+ test_files: []