constantine 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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 Jason L Perry
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,15 @@
1
+ # Constantine
2
+
3
+ ``` ruby
4
+ module Support
5
+ class Ticket
6
+ end
7
+ end
8
+
9
+ > Constantine.constantize("SupportTicket")
10
+ => Support::Ticket
11
+ ```
12
+
13
+ # Copyright
14
+
15
+ Copyright (c) 2011 Jason L Perry. See LICENSE for details.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new :test do |test|
5
+ test.libs << "lib"
6
+ test.pattern = "test/*_test.rb"
7
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "constantine/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "constantine"
7
+ s.version = Constantine::VERSION
8
+ s.authors = ["Jason L Perry"]
9
+ s.email = ["jasper@ambethia.com"]
10
+ s.homepage = "http://github.com/ambethia/constantine"
11
+ s.summary = %q{Experimental constantizing}
12
+ s.description = %q{Looks further than the obvious when trying to create a constant from a string}
13
+
14
+ s.rubyforge_project = "constantine"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "minitest"
23
+ s.add_development_dependency "turn"
24
+ end
@@ -0,0 +1,50 @@
1
+ require "constantine/version"
2
+
3
+ module Constantine
4
+
5
+ class << self
6
+
7
+ def constantize(word)
8
+ modules = []
9
+ variations(split(word)) { |m| modules << m }
10
+ modules.uniq!
11
+ modules.each do |potential_name|
12
+ names = potential_name.split('::')
13
+ names.shift if names.empty? || names.first.empty?
14
+ constant = Object
15
+ names.each do |name|
16
+ constant = constant.const_get(name) if constant.const_defined?(name)
17
+ return constant if constant.name == potential_name
18
+ end
19
+ end
20
+ Object.const_missing(word)
21
+ end
22
+
23
+ def variations(words, prefix = [], &block)
24
+ (words.length-1).downto(0).each do |count|
25
+ pre = words[0..count].join("")
26
+ post = words[count+1..words.length-1]
27
+ yield [prefix + [pre], post].reject(&:empty?).join("::")
28
+ variations(post, prefix + [pre], &block)
29
+ end
30
+ end
31
+
32
+ def split(word)
33
+ word = word.dup
34
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1 \2')
35
+ word.gsub!(/([a-z\d])([A-Z])/, '\1 \2')
36
+ word.split(" ")
37
+ end
38
+ end
39
+
40
+ # def self.constantize(camel_cased_word)
41
+ # names = camel_cased_word.split('::')
42
+ # names.shift if names.empty? || names.first.empty?
43
+ #
44
+ # constant = Object
45
+ # names.each do |name|
46
+ # constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
47
+ # end
48
+ # constant
49
+ # end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Constantine
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,102 @@
1
+ require "minitest/unit"
2
+ require "minitest/autorun"
3
+ require "turn" unless ENV['TM_RUBY']
4
+
5
+ require "constantine"
6
+
7
+ class ContantineTest < MiniTest::Unit::TestCase
8
+
9
+ def test_vanilla_constantization
10
+ define "AlphaBravo" do
11
+ assert_equal AlphaBravo, Constantine.constantize("AlphaBravo")
12
+ end
13
+ end
14
+
15
+ def test_vanilla_nested_constantization
16
+ define "Alpha::Bravo" do
17
+ assert_equal Alpha::Bravo, Constantine.constantize("Alpha::Bravo")
18
+ end
19
+ end
20
+
21
+ def test_nested_constantization
22
+ define "Alpha::Bravo" do
23
+ assert_equal Alpha::Bravo, Constantine.constantize("AlphaBravo")
24
+ end
25
+ end
26
+
27
+ def test_nested_constantization_variation_one
28
+ define "Alpha::BravoCharlie" do
29
+ assert_equal Alpha::BravoCharlie, Constantine.constantize("AlphaBravoCharlie")
30
+ end
31
+ end
32
+
33
+ def test_nested_constantization_variation_two
34
+ define "AlphaBravo::Charlie" do
35
+ assert_equal AlphaBravo::Charlie,
36
+ Constantine.constantize("AlphaBravoCharlie")
37
+ end
38
+ end
39
+
40
+ def test_nested_constantization_variation_three
41
+ define "Alpha::Bravo::Charlie" do
42
+ assert_equal Alpha::Bravo::Charlie,
43
+ Constantine.constantize("AlphaBravoCharlie")
44
+ end
45
+ end
46
+
47
+ def test_nested_constantization_variation_four
48
+ define "Alpha::Bravo::Charlie" do
49
+ assert_equal Alpha::Bravo::Charlie,
50
+ Constantine.constantize("Alpha::BravoCharlie")
51
+ end
52
+ end
53
+
54
+ def test_nested_constantization_on_needlessly_lengthy_variation
55
+ define "Alpha::BravoCharlie::Delta::EchoFoxtrot" do
56
+ assert_equal Alpha::BravoCharlie::Delta::EchoFoxtrot,
57
+ Constantine.constantize("AlphaBravoCharlieDeltaEchoFoxtrot")
58
+ end
59
+ end
60
+
61
+ def test_raises_name_error_on_missing_constant
62
+ assert_raises NameError do
63
+ Constantine.constantize("Alpha::Bravo")
64
+ end
65
+ end
66
+
67
+ def test_define_helper_defines_constant
68
+ define "AlphaBravo" do
69
+ assert AlphaBravo
70
+ end
71
+ end
72
+
73
+ def test_define_helper_undefines_constant
74
+ define "AlphaBravo" do
75
+ end
76
+ refute Object.const_defined? "AlphaBravo"
77
+ end
78
+
79
+ def test_define_helper_defines_nested_constant
80
+ define "Alpha::Bravo" do
81
+ assert Alpha::Bravo
82
+ end
83
+ end
84
+
85
+ def test_define_helper_undefines_nested_constant
86
+ define "Alpha::Bravo" do
87
+ end
88
+ refute Object.const_defined? "Alpha"
89
+ end
90
+
91
+ private
92
+
93
+ def define(full_name)
94
+ modules = full_name.split("::")
95
+ modules.inject(Object) do |klass, name|
96
+ klass.const_set(name, Module.new)
97
+ yield if name == modules.last
98
+ klass.const_get(name)
99
+ end
100
+ Object.send(:remove_const, modules.first)
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: constantine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason L Perry
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70162477510360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70162477510360
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70162477509920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70162477509920
36
+ - !ruby/object:Gem::Dependency
37
+ name: turn
38
+ requirement: &70162477509480 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70162477509480
47
+ description: Looks further than the obvious when trying to create a constant from
48
+ a string
49
+ email:
50
+ - jasper@ambethia.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - constantine.gemspec
61
+ - lib/constantine.rb
62
+ - lib/constantine/version.rb
63
+ - test/contantine_test.rb
64
+ homepage: http://github.com/ambethia/constantine
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ segments:
77
+ - 0
78
+ hash: -1520710909723818534
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ segments:
86
+ - 0
87
+ hash: -1520710909723818534
88
+ requirements: []
89
+ rubyforge_project: constantine
90
+ rubygems_version: 1.8.11
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Experimental constantizing
94
+ test_files:
95
+ - test/contantine_test.rb