modspace 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2930117c3d626d7606136b50133295902be4eb26cf4ea6377f8216964eec5140
4
- data.tar.gz: 0d9db6db194eab6c1ae0d2b89b412f45fbdb7d63c7280b8b4cc9a1fbfe647f5f
3
+ metadata.gz: 95e3764e4a780cee8b92285aa671e446427ba71fa0ce412f093ea0ac8703359d
4
+ data.tar.gz: 8be8b893403b4db6eac524f133ff4b757d6c458c3669eddd5470172ba1d651a5
5
5
  SHA512:
6
- metadata.gz: 3b409b9dfd31444b2e63fa3a545d3141b871407145fb159c3336dad316254aae4db1aed91682a853662a22f8fe7267f0c06da9c478708ff04ffe1e57d396814d
7
- data.tar.gz: ae8fc8258b54aee5a422606a72e0189a6b88dde8195c7a9aa7fdd364f5e881f85113fe4a188b8183238e37c68b0d08af59b45e3aa254f31c95a67eeb301a99d7
6
+ metadata.gz: 8ecbd9b92e35b7376b9750bba5a7618d16d157c75cabe512e5024a26e59dccf3e09cbc25cdb9a3842c42148ad25aeca951f510776cf34915813aa2578633b869
7
+ data.tar.gz: 75fe9c045adf2ad4923f48277da8706ac76d92c4033b65db63873996c86873781549629eb812bd9c189e7cd084f011bca9e60e82703dc1a3ba898d95f8e48f16
@@ -1,13 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- modspace (0.1.0)
5
- byebug (~> 11.1.3)
4
+ modspace (0.2.0)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
9
8
  specs:
10
- byebug (11.1.3)
11
9
  diff-lcs (1.4.4)
12
10
  rake (12.3.3)
13
11
  rspec (3.9.0)
data/README.md CHANGED
@@ -77,7 +77,7 @@ Define a new namespace (ie. nested module), such as `Foo::Bar::Zoo` in one line.
77
77
  Modspace.def_mod('Foo', 'Bar', 'Zoo')
78
78
  # Note: This creates the namespace Foo::Bar::Zoo
79
79
 
80
- # Implementation
80
+ # Implement the module you want with the methods you want...
81
81
  module Foo::Bar::Zoo::Shoe
82
82
  def some_funct
83
83
  puts "Eek"
@@ -87,6 +87,44 @@ Define a new namespace (ie. nested module), such as `Foo::Bar::Zoo` in one line.
87
87
  __Note: You only need to define the parent namespace. For example, we
88
88
  omit "Shoe" from the initial namespace definition above.__
89
89
 
90
+ You can also use a shortcut syntax that looks more like the nested
91
+ module syntax in Ruby...
92
+
93
+ require 'modspace'
94
+
95
+ # Define parent namespace
96
+ Modspace.def_mod('Foo::Bar::Zoo')
97
+ # Note: This creates the namespace Foo::Bar::Zoo
98
+
99
+ # Same as before, implement the module you want with the methods you want...
100
+ module Foo::Bar::Zoo::Shoe
101
+ def some_funct
102
+ puts "Eek"
103
+ end
104
+ end
105
+
106
+ All this is quite handle if you are doing functional programming or
107
+ modular programming in Ruby...
108
+
109
+ require 'modspace'
110
+
111
+ # Define parent namespace
112
+ Modspace.def_mod('Foo::Bar::Zoo')
113
+ # Note: This creates the namespace Foo::Bar::Zoo
114
+
115
+ # Same as before, implement the module you want with the methods you want...
116
+ module Foo::Bar::Zoo::Shoe
117
+ class << self
118
+ def some_funct
119
+ puts "Eek"
120
+ end
121
+ end
122
+ end
123
+
124
+ # Call you neatly scoped module!
125
+ Foo::Bar::Zoo::Shoe.some_funct
126
+ # => "Eek"
127
+
90
128
  For more examples, see the `examples/` directory.
91
129
 
92
130
  ## Contributing
@@ -17,7 +17,7 @@ end
17
17
 
