namespaces 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.
- data/.gitignore +5 -0
- data/README.markdown +45 -0
- data/Rakefile +24 -0
- data/VERSION +1 -0
- data/examples/gdata-api.rb +27 -0
- data/lib/namespaces.rb +13 -0
- data/namespaces.gemspec +47 -0
- data/test/test.rb +25 -0
- metadata +64 -0
data/README.markdown
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
## SYNOPSIS
|
2
|
+
The purpose of namespace is probably not just to define a scope for classes,
|
3
|
+
I think there are cases when it could be used as placeholder of something
|
4
|
+
shared among these classes. The generic code accessing whatever is shared
|
5
|
+
could use #namespaces method, which just returns Array of modules enclosing
|
6
|
+
the callee. Here is an example of shared constant XML_NAMESPACE and generic
|
7
|
+
code in Atom::Base class:
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'namespaces'
|
11
|
+
|
12
|
+
module Atom
|
13
|
+
XML_NAMESPACE = 'http://www.w3.org/2005/Atom'
|
14
|
+
class Base
|
15
|
+
extend Namespaces
|
16
|
+
def self.inherited(klass) # generic code:
|
17
|
+
puts klass.namespaces[-1]::XML_NAMESPACE
|
18
|
+
end
|
19
|
+
end
|
20
|
+
class Entry < Base; end
|
21
|
+
class Feed < Base; end
|
22
|
+
end
|
23
|
+
|
24
|
+
module GData
|
25
|
+
XML_NAMESPACE = 'http://schemas.google.com/g/2005'
|
26
|
+
class Entry < Atom::Entry; end
|
27
|
+
class Feed < Atom::Feed; end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
module GCalendar
|
32
|
+
XML_NAMESPACE = "http://schemas.google.com/gCal/2005"
|
33
|
+
class Entry < GData::Entry; end
|
34
|
+
class Feed < GData::Feed; end
|
35
|
+
end
|
36
|
+
|
37
|
+
The output is:
|
38
|
+
|
39
|
+
http://www.w3.org/2005/Atom
|
40
|
+
http://www.w3.org/2005/Atom
|
41
|
+
http://schemas.google.com/g/2005
|
42
|
+
http://schemas.google.com/g/2005
|
43
|
+
http://schemas.google.com/gCal/2005
|
44
|
+
http://schemas.google.com/gCal/2005
|
45
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.test_files = FileList['test/test.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |s|
|
14
|
+
s.name = "namespaces"
|
15
|
+
s.summary = "Gives access to enclosing modules through #namespaces."
|
16
|
+
s.email = "fkocherga@gmail.com"
|
17
|
+
s.homepage = "http://github.com/fkocherga/namespaces"
|
18
|
+
s.authors = ["Fedor Kocherga"]
|
19
|
+
s.test_files = ['test/test.rb']
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
24
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'namespaces'
|
3
|
+
|
4
|
+
module Atom
|
5
|
+
XML_NAMESPACE = 'http://www.w3.org/2005/Atom'
|
6
|
+
class Base
|
7
|
+
extend Namespaces
|
8
|
+
def self.inherited(klass) # generic code:
|
9
|
+
puts klass.namespaces[-1]::XML_NAMESPACE
|
10
|
+
end
|
11
|
+
end
|
12
|
+
class Entry < Base; end
|
13
|
+
class Feed < Base; end
|
14
|
+
end
|
15
|
+
|
16
|
+
module GData
|
17
|
+
XML_NAMESPACE = 'http://schemas.google.com/g/2005'
|
18
|
+
class Entry < Atom::Entry; end
|
19
|
+
class Feed < Atom::Feed; end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
module GCalendar
|
24
|
+
XML_NAMESPACE = "http://schemas.google.com/gCal/2005"
|
25
|
+
class Entry < GData::Entry; end
|
26
|
+
class Feed < GData::Feed; end
|
27
|
+
end
|
data/lib/namespaces.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Namespaces
|
2
|
+
def namespaces
|
3
|
+
name = self.is_a?(Class) ? self.name : self.class.name
|
4
|
+
namespaces = []
|
5
|
+
name.split(/::/)[0..-2].inject([]) do |parts, name|
|
6
|
+
fq_name = parts << name
|
7
|
+
namespaces << eval(fq_name.join("::"))
|
8
|
+
fq_name
|
9
|
+
end
|
10
|
+
namespaces
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
data/namespaces.gemspec
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{namespaces}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Fedor Kocherga"]
|
12
|
+
s.date = %q{2009-12-28}
|
13
|
+
s.email = %q{fkocherga@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.html",
|
16
|
+
"README.markdown"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"README.markdown",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"examples/gdata-api.rb",
|
24
|
+
"lib/namespaces.rb",
|
25
|
+
"namespaces.gemspec",
|
26
|
+
"test/test.rb"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/fkocherga/namespaces}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.5}
|
32
|
+
s.summary = %q{Gives access to enclosing modules through #namespaces.}
|
33
|
+
s.test_files = [
|
34
|
+
"test/test.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
else
|
43
|
+
end
|
44
|
+
else
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
data/test/test.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
require File.expand_path(File.dirname(__FILE__)+ '/../lib/namespaces')
|
6
|
+
|
7
|
+
module One
|
8
|
+
module Two
|
9
|
+
module Three
|
10
|
+
class A
|
11
|
+
include Namespaces
|
12
|
+
extend Namespaces
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class ModulesTest < Test::Unit::TestCase
|
19
|
+
should "return modules array when called as class method" do
|
20
|
+
assert_equal [One, One::Two, One::Two::Three], One::Two::Three::A.namespaces
|
21
|
+
end
|
22
|
+
should "return modules array when called as instance method" do
|
23
|
+
assert_equal [One, One::Two, One::Two::Three], One::Two::Three::A.new.namespaces
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: namespaces
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fedor Kocherga
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-28 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: fkocherga@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.html
|
24
|
+
- README.markdown
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- README.markdown
|
28
|
+
- Rakefile
|
29
|
+
- VERSION
|
30
|
+
- examples/gdata-api.rb
|
31
|
+
- lib/namespaces.rb
|
32
|
+
- namespaces.gemspec
|
33
|
+
- test/test.rb
|
34
|
+
- README.html
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/fkocherga/namespaces
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: "Gives access to enclosing modules through #namespaces."
|
63
|
+
test_files:
|
64
|
+
- test/test.rb
|