modspace 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/simple_example.rb +61 -0
- data/lib/modspace.rb +49 -0
- data/lib/modspace/version.rb +3 -0
- data/modspace.gemspec +29 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2930117c3d626d7606136b50133295902be4eb26cf4ea6377f8216964eec5140
|
4
|
+
data.tar.gz: 0d9db6db194eab6c1ae0d2b89b412f45fbdb7d63c7280b8b4cc9a1fbfe647f5f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3b409b9dfd31444b2e63fa3a545d3141b871407145fb159c3336dad316254aae4db1aed91682a853662a22f8fe7267f0c06da9c478708ff04ffe1e57d396814d
|
7
|
+
data.tar.gz: ae8fc8258b54aee5a422606a72e0189a6b88dde8195c7a9aa7fdd364f5e881f85113fe4a188b8183238e37c68b0d08af59b45e3aa254f31c95a67eeb301a99d7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
modspace (0.1.0)
|
5
|
+
byebug (~> 11.1.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
byebug (11.1.3)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
rake (12.3.3)
|
13
|
+
rspec (3.9.0)
|
14
|
+
rspec-core (~> 3.9.0)
|
15
|
+
rspec-expectations (~> 3.9.0)
|
16
|
+
rspec-mocks (~> 3.9.0)
|
17
|
+
rspec-core (3.9.2)
|
18
|
+
rspec-support (~> 3.9.3)
|
19
|
+
rspec-expectations (3.9.2)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.9.0)
|
22
|
+
rspec-mocks (3.9.1)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.9.0)
|
25
|
+
rspec-support (3.9.3)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
modspace!
|
32
|
+
rake (~> 12.0)
|
33
|
+
rspec (~> 3.0)
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Declan McGrath declan@weuseopensource.com
|
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/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Modspace
|
2
|
+
|
3
|
+
|
4
|
+
Modspace is a small library to allow the easy definition and management of namespaces and nested modules in Ruby.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'modspace'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install modspace
|
21
|
+
|
22
|
+
## The Problem
|
23
|
+
|
24
|
+
In Ruby, if you want to define nested modules you typically have to do some
|
25
|
+
extreme indentation in your source code files. For example, to define
|
26
|
+
a function - `some_funct()` - in a module, such as `Foo::Bar::Zoo::Shoe`,
|
27
|
+
would require this...
|
28
|
+
|
29
|
+
module Foo
|
30
|
+
module Bar
|
31
|
+
module Zoo
|
32
|
+
module Shoe
|
33
|
+
def some_funct
|
34
|
+
puts "Eek"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
This "nested plane of death" can be a massive pain. Not only does it
|
42
|
+
look awkward but, given many teams have coding standards with set line
|
43
|
+
length limits, it quickly eats up your available character count. Why
|
44
|
+
should a developer be penalised for nesting modules? (Unneccessary
|
45
|
+
nesting is not recommended of course!) One solution is to define the
|
46
|
+
parent namespace in advance of the implementation - and this is much
|
47
|
+
better...
|
48
|
+
|
49
|
+
# Define parent namespace
|
50
|
+
module Foo
|
51
|
+
module Bar
|
52
|
+
module Zoo
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Implementation
|
58
|
+
module Foo::Bar::Zoo::Shoe
|
59
|
+
def some_funct
|
60
|
+
puts "Eek"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
_Note: We use the term "namespace" to mean a nested module or nested class._
|
65
|
+
|
66
|
+
__The Modspace gem goes the extra mile and let's you clean up the
|
67
|
+
definition of the parent namespace (or any namespace for that matter) as
|
68
|
+
we will see in the next section.__
|
69
|
+
|
70
|
+
## Usage
|
71
|
+
|
72
|
+
Define a new namespace (ie. nested module), such as `Foo::Bar::Zoo` in one line...
|
73
|
+
|
74
|
+
require 'modspace'
|
75
|
+
|
76
|
+
# Define parent namespace
|
77
|
+
Modspace.def_mod('Foo', 'Bar', 'Zoo')
|
78
|
+
# Note: This creates the namespace Foo::Bar::Zoo
|
79
|
+
|
80
|
+
# Implementation
|
81
|
+
module Foo::Bar::Zoo::Shoe
|
82
|
+
def some_funct
|
83
|
+
puts "Eek"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
__Note: You only need to define the parent namespace. For example, we
|
88
|
+
omit "Shoe" from the initial namespace definition above.__
|
89
|
+
|
90
|
+
For more examples, see the `examples/` directory.
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/theirishpenguin/modspace.
|
95
|
+
|
96
|
+
|
97
|
+
## License
|
98
|
+
|
99
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "modspace"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# This line is only needed if you don't have the modspace gem installed (and
|
4
|
+
# you are in the root directory of modspace cloned repo
|
5
|
+
$:.unshift File.dirname('./lib/modspace')
|
6
|
+
|
7
|
+
require 'modspace'
|
8
|
+
|
9
|
+
# 1) Create a new namespace (ie. nested module) in one line...
|
10
|
+
Modspace.def_mod('Foo', 'Bar', 'Zoo1')
|
11
|
+
|
12
|
+
if Foo::Bar::Zoo1.is_a? Module
|
13
|
+
puts "1) We have defined a nested module Foo::Bar::Zoo1"
|
14
|
+
else
|
15
|
+
raise StandardError
|
16
|
+
end
|
17
|
+
|
18
|
+
# ^ Note: Defaults to defining the namespace within "Object", as that is the top-level namespace within Ruby programs
|
19
|
+
|
20
|
+
# 2) Create a new namespace (ie. nested module) and assign it to a variable...
|
21
|
+
zoo2_module = Modspace.def_mod('Foo', 'Bar', 'Zoo2')
|
22
|
+
|
23
|
+
if zoo2_module == Foo::Bar::Zoo2
|
24
|
+
puts "2) We have defined a nested module Foo::Bar::Zoo2 and assigned it to the variable zoo2_module"
|
25
|
+
else
|
26
|
+
raise StandardError
|
27
|
+
end
|
28
|
+
|
29
|
+
# 3) Create a new namespace (ie. nested module) within a class...
|
30
|
+
class SomeClass; end
|
31
|
+
|
32
|
+
zoo3_module = Modspace.def_mod('Foo', 'Bar', 'Zoo3', in_namespace: SomeClass)
|
33
|
+
|
34
|
+
if zoo3_module == SomeClass::Foo::Bar::Zoo3
|
35
|
+
puts "3) We have defined a nested module Foo::Bar::Zoo3 within an explicit class"
|
36
|
+
else
|
37
|
+
raise StandardError
|
38
|
+
end
|
39
|
+
|
40
|
+
# 4) Create a new namespace (ie. nested module) within a class...
|
41
|
+
|
42
|
+
an_instance_of_some_class = SomeClass.new
|
43
|
+
|
44
|
+
zoo4_module = Modspace.def_mod('Foo', 'Bar', 'Zoo4', in_class_of_instance: an_instance_of_some_class)
|
45
|
+
|
46
|
+
if zoo4_module == SomeClass::Foo::Bar::Zoo4
|
47
|
+
puts "4) We have defined a nested module Foo::Bar::Zoo4 within the class of a specified object"
|
48
|
+
else
|
49
|
+
raise StandardError
|
50
|
+
end
|
51
|
+
|
52
|
+
# 5) Create a new namespace (ie. nested module) within a module...
|
53
|
+
module SomeModule; end
|
54
|
+
|
55
|
+
zoo5_module = Modspace.def_mod('Foo', 'Bar', 'Zoo5', in_namespace: SomeModule)
|
56
|
+
|
57
|
+
if zoo5_module == SomeModule::Foo::Bar::Zoo5
|
58
|
+
puts "5) We have defined a nested module Foo::Bar::Zoo5 within an explicit module"
|
59
|
+
else
|
60
|
+
raise StandardError
|
61
|
+
end
|
data/lib/modspace.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require "modspace/version"
|
2
|
+
|
3
|
+
module Modspace
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# With thanks to https://stackoverflow.com/questions/37154774/ruby-define-modules-dynamically/37156102 :-)
|
8
|
+
|
9
|
+
# Note: We use the term "namespace" to mean "class" or "module". The term abstracts away the differences
|
10
|
+
# for our purposes.
|
11
|
+
|
12
|
+
def def_mod(*args, in_namespace: Object, in_class_of_instance: nil)
|
13
|
+
parent_namespace = if !in_class_of_instance.nil?
|
14
|
+
in_class_of_instance.class
|
15
|
+
elsif !in_namespace.nil? # :in_namespace can be a module or class
|
16
|
+
in_namespace
|
17
|
+
else
|
18
|
+
raise ArgumentError, "If in_class or for_instance non-nil is specified they must be non-nil."
|
19
|
+
end
|
20
|
+
|
21
|
+
defp_class_mod(parent_namespace, *args)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def defp_class_mod(parent_namespace, *args)
|
27
|
+
nesting_list = *args
|
28
|
+
|
29
|
+
return parent_namespace if nesting_list.empty?
|
30
|
+
|
31
|
+
desired_nested_module_name = nesting_list.shift # mutates nested list (but it's local-only)
|
32
|
+
|
33
|
+
# The desired nested module name may have already been defined elsewhere in the overall Ruby codebase.
|
34
|
+
# If this is the case, we just reuse that constant definition.
|
35
|
+
#
|
36
|
+
# Note 1: if we don't do this then we will get the warning: "already initialized constant Foo"
|
37
|
+
#
|
38
|
+
# Note 2: The false passed to const_defined?() and const_get() methods prevent Ruby from looking up the
|
39
|
+
# inheritance tree when searching for the desired nested module name.
|
40
|
+
desired_nested_module = if parent_namespace.const_defined?(desired_nested_module_name, false)
|
41
|
+
parent_namespace.const_get(desired_nested_module_name, false)
|
42
|
+
else
|
43
|
+
parent_namespace.const_set(desired_nested_module_name, Module.new)
|
44
|
+
end
|
45
|
+
|
46
|
+
defp_class_mod(desired_nested_module, *nesting_list)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/modspace.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/modspace/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "modspace"
|
5
|
+
spec.version = Modspace::VERSION
|
6
|
+
spec.authors = ["declan@weuseopensource.com"]
|
7
|
+
spec.email = ["declan@weuseopensource.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Modspace is a small library to allow the easy definition and management of namespaces and nested modules in Ruby.}
|
10
|
+
spec.description = %q{Modspace is a small library to allow the easy definition and management of namespaces and nested modules in Ruby.}
|
11
|
+
spec.homepage = "https://github.com/theirishpenguin/modspace"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/theirishpenguin/modspace"
|
17
|
+
#spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "byebug", "~> 11.1.3"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: modspace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- declan@weuseopensource.com
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: byebug
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 11.1.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 11.1.3
|
27
|
+
description: Modspace is a small library to allow the easy definition and management
|
28
|
+
of namespaces and nested modules in Ruby.
|
29
|
+
email:
|
30
|
+
- declan@weuseopensource.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".travis.yml"
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/console
|
44
|
+
- bin/setup
|
45
|
+
- examples/simple_example.rb
|
46
|
+
- lib/modspace.rb
|
47
|
+
- lib/modspace/version.rb
|
48
|
+
- modspace.gemspec
|
49
|
+
homepage: https://github.com/theirishpenguin/modspace
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata:
|
53
|
+
homepage_uri: https://github.com/theirishpenguin/modspace
|
54
|
+
source_code_uri: https://github.com/theirishpenguin/modspace
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.3.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.1.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: Modspace is a small library to allow the easy definition and management of
|
74
|
+
namespaces and nested modules in Ruby.
|
75
|
+
test_files: []
|