nesting 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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +1 -0
- data/Gemfile +11 -0
- data/LICENCE.txt +19 -0
- data/README.md +31 -0
- data/Rakefile +9 -0
- data/VERSION +1 -0
- data/lib/nesting.rb +51 -0
- data/nesting.gemspec +17 -0
- data/spec/nesting_spec.rb +231 -0
- data/spec/spec_helper.rb +3 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: dc78b802ce1d9064e4f6f617cf9c35d5349ee187
|
4
|
+
data.tar.gz: f585ba62184efafba53d9943d262372b15e6ae2d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 06689cacfa1be069d0be8cda3e89eb97f2eaaaaaeac05941f741ecb82e05bccdce5a0e49e60c1a654ffe82e9db1343c5270f32d4cb2e17cb36711dd5b5f8d93b
|
7
|
+
data.tar.gz: 0de37e3bd6b50da15bea44d874487bb8866e4bcd6962c0f854ad35ce52feea43f1d51c1d1037d3520632e4c6fc77e5afbd1e11d20d583b80acad5ccc2e351e08
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENCE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2014 Kyrylo Silin
|
2
|
+
|
3
|
+
This software is provided 'as-is', without any express or implied
|
4
|
+
warranty. In no event will the authors be held liable for any damages
|
5
|
+
arising from the use of this software.
|
6
|
+
|
7
|
+
Permission is granted to anyone to use this software for any purpose,
|
8
|
+
including commercial applications, and to alter it and redistribute it
|
9
|
+
freely, subject to the following restrictions:
|
10
|
+
|
11
|
+
1. The origin of this software must not be misrepresented; you must not
|
12
|
+
claim that you wrote the original software. If you use this software
|
13
|
+
in a product, an acknowledgment in the product documentation would be
|
14
|
+
appreciated but is not required.
|
15
|
+
|
16
|
+
2. Altered source versions must be plainly marked as such, and must not be
|
17
|
+
misrepresented as being the original software.
|
18
|
+
|
19
|
+
3. This notice may not be removed or altered from any source distribution.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Nesting
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nesting'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nesting
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/nesting/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/nesting.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Nesting
|
2
|
+
class << self
|
3
|
+
def of(mod)
|
4
|
+
if nonmodule?(mod)
|
5
|
+
raise TypeError, 'mod is not a class or module'
|
6
|
+
end
|
7
|
+
|
8
|
+
level = 0
|
9
|
+
|
10
|
+
# Special case Object as it's the namespace of all classes.
|
11
|
+
return level if mod == Object
|
12
|
+
|
13
|
+
level = if mod.name
|
14
|
+
mod.name.split('::').count
|
15
|
+
else
|
16
|
+
-1 # Probably an anonymous module.
|
17
|
+
end
|
18
|
+
|
19
|
+
level
|
20
|
+
end
|
21
|
+
|
22
|
+
def parents(mod)
|
23
|
+
if nonmodule?(mod)
|
24
|
+
raise TypeError, 'mod is not a class or module'
|
25
|
+
end
|
26
|
+
|
27
|
+
parents = []
|
28
|
+
|
29
|
+
return nil if mod == Object
|
30
|
+
|
31
|
+
if mod.name
|
32
|
+
mod.name.split('::')[0..-2].inject(Object) do |parent, child|
|
33
|
+
const = parent.const_get(child)
|
34
|
+
parents << const
|
35
|
+
const
|
36
|
+
end
|
37
|
+
parents.reverse! << Object
|
38
|
+
else
|
39
|
+
parents = nil # Probably an anonymous module.
|
40
|
+
end
|
41
|
+
|
42
|
+
parents
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def nonmodule?(mod)
|
48
|
+
!((mod.class == Module) ^ (mod.class == Class))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/nesting.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "nesting"
|
3
|
+
spec.version = File.read('VERSION')
|
4
|
+
spec.date = Time.now.strftime('%Y-%m-%d')
|
5
|
+
spec.authors = ["Kyrylo Silin"]
|
6
|
+
spec.email = ["silin@kyrylo.org"]
|
7
|
+
spec.summary = %q{Detects nesting of a module/class}
|
8
|
+
spec.homepage = "https://github.com/kyrylo/nesting"
|
9
|
+
spec.license = "zlib"
|
10
|
+
|
11
|
+
spec.files = `git ls-files -z`.split("\x0")
|
12
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
13
|
+
spec.require_paths = ["lib"]
|
14
|
+
|
15
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
16
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
17
|
+
end
|
@@ -0,0 +1,231 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
RSpec.describe Nesting do
|
5
|
+
describe ".of" do
|
6
|
+
it "raises an error on a non-module or a non-class argument" do
|
7
|
+
expect { Nesting.of(123) }.to raise_error(TypeError)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "doesn't raise error on an instance of Module" do
|
11
|
+
expect { Nesting.of(Module.new).not_to raise_error(TypeError) }
|
12
|
+
end
|
13
|
+
|
14
|
+
it "doesn't raise error on an instance of Class" do
|
15
|
+
expect { Nesting.of(Class.new).not_to raise_error(TypeError) }
|
16
|
+
end
|
17
|
+
|
18
|
+
it "detects nesting of BasicObject" do
|
19
|
+
expect(Nesting.of(BasicObject)).to eq(1)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "detects nesting of Object" do
|
23
|
+
expect(Nesting.of(Object)).to eq(0)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "detects nesting of a top-level class" do
|
27
|
+
class ::TopLevelClass1; end
|
28
|
+
expect(Nesting.of(TopLevelClass1)).to eq(1)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "detects nesting of a top-level module" do
|
32
|
+
module ::TopLevelModule1; end
|
33
|
+
expect(Nesting.of(TopLevelModule1)).to eq(1)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns -1 if a class is anonymous" do
|
37
|
+
expect(Nesting.of(Class.new)).to eq(-1)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns -1 if a module is anonymous" do
|
41
|
+
expect(Nesting.of(Module.new)).to eq(-1)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "detects nesting of a nested class" do
|
45
|
+
class A1
|
46
|
+
class B1; end
|
47
|
+
end
|
48
|
+
|
49
|
+
expect(Nesting.of(A1)).to eq(1)
|
50
|
+
expect(Nesting.of(A1::B1)).to eq(2)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "detects nesting of a nested module" do
|
54
|
+
module A2
|
55
|
+
module B2; end
|
56
|
+
end
|
57
|
+
|
58
|
+
expect(Nesting.of(A2)).to eq(1)
|
59
|
+
expect(Nesting.of(A2::B2)).to eq(2)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "detects mixed module/class nesting" do
|
63
|
+
module A3
|
64
|
+
class B3
|
65
|
+
module C3; end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
expect(Nesting.of(A3)).to eq(1)
|
70
|
+
expect(Nesting.of(A3::B3)).to eq(2)
|
71
|
+
expect(Nesting.of(A3::B3::C3)).to eq(3)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "detects nesting of a nested module defined via the shortcut" do
|
75
|
+
module A4; end
|
76
|
+
module A4::B4; end
|
77
|
+
|
78
|
+
expect(Nesting.of(A4)).to eq(1)
|
79
|
+
expect(Nesting.of(A4::B4)).to eq(2)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "detects nesting of the class whose superclass is BasicObject" do
|
83
|
+
class A5 < BasicObject; end
|
84
|
+
expect(Nesting.of(A5)).to eq(1)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "detects nesting of the nested class whose superclass is BasicObject" do
|
88
|
+
class A6; end
|
89
|
+
class A6::B6 < BasicObject; end
|
90
|
+
|
91
|
+
expect(Nesting.of(A6)).to eq(1)
|
92
|
+
expect(Nesting.of(A6::B6)).to eq(2)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "detects nesting of a module depending on the context" do
|
96
|
+
$expect = method(:expect)
|
97
|
+
$eq = method(:eq)
|
98
|
+
|
99
|
+
module A7
|
100
|
+
module B7
|
101
|
+
module C7
|
102
|
+
$expect.(Nesting.of(B7)).to $eq.(2)
|
103
|
+
$expect.(Nesting.of(C7)).to $eq.(3)
|
104
|
+
end
|
105
|
+
$expect.(Nesting.of(A7)).to $eq.(1)
|
106
|
+
$expect.(Nesting.of(B7)).to $eq.(2)
|
107
|
+
end
|
108
|
+
|
109
|
+
$expect.(Nesting.of(A7)).to $eq.(1)
|
110
|
+
$expect.(Nesting.of(B7)).to $eq.(2)
|
111
|
+
$expect.(Nesting.of(B7::C7)).to $eq.(3)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe ".parents" do
|
117
|
+
it "raises an error on a non-module or a non-class argument" do
|
118
|
+
expect { Nesting.parents(123) }.to raise_error(TypeError)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "doesn't raise error on an instance of Module" do
|
122
|
+
expect { Nesting.parents(Module.new).not_to raise_error(TypeError) }
|
123
|
+
end
|
124
|
+
|
125
|
+
it "doesn't raise error on an instance of Class" do
|
126
|
+
expect { Nesting.parents(Class.new).not_to raise_error(TypeError) }
|
127
|
+
end
|
128
|
+
|
129
|
+
it "detects parents of BasicObject" do
|
130
|
+
expect(Nesting.parents(BasicObject)).to eq([Object])
|
131
|
+
end
|
132
|
+
|
133
|
+
it "detects parents of Object" do
|
134
|
+
expect(Nesting.parents(Object)).to eq(nil)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "detects parents of a top-level class" do
|
138
|
+
class ::TopLevelClass2; end
|
139
|
+
expect(Nesting.parents(TopLevelClass2)).to eq([Object])
|
140
|
+
end
|
141
|
+
|
142
|
+
it "detects parents of a top-level module" do
|
143
|
+
module ::TopLevelModule2; end
|
144
|
+
expect(Nesting.parents(TopLevelModule2)).to eq([Object])
|
145
|
+
end
|
146
|
+
|
147
|
+
it "returns nil if a class is anonymous" do
|
148
|
+
expect(Nesting.parents(Class.new)).to eq(nil)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "returns nil if a module is anonymous" do
|
152
|
+
expect(Nesting.parents(Module.new)).to eq(nil)
|
153
|
+
end
|
154
|
+
|
155
|
+
it "detects parents of a nested class" do
|
156
|
+
class A1
|
157
|
+
class B1; end
|
158
|
+
end
|
159
|
+
|
160
|
+
expect(Nesting.parents(A1)).to eq([Object])
|
161
|
+
expect(Nesting.parents(A1::B1)).to eq([A1, Object])
|
162
|
+
end
|
163
|
+
|
164
|
+
it "detects parents of a nested module" do
|
165
|
+
module A2
|
166
|
+
module B2; end
|
167
|
+
end
|
168
|
+
|
169
|
+
expect(Nesting.parents(A2)).to eq([Object])
|
170
|
+
expect(Nesting.parents(A2::B2)).to eq([A2, Object])
|
171
|
+
end
|
172
|
+
|
173
|
+
it "detects mixed module/class parents" do
|
174
|
+
module A3
|
175
|
+
class B3
|
176
|
+
module C3
|
177
|
+
class D3; end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
expect(Nesting.parents(A3)).to eq([Object])
|
183
|
+
expect(Nesting.parents(A3::B3)).to eq([A3, Object])
|
184
|
+
expect(Nesting.parents(A3::B3::C3)).to eq([A3::B3, A3, Object])
|
185
|
+
end
|
186
|
+
|
187
|
+
it "detects parents of a nested module defined via the shortcut" do
|
188
|
+
module A4; end
|
189
|
+
module A4::B4; end
|
190
|
+
|
191
|
+
expect(Nesting.parents(A4)).to eq([Object])
|
192
|
+
expect(Nesting.parents(A4::B4)).to eq([A4, Object])
|
193
|
+
end
|
194
|
+
|
195
|
+
it "detects parents of the class whose superclass is BasicObject" do
|
196
|
+
class A5 < BasicObject; end
|
197
|
+
expect(Nesting.parents(A5)).to eq([Object])
|
198
|
+
end
|
199
|
+
|
200
|
+
it "detects parents of the nested class whose superclass is BasicObject" do
|
201
|
+
class A6; end
|
202
|
+
class A6::B6 < BasicObject; end
|
203
|
+
|
204
|
+
expect(Nesting.parents(A6)).to eq([Object])
|
205
|
+
expect(Nesting.parents(A6::B6)).to eq([A6, Object])
|
206
|
+
end
|
207
|
+
|
208
|
+
it "detects parents of a module depending on the context" do
|
209
|
+
$expect = method(:expect)
|
210
|
+
$eq = method(:eq)
|
211
|
+
|
212
|
+
module A7
|
213
|
+
module B7
|
214
|
+
module C7
|
215
|
+
$expect.(Nesting.parents(A7)).to $eq.([Object])
|
216
|
+
$expect.(Nesting.parents(B7)).to $eq.([A7, Object])
|
217
|
+
$expect.(Nesting.parents(C7)).to $eq.([A7::B7, A7, Object])
|
218
|
+
end
|
219
|
+
|
220
|
+
$expect.(Nesting.parents(A7)).to $eq.([Object])
|
221
|
+
$expect.(Nesting.parents(B7)).to $eq.([A7, Object])
|
222
|
+
$expect.(Nesting.parents(C7)).to $eq.([A7::B7, A7, Object])
|
223
|
+
end
|
224
|
+
|
225
|
+
$expect.(Nesting.parents(A7)).to $eq.([Object])
|
226
|
+
$expect.(Nesting.parents(B7)).to $eq.([A7, Object])
|
227
|
+
$expect.(Nesting.parents(B7::C7)).to $eq.([A7::B7, A7, Object])
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nesting
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyrylo Silin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- silin@kyrylo.org
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- Gemfile
|
51
|
+
- LICENCE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- VERSION
|
55
|
+
- lib/nesting.rb
|
56
|
+
- nesting.gemspec
|
57
|
+
- spec/nesting_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: https://github.com/kyrylo/nesting
|
60
|
+
licenses:
|
61
|
+
- zlib
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Detects nesting of a module/class
|
83
|
+
test_files:
|
84
|
+
- spec/nesting_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
has_rdoc:
|