consty 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/consty.rb +15 -9
- data/lib/consty/version.rb +1 -1
- data/spec/consty_spec.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb8304245d869a4725cb9b53a4ae2c3e49b27bbb
|
4
|
+
data.tar.gz: a059822f1637544aa861b912be6addc2f652af3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f48761cae2c858322e302667c7a9450ee38202fdd570ef378a884e2b3f3b61c67881fe0e975bab657b843f8f1a1fdb364f724a07a0309a6e14781fdb29cd2de
|
7
|
+
data.tar.gz: 5eb9355d2629433ba8b88d2de15280766b102d21ac12abd347448046d50309f1dcb85d3acc9e0dca1879cab550e99456508cca1f296c42369ad6fbe4f9043406
|
data/lib/consty.rb
CHANGED
@@ -7,11 +7,17 @@ class Consty
|
|
7
7
|
def get(name, namespace=Object)
|
8
8
|
current_namespace = namespace
|
9
9
|
while current_namespace do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
begin
|
11
|
+
return secuential_get name, current_namespace
|
12
|
+
rescue NameError
|
13
|
+
namespace_name = current_namespace.name.split('::')[0..-2].join('::')
|
14
|
+
if !namespace_name.empty?
|
15
|
+
current_namespace = secuential_get namespace_name
|
16
|
+
elsif current_namespace != Object
|
17
|
+
current_namespace = Object
|
18
|
+
else
|
19
|
+
current_namespace = nil
|
20
|
+
end
|
15
21
|
end
|
16
22
|
end
|
17
23
|
namespace.const_missing name
|
@@ -27,11 +33,11 @@ class Consty
|
|
27
33
|
name_sections = name_sections[1..-1]
|
28
34
|
end
|
29
35
|
|
30
|
-
name_sections.inject(namespace) do |
|
31
|
-
if
|
32
|
-
|
36
|
+
name_sections.inject(namespace) do |scope, section|
|
37
|
+
if scope.constants.include?(section.to_sym)
|
38
|
+
scope.const_get section
|
33
39
|
else
|
34
|
-
|
40
|
+
scope.const_missing section
|
35
41
|
end
|
36
42
|
end
|
37
43
|
|
data/lib/consty/version.rb
CHANGED
data/spec/consty_spec.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'minitest_helper'
|
2
2
|
|
3
|
+
ROOT = 0
|
3
4
|
VAL = 1
|
4
5
|
|
5
6
|
module Foo
|
@@ -28,6 +29,7 @@ describe Consty do
|
|
28
29
|
describe 'Unscoped' do
|
29
30
|
|
30
31
|
it 'Get from string' do
|
32
|
+
Consty.get('ROOT').must_equal ROOT
|
31
33
|
Consty.get('VAL').must_equal VAL
|
32
34
|
Consty.get('Foo').must_equal Foo
|
33
35
|
Consty.get('Foo::VAL').must_equal Foo::VAL
|
@@ -37,6 +39,7 @@ describe Consty do
|
|
37
39
|
end
|
38
40
|
|
39
41
|
it 'Get from symbol' do
|
42
|
+
Consty.get('ROOT'.to_sym).must_equal ROOT
|
40
43
|
Consty.get('VAL'.to_sym).must_equal VAL
|
41
44
|
Consty.get('Foo'.to_sym).must_equal Foo
|
42
45
|
Consty.get('Foo::VAL'.to_sym).must_equal Foo::VAL
|
@@ -46,6 +49,8 @@ describe Consty do
|
|
46
49
|
end
|
47
50
|
|
48
51
|
it 'Explicit :: prefix (::SomeClass)' do
|
52
|
+
Consty.get('::ROOT').must_equal ROOT
|
53
|
+
Consty.get('::VAL').must_equal VAL
|
49
54
|
Consty.get('::Foo').must_equal Foo
|
50
55
|
Consty.get('::Foo::Bar').must_equal Foo::Bar
|
51
56
|
Consty.get('::Foo::Bar::VAL').must_equal Foo::Bar::VAL
|
@@ -65,17 +70,20 @@ describe Consty do
|
|
65
70
|
describe 'Namespace scoped' do
|
66
71
|
|
67
72
|
it 'Get closest constant' do
|
73
|
+
Consty.get('ROOT', Foo).must_equal ROOT
|
68
74
|
Consty.get('VAL', Foo).must_equal Foo::VAL
|
69
75
|
Consty.get('Bar', Foo).must_equal Foo::Bar
|
70
76
|
Consty.get('Bar::VAL', Foo).must_equal Foo::Bar::VAL
|
71
77
|
Consty.get('Baz::VAL', Foo).must_equal Foo::Bar::VAL
|
72
78
|
Consty.get('VAL', Foo::Bar).must_equal Foo::Bar::VAL
|
79
|
+
Consty.get('VAL', Foo::Baz).must_equal Foo::Bar::VAL
|
73
80
|
Consty.get('Bar', Foo::Baz).must_equal Foo::Bar
|
74
81
|
Consty.get('VAL', Foo::Bar::Qux).must_equal Foo::Bar::VAL
|
75
82
|
Consty.get('VAL', Foo::Baz::Qux).must_equal Foo::Bar::VAL
|
76
83
|
end
|
77
84
|
|
78
85
|
it 'Explicit :: prefix (::SomeClass)' do
|
86
|
+
Consty.get('::ROOT', Foo).must_equal ROOT
|
79
87
|
Consty.get('::VAL', Foo).must_equal VAL
|
80
88
|
Consty.get('::VAL', Foo::Bar).must_equal VAL
|
81
89
|
end
|
@@ -92,6 +100,7 @@ describe Consty do
|
|
92
100
|
end
|
93
101
|
|
94
102
|
it 'Ignore top level defined constant' do
|
103
|
+
proc { Consty.get('Foo::ROOT') }.must_raise NameError
|
95
104
|
proc { Consty.get('Foo::Bar::Qux::VAL') }.must_raise NameError
|
96
105
|
proc { Consty.get('Foo::Baz::Qux::VAL') }.must_raise NameError
|
97
106
|
proc { Consty.get('Bar::Qux::VAL', Foo) }.must_raise NameError
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Naiman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|