18
18
  # ^ Note: Defaults to defining the namespace within "Object", as that is the top-level namespace within Ruby programs
19
19
 
20
- # 2) Create a new namespace (ie. nested module) and assign it to a variable...
20
+ # 2) Create a new namespace and assign it to a variable...
21
21
  zoo2_module = Modspace.def_mod('Foo', 'Bar', 'Zoo2')
22
22
 
23
23
  if zoo2_module == Foo::Bar::Zoo2
@@ -26,7 +26,7 @@ else
26
26
  raise StandardError
27
27
  end
28
28
 
29
- # 3) Create a new namespace (ie. nested module) within a class...
29
+ # 3) Create a new namespace within a class...
30
30
  class SomeClass; end
31
31
 
32
32
  zoo3_module = Modspace.def_mod('Foo', 'Bar', 'Zoo3', in_namespace: SomeClass)
@@ -37,7 +37,7 @@ else
37
37
  raise StandardError
38
38
  end
39
39
 
40
- # 4) Create a new namespace (ie. nested module) within a class...
40
+ # 4) Create a new namespace within a class given an instance...
41
41
 
42
42
  an_instance_of_some_class = SomeClass.new
43
43
 
@@ -49,7 +49,7 @@ else
49
49
  raise StandardError
50
50
  end
51
51
 
52
- # 5) Create a new namespace (ie. nested module) within a module...
52
+ # 5) Create a new namespace within a module...
53
53
  module SomeModule; end
54
54
 
55
55
  zoo5_module = Modspace.def_mod('Foo', 'Bar', 'Zoo5', in_namespace: SomeModule)
@@ -59,3 +59,14 @@ if zoo5_module == SomeModule::Foo::Bar::Zoo5
59
59
  else
60
60
  raise StandardError
61
61
  end
62
+
63
+ # 6) Create a new namespace within a module using the shortcut syntax...
64
+ module SomeModule; end
65
+
66
+ zoo6_module = Modspace.def_mod('Foo::Bar::Zoo6', in_namespace: SomeModule)
67
+
68
+ if zoo6_module == SomeModule::Foo::Bar::Zoo6
69
+ puts "6) We have defined a nested module Foo::Bar::Zoo5 within an explicit module using shortcut syntax"
70
+ else
71
+ raise StandardError
72
+ end
@@ -10,6 +10,8 @@ module Modspace
10
10
  # for our purposes.
11
11
 
12
12
  def def_mod(*args, in_namespace: Object, in_class_of_instance: nil)
13
+ normalised_args = expand_any_double_colon_namespace_args(args)
14
+
13
15
  parent_namespace = if !in_class_of_instance.nil?
14
16
  in_class_of_instance.class
15
17
  elsif !in_namespace.nil? # :in_namespace can be a module or class
@@ -18,7 +20,7 @@ module Modspace
18
20
  raise ArgumentError, "If in_class or for_instance non-nil is specified they must be non-nil."
19
21
  end
20
22
 
21
- defp_class_mod(parent_namespace, *args)
23
+ defp_class_mod(parent_namespace, *normalised_args)
22
24
  end
23
25
 
24
26
  private
@@ -45,5 +47,10 @@ module Modspace
45
47
 
46
48
  defp_class_mod(desired_nested_module, *nesting_list)
47
49
  end
50
+
51
+ # This method takes args like ["Foo::Bar::Zoo"] and turns them into ["Foo", "Bar", "Zoo"]
52
+ def expand_any_double_colon_namespace_args(args_arr)
53
+ args_arr.map{|arg| arg.split('::') }.flatten
54
+ end
48
55
  end
49
56
  end
@@ -1,3 +1,3 @@
1
1
  module Modspace
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -25,5 +25,4 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
27
 
28
- spec.add_dependency "byebug", "~> 11.1.3"
29
28
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modspace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - declan@weuseopensource.com
8
8
  autorequire:
9
9
  bindir: exe
10
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
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: Modspace is a small library to allow the easy definition and management
28
14
  of namespaces and nested modules in Ruby.
29
15
  email: