name_space 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
19
+ .redcar/
20
+
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in namespace.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 fntzr <fantazuor@gmail.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,151 @@
1
+ # Namespace
2
+
3
+ Include and Extend your class with Namespace.
4
+ Method with the same name in different namespaces.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'namespace'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install namespace
19
+
20
+ # Usage
21
+
22
+ ## Define namespace with methods.
23
+
24
+ * name - [Symbol] - namespace name,
25
+
26
+ * block - []Block] with methods
27
+
28
+ ```ruby
29
+ class SomeClass
30
+ include Namespace
31
+ extend Namespace
32
+
33
+ namespace :my_namespace do
34
+ def method_a
35
+ 'method_a'
36
+ end
37
+ end
38
+ end
39
+
40
+ # For Instance
41
+ SomeClass.new.my_namespace.method_a # => 'method_a'
42
+ # For class
43
+ SomeClass.my_namespace.method_a # => 'method_a'
44
+ ```
45
+ ## Outer methods
46
+
47
+ All *namespaces* can call outer methods.
48
+ ```ruby
49
+ class SomeClass
50
+ include Namespace
51
+ extend Namespace
52
+
53
+ def outer
54
+ 'outer'
55
+ end
56
+
57
+ namespace :my_namespace do
58
+ def method_a
59
+ outer
60
+ end
61
+ end
62
+ end
63
+
64
+ SomeClass.new.my_namespace.method_a #=> outer
65
+ ```
66
+
67
+ ## Take all namespaces for class with `namespaces` method
68
+ ```ruby
69
+ class SomeClass
70
+ include Namespace
71
+ extend Namespace
72
+
73
+ namespace :a do
74
+ end
75
+ namespace :b do
76
+ end
77
+ namespace :c do
78
+ end
79
+ end
80
+
81
+ SomeClass.new.namespaces # => [:a, :b, :c]
82
+ ```
83
+
84
+ ## Reopen namespace.
85
+
86
+ You can reopen any namespace
87
+ ```ruby
88
+ class SomeClass
89
+ include Namespace
90
+ extend Namespace
91
+
92
+ namespace :a do
93
+ def a; 'a' end
94
+ end
95
+ namespace :a do
96
+ def b; 'b' end
97
+ end
98
+ end
99
+
100
+ SomeClass.a.a # => 'a'
101
+ SomeClass.a.b # => 'b'
102
+ ```
103
+
104
+ ##Namespaces and Inheritance.
105
+
106
+ All namespaces from superclass available in subclasses.
107
+ ```ruby
108
+ class SomeClass
109
+ include Namespace
110
+ extend Namespace
111
+
112
+ namespace :a do
113
+ def a; 'super' end
114
+ end
115
+ end
116
+
117
+ class SubSomeClass < SomeClass
118
+ namespace :a do
119
+ def 'a'; 'subclass' end
120
+ end
121
+ end
122
+
123
+ SubSomeClass.a.a # => 'subclass'
124
+ ```
125
+
126
+ ##Namespaces with Rails.
127
+
128
+ It can be use for save scopes (for example).
129
+ ```ruby
130
+ class Model < AR::B
131
+ include Namespace
132
+ extend Namespace
133
+
134
+ scope :lasts, ->{ }
135
+ namespace :my_custom_scopes do
136
+ scope :lasts, ->{ }
137
+ end
138
+ end
139
+
140
+ Model.lasts # => return from "lasts" scope
141
+ Model.my_custom_scopes.lasts # => return from namespace scope, "lasts" scope will be override
142
+ ```
143
+
144
+
145
+ ## Contributing
146
+
147
+ 1. Fork it
148
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
149
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
150
+ 4. Push to the branch (`git push origin my-new-feature`)
151
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run RSpec"
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.verbose = false
8
+ end
data/lib/namespace.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'namespace/namespace'
2
+ require 'namespace/exceptions'
3
+ require 'namespace/version'
4
+
5
+ module Namespace
6
+ end
@@ -0,0 +1,15 @@
1
+ #
2
+ # Raised when Class have method, which used for define namespace
3
+ # For Example:
4
+ #
5
+ # class SomeClass
6
+ # def some
7
+ # end
8
+ #
9
+ # namespace :some do
10
+ # end
11
+ # end
12
+ #
13
+ # Will be raised MethodExist error.
14
+ class MethodExist < Exception
15
+ end
@@ -0,0 +1,252 @@
1
+ # Include and Extend Namespace in your class.
2
+ #
3
+ # == Define namespace with methods.
4
+ #
5
+ # * name - [Symbol] - namespace name,
6
+ #
7
+ # * block - [Block] with methods/
8
+ #
9
+ #
10
+ # class SomeClass
11
+ # include Namespace
12
+ # extend Namespace
13
+ #
14
+ # namespace :my_namespace do
15
+ # def method_a
16
+ # 'method_a'
17
+ # end
18
+ # end
19
+ # end
20
+ #
21
+ # # For Instance
22
+ # SomeClass.new.my_namespace.method_a # => 'method_a'
23
+ # # For class
24
+ # SomeClass.my_namespace.method_a # => 'method_a'
25
+ #
26
+ # == Outer methods
27
+ # All *namespaces* can call outer methods.
28
+ #
29
+ # class SomeClass
30
+ # include Namespace
31
+ # extend Namespace
32
+ #
33
+ # def outer
34
+ # 'outer'
35
+ # end
36
+ #
37
+ # namespace :my_namespace do
38
+ # def method_a
39
+ # outer
40
+ # end
41
+ # end
42
+ # end
43
+ #
44
+ # SomeClass.new.my_namespace.method_a #=> outer
45
+ #
46
+ #
47
+ # == Take all namespaces for class with `namespaces` method
48
+ #
49
+ # class SomeClass
50
+ # include Namespace
51
+ # extend Namespace
52
+ #
53
+ # namespace :a do
54
+ # end
55
+ # namespace :b do
56
+ # end
57
+ # namespace :c do
58
+ # end
59
+ # end
60
+ #
61
+ # SomeClass.new.namespaces # => [:a, :b, :c]
62
+ #
63
+ #
64
+ # ==Reopen namespace.
65
+ # You can reopen any namespace
66
+ #
67
+ # class SomeClass
68
+ # include Namespace
69
+ # extend Namespace
70
+ #
71
+ # namespace :a do
72
+ # def a; 'a' end
73
+ # end
74
+ # namespace :a do
75
+ # def b; 'b' end
76
+ # end
77
+ # end
78
+ #
79
+ # SomeClass.a.a # => 'a'
80
+ # SomeClass.a.b # => 'b'
81
+ #
82
+ # ==Namespaces and Inheritance.
83
+ #
84
+ # All namespaces from superclass available in subclasses.
85
+ #
86
+ # class SomeClass
87
+ # include Namespace
88
+ # extend Namespace
89
+ #
90
+ # namespace :a do
91
+ # def a; 'super' end
92
+ # end
93
+ # end
94
+ #
95
+ # class SubSomeClass < SomeClass
96
+ # namespace :a do
97
+ # def 'a'; 'subclass' end
98
+ # end
99
+ # end
100
+ #
101
+ # SubSomeClass.a.a # => 'subclass'
102
+ #
103
+ #
104
+ # == Namespaces and Rails.
105
+ # It can be use for save scopes (for example).
106
+ #
107
+ # class Model < AR::B
108
+ # include Namespace
109
+ # extend Namespace
110
+ #
111
+ # scope :lasts, ->{ }
112
+ # namespace :my_custom_scopes do
113
+ # scope :lasts, ->{ }
114
+ # end
115
+ # end
116
+ #
117
+ # Model.lasts # => return from "lasts" scope
118
+ # Model.my_custom_scopes.lasts # => return from namespace scope, "lasts" scope will be override
119
+ #
120
+ #
121
+ module Namespace
122
+ #
123
+ #
124
+ # == Define namespace with methods.
125
+ #
126
+ # * name - Symbol - namespace name.
127
+ #
128
+ # * block - Blocl with methods
129
+ #
130
+ #
131
+ # class SomeClass
132
+ # include Namespace
133
+ # extend Namespace
134
+ #
135
+ # namespace :my_namespace do
136
+ # def method_a
137
+ # 'method_a'
138
+ # end
139
+ # end
140
+ # end
141
+ #
142
+ # # For Instance
143
+ # SomeClass.new.my_namespace.method_a # => 'method_a'
144
+ # # For class
145
+ # SomeClass.my_namespace.method_a # => 'method_a'
146
+ #
147
+ def namespace(name, &block)
148
+ namespace_exist?(name)
149
+ m = case self.respond_to?(name)
150
+ when true
151
+ self.send(name).instance_eval &block if block_given?
152
+ self.send(name)
153
+ else
154
+ Module.new do
155
+ instance_eval &block if block_given?
156
+ instance_eval do
157
+ #
158
+ # Return all methods from namespace
159
+ #
160
+ #
161
+ # class SomeClass
162
+ # include Namespace
163
+ # extend Namespace
164
+ #
165
+ # namespace :a do
166
+ # def a; end
167
+ # def b; end
168
+ # def c; end
169
+ # end
170
+ # end
171
+ #
172
+ # SomeClass.a.nmethods # => [:a, :b, :c]
173
+ #
174
+ def nmethods
175
+ methods(false) - [:method_missing, :nmethods]
176
+ end
177
+
178
+ def method_missing(method, *args, &block) #:nodoc:
179
+ raise NoMethodError.new(method) if not @klass.respond_to?(method) or @klass.namespaces.include?(method)
180
+ @klass.send(method, *args, &block)
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ ((@namespaces ||= []) << name).uniq!
187
+
188
+ define_namespaces # ->
189
+
190
+ %w(define_method define_singleton_method).each do |method|
191
+ send(method, name) do
192
+ klass = self
193
+ m.instance_eval{ @klass = klass }
194
+ m
195
+ end
196
+ end
197
+ end
198
+
199
+ #
200
+ # Use for create short alias for namespace.method
201
+ #
202
+ # * m1 - [String] method for alias
203
+ #
204
+ # * m2 - [String] namespace.method
205
+ #
206
+ # class SomeClass
207
+ # include Namespace
208
+ # extend Namespace
209
+ #
210
+ # namespace :some_namespace do
211
+ # def some_method
212
+ # #....
213
+ # 'hi'
214
+ # end
215
+ # end
216
+ #
217
+ # nalias 'my_method', 'some_namespace.some_method'
218
+ # end
219
+ #
220
+ # # Then
221
+ # SomeClass.new.my_method # => 'hi'
222
+ #
223
+ def nalias(m1, m2)
224
+ %w(define_method define_singleton_method).each do |method|
225
+ send(method, m1) do
226
+ namespace, method = m2.split('.')
227
+ self.send(namespace).send(method)
228
+ end
229
+ end
230
+ end
231
+
232
+ private
233
+ #
234
+ # define method for get all `namespaces` for class or class instance
235
+ #
236
+ def define_namespaces #:nodoc:
237
+ unless self.respond_to?(:namespaces)
238
+ send(:define_singleton_method, :namespaces) { @namespaces }
239
+ send(:define_method, :namespaces) { self.class.namespaces }
240
+ end
241
+ end
242
+
243
+ def namespace_exist?(name) #:nodoc:
244
+ if self.respond_to?(:namespaces)
245
+ return false if ( self.superclass.respond_to?(:namespaces) and self.superclass.namespaces.include?(name) ) or (self.respond_to?(:namespaces) and self.namespaces.include?(name))
246
+ end
247
+ raise MethodExist.new("Method `#{name}` already exist.") if (self.methods + self.instance_methods).include?(name)
248
+ end
249
+ end
250
+
251
+
252
+
@@ -0,0 +1,3 @@
1
+ module Namespace
2
+ VERSION = "0.0.1"
3
+ end
data/namespace.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'namespace/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "name_space"
8
+ gem.version = Namespace::VERSION
9
+ gem.authors = ["fntzr"]
10
+ gem.email = ["fantazuor@gmail.com"]
11
+ gem.description = "Add Namespace functionality"
12
+ gem.summary = "Include and Extend your class with Namespace. Create methods with the same name in different namespaces."
13
+ gem.homepage = "https://github.com/fntzr/namespace"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(spec|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency('rspec', [">= 2.0.0"])
20
+ end
@@ -0,0 +1,191 @@
1
+ require 'spec_helper'
2
+
3
+ class Object
4
+ include Namespace
5
+ extend Namespace
6
+ end
7
+
8
+ describe Namespace do
9
+
10
+ context "Base" do
11
+
12
+ before(:each) do
13
+ class A
14
+
15
+ def self.outer
16
+ 'class outer'
17
+ end
18
+
19
+ def outer
20
+ 'instance outer'
21
+ end
22
+
23
+ def self.z
24
+ 'from class'
25
+ end
26
+ def z
27
+ 'from instance'
28
+ end
29
+ namespace :a do
30
+ def z
31
+ 'from namespace @a'
32
+ end
33
+ def out
34
+ outer
35
+ end
36
+ end
37
+ namespace :b do
38
+ def z
39
+ 'from namespace @b'
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ it "have namespaces for instance" do
46
+ A.new.a.z.should == 'from namespace @a'
47
+ A.new.b.z.should == 'from namespace @b'
48
+ A.new.z.should == 'from instance'
49
+ end
50
+
51
+ it "have namespaces for class" do
52
+ A.a.z.should == 'from namespace @a'
53
+ A.b.z.should == 'from namespace @b'
54
+ A.z.should == 'from class'
55
+ end
56
+
57
+ it "call method from outer" do
58
+ A.new.a.out.should == 'instance outer'
59
+ A.a.out.should == 'class outer'
60
+ end
61
+ end
62
+
63
+ context "Exceptions" do
64
+
65
+ before(:each) do
66
+ class B
67
+ namespace :a do
68
+ end
69
+ namespace :b do
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ it "raise error when method not found" do
76
+ expect { B.new.a.no_method }.to raise_error(NoMethodError)
77
+ end
78
+
79
+ it "raise error when called method is namespace" do
80
+ expect { B.new.a.b }.to raise_error(NoMethodError)
81
+ end
82
+
83
+ it "raise error when method exist" do
84
+ expect do
85
+ class C
86
+ def a
87
+ end
88
+ namespace :a do
89
+ end
90
+ end.to raise_error(MethodExist)
91
+ end
92
+ end
93
+
94
+ it "not raise error when reopen namespace" do
95
+ expect do
96
+ class Z
97
+ namespace :a do end
98
+ namespace :a do end
99
+ end.to_not raise_error
100
+ end
101
+ end
102
+ end
103
+
104
+ context "Helpers" do
105
+ before (:each) do
106
+ class D
107
+ namespace :a do
108
+ def a; end
109
+ def b; end
110
+ def c; end
111
+ end
112
+ namespace :b do
113
+ def a; '@bnamespace' end
114
+ end
115
+ namespace :c do
116
+ end
117
+
118
+ nalias 'z', 'b.a'
119
+ end
120
+ end
121
+
122
+ it "return all namespaces with #namespaces method" do
123
+ D.new.namespaces.should == [:a, :b, :c]
124
+ D.namespaces.should == [:a, :b, :c]
125
+ end
126
+
127
+ it "create aliases for namespaces #nalias method" do
128
+ D.new.z.should == '@bnamespace'
129
+ D.z.should == '@bnamespace'
130
+ end
131
+
132
+ it "return all method from namespace" do
133
+ D.a.nmethods.should eq [:a, :b, :c]
134
+ D.b.nmethods.should eq [:a]
135
+ D.new.a.nmethods.should eq [:a, :b, :c]
136
+ D.new.b.nmethods.should eq [:a]
137
+ end
138
+ end
139
+
140
+ context "Reopen" do
141
+ before(:each) do
142
+ class E
143
+ namespace :a do
144
+ def a; end
145
+ end
146
+ namespace :a do
147
+ def b; end
148
+ end
149
+ end
150
+ end
151
+
152
+ it "reopen namespace" do
153
+ E.new.a.nmethods.should == [:a, :b]
154
+ E.namespaces.should == [:a]
155
+ end
156
+ end
157
+
158
+ context "Inheritance" do
159
+ before(:each) do
160
+ class F
161
+ namespace :a do
162
+ def a; 'a' end
163
+ def b; 'b' end
164
+ end
165
+
166
+ def z; 'z' end
167
+ end
168
+
169
+ class G < F
170
+ namespace :a do
171
+ def b; 'G class a-namespace' end
172
+ def c; 'c' end
173
+ end
174
+ namespace :b do
175
+ def c; z end
176
+ end
177
+
178
+ nalias 'd', 'a.a'
179
+ end
180
+ end
181
+
182
+ it "namespace in subclasses" do
183
+ G.new.a.a.should == 'a'
184
+ G.new.a.b.should == 'G class a-namespace'
185
+ G.namespaces.should == [:a, :b]
186
+ F.namespaces.should == [:a]
187
+ G.new.b.c.should == 'z'
188
+ G.new.d.should == 'a'
189
+ end
190
+ end
191
+ end
@@ -0,0 +1 @@
1
+ require 'namespace'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: name_space
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - fntzr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ description: Add Namespace functionality
31
+ email:
32
+ - fantazuor@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/namespace.rb
44
+ - lib/namespace/exceptions.rb
45
+ - lib/namespace/namespace.rb
46
+ - lib/namespace/version.rb
47
+ - namespace.gemspec
48
+ - spec/namespace_spec.rb
49
+ - spec/spec_helper.rb
50
+ homepage: https://github.com/fntzr/namespace
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.25
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Include and Extend your class with Namespace. Create methods with the same
74
+ name in different namespaces.
75
+ test_files:
76
+ - spec/namespace_spec.rb
77
+ - spec/spec_helper.rb
78
+ has_rdoc: