const_lookup 0.3.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1458ea9888b8c803ec0caa1f836100e7804b5b3c
4
+ data.tar.gz: 7ed3c09d749f078e480289026678abb74e684038
5
+ SHA512:
6
+ metadata.gz: 668cb854b6887322fc9da2175f428451b4dcdb42e20b13013a813bd57bba237531e1057c41dc24c33db5f540b1eb468d3f5be097ece4d781d4c8b663b585a010
7
+ data.tar.gz: eacb2acf1eaf9c967cfca7c02f3e0d7da7615fe4802f324349e5bf5a85c3f755dede4d0cde85b4b5a0718d702dcdeaeef03e0d753a8e0571ff58860f62178887
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in const_lookup.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Marshall <http://johnandrewmarshall.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.
@@ -0,0 +1,40 @@
1
+ # ConstLookup
2
+
3
+ [![Build Status](https://secure.travis-ci.org/amarshall/const_lookup.png?branch=master)](http://travis-ci.org/amarshall/const_lookup)
4
+
5
+ Makes resolving a constant in a given namespace easy.
6
+
7
+ ## Installation
8
+
9
+ Install as usual: `gem install const_lookup` or add `gem 'const_lookup'` to your Gemfile. Note that Ruby 2.0 is required.
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ module A
15
+ module B; end
16
+ module C; end
17
+ end
18
+ module D; end
19
+
20
+ require 'const_lookup'
21
+
22
+ ConstLookup.lookup('A', A::C) #=> A
23
+ ConstLookup.lookup('B', A::C) #=> A::B
24
+ ConstLookup.lookup('D', A::C) #=> D
25
+ ConstLookup.lookup('E', A::C) #=> #<NameError: Failed to find `E' in A::C>
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ Contributions are welcome. Please be sure that your pull requests are atomic so they can be considered and accepted separately.
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
37
+
38
+ ## Credits & License
39
+
40
+ Copyright © 2013 J. Andrew Marshall. License is available in the LICENSE file.
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Run specs'
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.ruby_opts = '-w' unless ENV['TRAVIS']
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'const_lookup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'const_lookup'
8
+ spec.version = ConstLookup::VERSION
9
+ spec.authors = ["Andrew Marshall"]
10
+ spec.email = ["andrew@johnandrewmarshall.com"]
11
+ spec.description = %q{Makes resolving a constant in a given namespace easy.}
12
+ spec.summary = %q{Makes resolving a constant in a given namespace easy.}
13
+ spec.homepage = 'http://johnandrewmarshall.com/projects/const_lookup'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ end
@@ -0,0 +1,33 @@
1
+ require 'const_lookup/version'
2
+
3
+ class ConstLookup
4
+ def self.lookup name, namespace = nil
5
+ new(*namespace).lookup(name)
6
+ end
7
+
8
+ def initialize namespace = Object
9
+ @namespace = namespace.to_s
10
+ end
11
+
12
+ def lookup name
13
+ name = name.to_sym
14
+ constant = lookup_path.select do |namespace|
15
+ namespace.const_defined? name
16
+ end.map do |namespace|
17
+ namespace.const_get name
18
+ end.sort_by do |constant|
19
+ -constant.to_s.split('::').size
20
+ end.first
21
+ constant or raise NameError, "Failed to find `#{name}' in #@namespace"
22
+ end
23
+
24
+ def lookup_path
25
+ @namespace.split('::').reduce([]) do |lookup_path, part|
26
+ lookup_path << [*lookup_path.last, part]
27
+ end.map do |parts|
28
+ parts.join '::'
29
+ end.map do |constant_name|
30
+ Object.const_get constant_name
31
+ end.reverse
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ class ConstLookup
2
+ VERSION = '0.3.0'
3
+ end
@@ -0,0 +1,85 @@
1
+ require 'const_lookup'
2
+
3
+ describe ConstLookup do
4
+ describe ".lookup" do
5
+ it "creates a new finder with the given namespace and calls lookup with the name" do
6
+ name = double
7
+ namespace = double
8
+ finder = double
9
+ expect(ConstLookup).to receive(:new).with(namespace).and_return finder
10
+ expect(finder).to receive(:lookup).with(name)
11
+
12
+ ConstLookup.lookup name, namespace
13
+ end
14
+
15
+ it "creates the new finder with no args when no namespace given" do
16
+ name = double
17
+ finder = double
18
+ expect(ConstLookup).to receive(:new).with(no_args).and_return finder
19
+ expect(finder).to receive(:lookup).with(name)
20
+
21
+ ConstLookup.lookup name
22
+ end
23
+ end
24
+
25
+ describe "#lookup" do
26
+ it "returns the constant when it resides in the most specific namespace" do
27
+ stub_const('Foo::Bar::Baz', Module.new)
28
+ stub_const('Foo::Bar::Baz::ToFind', Module.new)
29
+ constant_finder = ConstLookup.new(Foo::Bar::Baz)
30
+ expect(constant_finder.lookup(:ToFind)).to eq Foo::Bar::Baz::ToFind
31
+ end
32
+
33
+ it "returns the constant when it's a sibling" do
34
+ stub_const('Foo::Bar::ToFind', Module.new)
35
+ stub_const('Foo::Bar::Baz', Module.new)
36
+ constant_finder = ConstLookup.new(Foo::Bar::Baz)
37
+ expect(constant_finder.lookup(:ToFind)).to eq Foo::Bar::ToFind
38
+ end
39
+
40
+ it "returns the constant when it resides in an intermediate namespace" do
41
+ stub_const('Foo::ToFind', Module.new)
42
+ stub_const('Foo::Bar::Baz', Module.new)
43
+ constant_finder = ConstLookup.new(Foo::Bar::Baz)
44
+ expect(constant_finder.lookup(:ToFind)).to eq Foo::ToFind
45
+ end
46
+
47
+ it "returns the constant when it resides in global namespace" do
48
+ stub_const('ToFind', Module.new)
49
+ stub_const('Foo::Bar::Baz', Module.new)
50
+ constant_finder = ConstLookup.new(Foo::Bar::Baz)
51
+ expect(constant_finder.lookup(:ToFind)).to eq ToFind
52
+ end
53
+
54
+ it "returns the constant in the most specific namespace when it exists in multiple parts of the lookup path" do
55
+ stub_const('ToFind', Module.new)
56
+ stub_const('Foo::ToFind', Module.new)
57
+ stub_const('Foo::Bar::ToFind', Module.new)
58
+ stub_const('Foo::Bar::Baz', Module.new)
59
+ constant_finder = ConstLookup.new(Foo::Bar::Baz)
60
+ expect(constant_finder.lookup(:ToFind)).to eq Foo::Bar::ToFind
61
+ end
62
+
63
+ it "raises a NameError when the constant does not exist" do
64
+ stub_const('Other::ToFind', Module.new)
65
+ stub_const('Foo::Bar::Baz', Module.new)
66
+ constant_finder = ConstLookup.new(Foo::Bar::Baz)
67
+ expect do
68
+ constant_finder.lookup(:ToFind)
69
+ end.to raise_error NameError, %q(Failed to find `ToFind' in Foo::Bar::Baz)
70
+ end
71
+ end
72
+
73
+ 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
+ it "is a list of each constant in the namespace in ascending order" do
80
+ stub_const('Foo::Bar::Baz', Module.new)
81
+ constant_finder = ConstLookup.new(Foo::Bar::Baz)
82
+ expect(constant_finder.lookup_path).to eq [Foo::Bar::Baz, Foo::Bar, Foo]
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,25 @@
1
+ require 'const_lookup'
2
+
3
+ describe "README examples" do
4
+ describe "example 1" do
5
+ before do
6
+ module A
7
+ module B; end
8
+ module C; end
9
+ end
10
+ module D; end
11
+ end
12
+
13
+ after do
14
+ Object.send :remove_const, :A
15
+ Object.send :remove_const, :D
16
+ end
17
+
18
+ it "works" do
19
+ expect(ConstLookup.lookup('A', A::C)).to eq A
20
+ expect(ConstLookup.lookup('B', A::C)).to eq A::B
21
+ expect(ConstLookup.lookup('D', A::C)).to eq D
22
+ expect{ConstLookup.lookup('E', A::C)}.to raise_error NameError, %q(Failed to find `E' in A::C)
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: const_lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Marshall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-09 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Makes resolving a constant in a given namespace easy.
56
+ email:
57
+ - andrew@johnandrewmarshall.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - const_lookup.gemspec
69
+ - lib/const_lookup.rb
70
+ - lib/const_lookup/version.rb
71
+ - spec/const_lookup_spec.rb
72
+ - spec/readme_spec.rb
73
+ homepage: http://johnandrewmarshall.com/projects/const_lookup
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Makes resolving a constant in a given namespace easy.
97
+ test_files:
98
+ - spec/const_lookup_spec.rb
99
+ - spec/readme_spec.rb
100
+ has_rdoc: