const_lookup 0.4.0 → 0.5.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
  SHA1:
3
- metadata.gz: 3eecd6ef6683b4b6cb3aaa860153047ba969b853
4
- data.tar.gz: 87609285e5549e1e375a8f958118037cdae90a6a
3
+ metadata.gz: 6272f106053cfae63ddee05a1ee5e21636d40852
4
+ data.tar.gz: b8393dacdefb03921a50aa5b613d483414ad2abb
5
5
  SHA512:
6
- metadata.gz: 55f4cc3995ca5af7e0b31c3d5df4124ab0b13f877111b5ea3ca6bcde58c099df17241d5c8369ad4ecd5ac4db9dd43500891aaf53a99230a20c72db4cca914b82
7
- data.tar.gz: 787cac62e65e37edf237f3fbaf408a9f3e103ab1f0af3f5aba6e69d7da779c8ae57d5af576dcbc829c1a57319b5ce783e60f2d491147bfd19fa188413612234b
6
+ metadata.gz: f5fbc355c841826f7fe708fdc6a806c59fd49fdc637f78360ba3297cd335a64f66756a9205efaf77c287a3e415e2330ffd618acc3b14ae59ea95e41c067d68fa
7
+ data.tar.gz: e01ed3588c93c7ff9a3c67bd01fbc51eb1244cc5b39ec78aa5330fe5640aedda3edc45e74b8057ba76fef505b6614f88564d89651ebd70ad094efa632ac70c53
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/amarshall/const_lookup.png?branch=master)](http://travis-ci.org/amarshall/const_lookup)
4
4
  [![Code Climate rating](https://codeclimate.com/github/amarshall/const_lookup.png)](https://codeclimate.com/github/amarshall/const_lookup)
5
- [![Gem Version](https://badge.fury.io/rb/const_lookup.png)](https://badge.fury.io/rb/const_lookup)
5
+ [![Gem Version](https://badge.fury.io/rb/const_lookup.png)](https://rubygems.org/gems/const_lookup)
6
6
 
7
7
  Makes resolving a constant in a given namespace easy.
8
8
 
@@ -29,7 +29,7 @@ ConstLookup.lookup('E', A::C) #=> #<NameError: Failed to find `E' in A::C>
29
29
 
30
30
  Or, if you like monkey-patching Ruby core:
31
31
 
32
- ```
32
+ ```ruby
33
33
  module A
34
34
  module B; end
35
35
  module C; end
@@ -42,6 +42,23 @@ A::C.const_lookup('B') #=> A::B
42
42
  A::C.const_lookup('D') #=> D
43
43
  ```
44
44
 
45
+ Compare this with using [`Module#const_get`](http://ruby-doc.org/core/Module.html#method-i-const_get):
46
+
47
+ ```ruby
48
+ module A
49
+ module B; end
50
+ module C; end
51
+ end
52
+ module D; end
53
+
54
+ A::C.const_get('A') #=> A
55
+ A::C.const_get('B') #=> #<NameError: uninitialized constant A::C::B>
56
+ A::C.const_get('D') #=> D
57
+ A::C.const_get('E') #=> #<NameError: uninitialized constant A::C::E>
58
+ ```
59
+
60
+ This is because `const_get` looks up the *ancestor tree*, not the namespace tree like `ConstLookup` does.
61
+
45
62
  ## Contributing
46
63
 
47
64
  Contributions are welcome. Please be sure that your pull requests are atomic so they can be considered and accepted separately.
@@ -1,11 +1,12 @@
1
1
  require 'const_lookup/version'
2
2
 
3
3
  class ConstLookup
4
- def self.lookup name, namespace = nil
5
- new(*namespace).lookup(name)
4
+ def self.lookup name, namespace = Object
5
+ new(namespace).lookup(name)
6
6
  end
7
7
 
8
- def initialize namespace = Object
8
+ def initialize namespace
9
+ raise ArgumentError unless namespace.is_a? Module
9
10
  @namespace = namespace.to_s
10
11
  end
11
12
 
@@ -1,3 +1,3 @@
1
1
  class ConstLookup
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -12,16 +12,30 @@ describe ConstLookup do
12
12
  ConstLookup.lookup name, namespace
13
13
  end
14
14
 
15
- it "creates the new finder with no args when no namespace given" do
15
+ it "creates the new finder with Object when no namespace given" do
16
16
  name = double
17
17
  finder = double
18
- expect(ConstLookup).to receive(:new).with(no_args).and_return finder
18
+ expect(ConstLookup).to receive(:new).with(Object).and_return finder
19
19
  expect(finder).to receive(:lookup).with(name)
20
20
 
21
21
  ConstLookup.lookup name
22
22
  end
23
23
  end
24
24
 
25
+ describe "#initialize" do
26
+ it "accepts a Module as a namespace" do
27
+ expect { ConstLookup.new(Module.new) }.to_not raise_error
28
+ end
29
+
30
+ it "accepts a Class as a namespace" do
31
+ expect { ConstLookup.new(Class.new) }.to_not raise_error
32
+ end
33
+
34
+ it "raises an ArgumentError when the namespace is not a Class/Module" do
35
+ expect { ConstLookup.new(Object.new) }.to raise_error ArgumentError
36
+ end
37
+ end
38
+
25
39
  describe "#lookup" do
26
40
  it "returns the constant when it resides in the most specific namespace" do
27
41
  stub_const('Foo::Bar::Baz', Module.new)
@@ -71,11 +85,6 @@ describe ConstLookup do
71
85
  end
72
86
 
73
87
  describe "#lookup_path" do
74
- it "is just Object when no namespace is given" do
75
- constant_finder = ConstLookup.new
76
- expect(constant_finder.lookup_path).to eq [Object]
77
- end
78
-
79
88
  it "is a list of each constant in the namespace in ascending order" do
80
89
  stub_const('Foo::Bar::Baz', Module.new)
81
90
  constant_finder = ConstLookup.new(Foo::Bar::Baz)
@@ -43,4 +43,26 @@ describe "README examples" do
43
43
  expect(A::C.const_lookup('D')).to eq D
44
44
  end
45
45
  end
46
+
47
+ describe "example 3" do
48
+ before do
49
+ module A
50
+ module B; end
51
+ module C; end
52
+ end
53
+ module D; end
54
+ end
55
+
56
+ after do
57
+ Object.send :remove_const, :A
58
+ Object.send :remove_const, :D
59
+ end
60
+
61
+ it "works" do
62
+ expect(A::C.const_get('A')).to eq A
63
+ expect{A::C.const_get('B')}.to raise_error NameError, %q(uninitialized constant A::C::B)
64
+ expect(A::C.const_get('D')).to eq D
65
+ expect{A::C.const_get('E')}.to raise_error NameError, %q(uninitialized constant A::C::E)
66
+ end
67
+ end
46
68
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: const_lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Marshall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-09 00:00:00.000000000 Z
11
+ date: 2013-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler