flyingmachine-aikidoka 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ aikidoka-*.gem
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "aikidoka"
5
+ gemspec.summary = "Avoid namespace collisions"
6
+ gemspec.email = "daniel@flyingmachinestudios.com"
7
+ gemspec.homepage = "http://github.com/flyingmachine/aikidoka"
8
+ gemspec.description = "Avoid namespace collisions"
9
+ gemspec.authors = ["Daniel Higginbotham"]
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/aikidoka.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{aikidoka}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Daniel Higginbotham"]
9
+ s.date = %q{2009-05-10}
10
+ s.description = %q{Avoid namespace collisions}
11
+ s.email = %q{daniel@flyingmachinestudios.com}
12
+ s.files = [
13
+ ".gitignore",
14
+ "Rakefile",
15
+ "VERSION",
16
+ "aikidoka.gemspec",
17
+ "lib/aikidoka.rb",
18
+ "spec/aikidoka_spec.rb"
19
+ ]
20
+ s.has_rdoc = true
21
+ s.homepage = %q{http://github.com/flyingmachine/aikidoka}
22
+ s.rdoc_options = ["--charset=UTF-8"]
23
+ s.require_paths = ["lib"]
24
+ s.rubygems_version = %q{1.3.1}
25
+ s.summary = %q{Avoid namespace collisions}
26
+ s.test_files = [
27
+ "spec/aikidoka_spec.rb"
28
+ ]
29
+
30
+ if s.respond_to? :specification_version then
31
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
32
+ s.specification_version = 2
33
+
34
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
35
+ else
36
+ end
37
+ else
38
+ end
39
+ end
data/lib/aikidoka.rb ADDED
@@ -0,0 +1,67 @@
1
+ module Aikidoka
2
+ # Keys should be old names of classes/modules to rename,
3
+ # Values should be new names
4
+ def self.rename(pairs = {})
5
+ hide_existing_constants(*pairs.keys)
6
+ yield
7
+ create_modules(*pairs.values)
8
+ copy_constants(pairs)
9
+ remove_constants(*pairs.keys)
10
+ unhide_constants(*pairs.keys)
11
+ end
12
+
13
+ def self.create_modules(*constant_names)
14
+ constant_names.uniq.each do |name|
15
+ pieces = name.split("::")
16
+ pieces.pop
17
+ parent_constant = Object
18
+ pieces.each do |piece|
19
+ if parent_constant.const_defined?(piece)
20
+ parent_constant = parent_constant.const_get(piece)
21
+ else
22
+ new_module = Module.new
23
+ parent_constant.const_set(piece, new_module)
24
+ parent_constant = new_module
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.copy_constants(pairs)
31
+ pairs.each do |old_name, new_name|
32
+ parent_constant = Object
33
+ namespace_pieces = new_name.split("::")
34
+ new_constant_name = namespace_pieces.pop
35
+
36
+ namespace_pieces.each do |piece|
37
+ parent_constant = parent_constant.const_get(piece)
38
+ end
39
+ parent_constant.const_set(new_constant_name, Object.class_eval(old_name))
40
+ end
41
+ end
42
+
43
+ def self.remove_constants(*constant_names)
44
+ constant_names.each do |name|
45
+ Object.send(:remove_const, name)
46
+ end
47
+ end
48
+
49
+ def self.hide_existing_constants(*constant_names)
50
+ constant_names.each do |name|
51
+ if(Object.const_defined? name)
52
+ Object.const_set("Aikidoka#{name}", Object.const_get(name))
53
+ Object.send(:remove_const, name)
54
+ end
55
+ end
56
+ end
57
+
58
+ def self.unhide_constants(*constant_names)
59
+ constant_names.each do |name|
60
+ hidden_name = "Aikidoka#{name}"
61
+ if(Object.const_defined? hidden_name)
62
+ Object.const_set(name, Object.const_get(hidden_name))
63
+ Object.send(:remove_const, hidden_name)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,79 @@
1
+ require 'aikidoka'
2
+
3
+ describe Aikidoka do
4
+ it "should create modules/classes for all but most-nested constant name" do
5
+ Object.const_defined?(:Weapons).should be_false
6
+ Aikidoka.create_modules("Weapons::Bokken")
7
+ Object.const_defined?(:Weapons).should be_true
8
+ Weapons.const_defined?(:Bokken).should be_false
9
+
10
+ remove_constant_hierarchy("Weapons")
11
+ end
12
+
13
+ it "should create modules/classes for all but most-nested constant name when constants with those names exist" do
14
+ module Aikido; class Weapons; class Bokken; end end end
15
+ Aikidoka.create_modules("Aikido::Weapons::Bokken")
16
+
17
+ remove_constant_hierarchy("Aikido::Weapons")
18
+ end
19
+
20
+ it "should remove constants" do
21
+ module Weapons; end
22
+ Object.const_defined?(:Weapons).should be_true
23
+ Aikidoka.remove_constants(:Weapons)
24
+ Object.const_defined?(:Weapons).should be_false
25
+ end
26
+
27
+ it "should rename a top-level constant" do
28
+ Aikidoka.rename("Weapons" => "Aikido::Weapons") do
29
+ module Weapons
30
+ def strike
31
+ end
32
+ end
33
+ end
34
+ Object.const_defined?(:Weapons).should be_false
35
+ Object.const_defined?(:Aikido).should be_true
36
+ Aikido.const_defined?(:Weapons).should be_true
37
+ Aikido::Weapons.method_defined?(:strike).should be_true
38
+
39
+ remove_constant_hierarchy("Aikido::Weapons")
40
+ end
41
+
42
+ it "should not overwrite existing constant" do
43
+ module Weapons
44
+ def block
45
+ end
46
+ end
47
+
48
+
49
+ Aikidoka.rename("Weapons" => "Aikido::Weapons") do
50
+ module Weapons
51
+ def strike
52
+ end
53
+ end
54
+ end
55
+
56
+ Object.const_defined?(:Weapons).should be_true
57
+ Object.const_defined?(:Aikido).should be_true
58
+ Aikido.const_defined?(:Weapons).should be_true
59
+ Aikido::Weapons.method_defined?(:strike).should be_true
60
+ Aikido::Weapons.method_defined?(:block).should be_false
61
+ Weapons.method_defined?(:block).should be_true
62
+ Weapons.method_defined?(:strike).should be_false
63
+
64
+ remove_constant_hierarchy("Aikido::Weapons", "Weapons")
65
+ end
66
+ end
67
+
68
+ # each name is i.e. Aikido::Weapons::Bokken
69
+ def remove_constant_hierarchy(*names)
70
+ names.each do |name|
71
+ pieces = name.split("::")
72
+ pieces.size.downto(1).each do |size|
73
+ to_remove = pieces.pop
74
+ const_name = pieces.join("::")
75
+ parent = Object.class_eval(const_name) || Object
76
+ parent.send(:remove_const, to_remove)
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flyingmachine-aikidoka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Higginbotham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Avoid namespace collisions
17
+ email: daniel@flyingmachinestudios.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - .gitignore
26
+ - Rakefile
27
+ - VERSION
28
+ - aikidoka.gemspec
29
+ - lib/aikidoka.rb
30
+ - spec/aikidoka_spec.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/flyingmachine/aikidoka
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --charset=UTF-8
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: Avoid namespace collisions
57
+ test_files:
58
+ - spec/aikidoka_spec.rb