consty 0.1.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.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +19 -0
- data/consty.gemspec +33 -0
- data/lib/consty.rb +35 -0
- data/lib/consty/version.rb +3 -0
- data/spec/consty_spec.rb +84 -0
- data/spec/coverage_helper.rb +5 -0
- data/spec/minitest_helper.rb +5 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7330601bbe951532411c62d3c4f0e7d6318601be
|
4
|
+
data.tar.gz: 2f0b7891ce58b30e59fbadf0b49229b1a1622576
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7e7aacbd598382693629ab700bbe68821f775d8410bdc23d824c0a659930f19545af6278c3d6d280e81e3cccbfbab65392adb3b385a2a98e5f0a295149c9a0d
|
7
|
+
data.tar.gz: 9268635dbcaffd5110fc7e2ebde467e3fae49c827a0121f7b0c498f1bf01e6d199a8e084cda9be3a1427ea79d17f0bcc84cdb04f99d0d7cc75e3537d2cd0652a
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
consty
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Gabriel Naiman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Consty
|
2
|
+
|
3
|
+
[](https://rubygems.org/gems/consty)
|
4
|
+
[](https://travis-ci.org/gabynaiman/consty)
|
5
|
+
[](https://coveralls.io/github/gabynaiman/consty?branch=master)
|
6
|
+
[](https://codeclimate.com/github/gabynaiman/consty)
|
7
|
+
[](https://gemnasium.com/gabynaiman/consty)
|
8
|
+
|
9
|
+
Convert strings and symbols to constants in specific namespace
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'consty'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install consty
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
VAL = 0
|
31
|
+
module Foo
|
32
|
+
VAL = 1
|
33
|
+
class Bar
|
34
|
+
VAL = 10
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Consty.get 'VAL' # => VAL
|
39
|
+
Consty.get 'Foo::Bar' # => Foo::Bar
|
40
|
+
Consty.get 'VAL', Foo # => Foo::VAL
|
41
|
+
Consty.get 'Bar', Foo # => Foo::Bar
|
42
|
+
Consty.get 'VAL', Foo::Bar # => Foo::Bar::VAL
|
43
|
+
Consty.get '::VAL', Foo::Bar # => VAL
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/consty.
|
49
|
+
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
54
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:spec) do |t|
|
5
|
+
t.libs << 'spec'
|
6
|
+
t.pattern = 'spec/**/*_spec.rb'
|
7
|
+
t.verbose = false
|
8
|
+
t.warning = false
|
9
|
+
end
|
10
|
+
|
11
|
+
task default: :spec
|
12
|
+
|
13
|
+
desc 'Pry console'
|
14
|
+
task :console do
|
15
|
+
require 'consty'
|
16
|
+
require 'pry'
|
17
|
+
ARGV.clear
|
18
|
+
Pry.start
|
19
|
+
end
|
data/consty.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'consty/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'consty'
|
8
|
+
spec.version = Consty::VERSION
|
9
|
+
spec.authors = ['Gabriel Naiman']
|
10
|
+
spec.email = ['gnaiman@keepcon.com']
|
11
|
+
spec.summary = 'Convert strings and symbols to constants in specific namespace'
|
12
|
+
spec.description = 'Convert strings and symbols to constants in specific namespace'
|
13
|
+
spec.homepage = 'https://github.com/gabynaiman/consty'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.add_development_dependency 'bundler', '~> 1.12'
|
22
|
+
spec.add_development_dependency 'rake', '~> 11.0'
|
23
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
24
|
+
spec.add_development_dependency 'minitest-colorin', '~> 0.1'
|
25
|
+
spec.add_development_dependency 'simplecov', '~> 0.12'
|
26
|
+
spec.add_development_dependency 'coveralls', '~> 0.8'
|
27
|
+
spec.add_development_dependency 'pry-nav', '~> 0.2'
|
28
|
+
|
29
|
+
if RUBY_VERSION < '2'
|
30
|
+
spec.add_development_dependency 'tins', '~> 1.6.0'
|
31
|
+
spec.add_development_dependency 'json', '~> 1.8'
|
32
|
+
end
|
33
|
+
end
|
data/lib/consty.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative 'consty/version'
|
2
|
+
|
3
|
+
class Consty
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def get(name, namespace=Object)
|
8
|
+
current_namespace = namespace
|
9
|
+
while current_namespace do
|
10
|
+
begin
|
11
|
+
return secuential_get name, current_namespace
|
12
|
+
rescue NameError
|
13
|
+
namespace_name = current_namespace.name.split('::')[0..-2].join('::')
|
14
|
+
current_namespace = namespace_name.empty? ? nil : secuential_get(namespace_name)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
namespace.const_missing name
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def secuential_get(name, namespace=Object)
|
23
|
+
name_sections = name.to_s.split('::')
|
24
|
+
|
25
|
+
if name_sections.first.empty?
|
26
|
+
namespace = Object
|
27
|
+
name_sections = name_sections[1..-1]
|
28
|
+
end
|
29
|
+
|
30
|
+
name_sections.inject(namespace) { |c,n| c.const_get n }
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/spec/consty_spec.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
VAL = 1
|
4
|
+
|
5
|
+
module Foo
|
6
|
+
VAL = 2
|
7
|
+
|
8
|
+
class Bar
|
9
|
+
VAL = 3
|
10
|
+
|
11
|
+
def self.const_missing(name)
|
12
|
+
name.to_s == 'Const' ? Baz : super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Baz < Bar; end
|
17
|
+
|
18
|
+
def self.const_missing(name)
|
19
|
+
name.to_s == 'Autoloaded' ? Bar : super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe Consty do
|
24
|
+
|
25
|
+
describe 'Unscoped' do
|
26
|
+
|
27
|
+
it 'Get from string' do
|
28
|
+
Consty.get('VAL').must_equal VAL
|
29
|
+
Consty.get('Foo').must_equal Foo
|
30
|
+
Consty.get('Foo::VAL').must_equal Foo::VAL
|
31
|
+
Consty.get('Foo::Bar').must_equal Foo::Bar
|
32
|
+
Consty.get('Foo::Bar::VAL').must_equal Foo::Bar::VAL
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'Get from symbol' do
|
36
|
+
Consty.get('VAL'.to_sym).must_equal VAL
|
37
|
+
Consty.get('Foo'.to_sym).must_equal Foo
|
38
|
+
Consty.get('Foo::VAL'.to_sym).must_equal Foo::VAL
|
39
|
+
Consty.get('Foo::Bar'.to_sym).must_equal Foo::Bar
|
40
|
+
Consty.get('Foo::Bar::VAL'.to_sym).must_equal Foo::Bar::VAL
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'Explicit :: prefix (::SomeClass)' do
|
44
|
+
Consty.get('::Foo').must_equal Foo
|
45
|
+
Consty.get('::Foo::Bar').must_equal Foo::Bar
|
46
|
+
Consty.get('::Foo::Bar::VAL').must_equal Foo::Bar::VAL
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'NameError for undefined constant' do
|
50
|
+
proc { Consty.get '' }.must_raise NameError
|
51
|
+
proc { Consty.get 'UndefinedConstant' }.must_raise NameError
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'Search in ancestors' do
|
55
|
+
Consty.get('Foo::Baz::VAL').must_equal Foo::Baz::VAL
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'Searches in const_missing (autoload support)' do
|
59
|
+
Consty.get('Foo::Autoloaded::Const').must_equal Foo::Baz
|
60
|
+
Consty.get('Foo::Bar::Const').must_equal Foo::Baz
|
61
|
+
proc { Consty.get 'Foo::UndefinedConstant' }.must_raise NameError
|
62
|
+
proc { Consty.get 'Foo::Bar::UndefinedConstant' }.must_raise NameError
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'Namespace scoped' do
|
68
|
+
|
69
|
+
it 'Get closest constant' do
|
70
|
+
Consty.get('VAL', Foo).must_equal Foo::VAL
|
71
|
+
Consty.get('Bar', Foo).must_equal Foo::Bar
|
72
|
+
Consty.get('Bar::VAL', Foo).must_equal Foo::Bar::VAL
|
73
|
+
Consty.get('VAL', Foo::Bar).must_equal Foo::Bar::VAL
|
74
|
+
Consty.get('Bar', Foo::Baz).must_equal Foo::Bar
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'Explicit :: prefix (::SomeClass)' do
|
78
|
+
Consty.get('::VAL', Foo).must_equal VAL
|
79
|
+
Consty.get('::VAL', Foo::Bar).must_equal VAL
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: consty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Naiman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-16 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '11.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '11.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-colorin
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.12'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.12'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: coveralls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.8'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.8'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry-nav
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.2'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.2'
|
111
|
+
description: Convert strings and symbols to constants in specific namespace
|
112
|
+
email:
|
113
|
+
- gnaiman@keepcon.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".coveralls.yml"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".ruby-gemset"
|
121
|
+
- ".ruby-version"
|
122
|
+
- ".travis.yml"
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- consty.gemspec
|
128
|
+
- lib/consty.rb
|
129
|
+
- lib/consty/version.rb
|
130
|
+
- spec/consty_spec.rb
|
131
|
+
- spec/coverage_helper.rb
|
132
|
+
- spec/minitest_helper.rb
|
133
|
+
homepage: https://github.com/gabynaiman/consty
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.5.1
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: Convert strings and symbols to constants in specific namespace
|
157
|
+
test_files:
|
158
|
+
- spec/consty_spec.rb
|
159
|
+
- spec/coverage_helper.rb
|
160
|
+
- spec/minitest_helper.rb
|