namespaces 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ ## EXAMPLE
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'shoulda'
6
+
7
+ require File.expand_path('../../lib/namespaces.rb', __FILE__)
8
+
9
+ module A
10
+ module B
11
+ module C
12
+ class X
13
+ include Namespaces
14
+ extend Namespaces
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ class ModulesTest < Test::Unit::TestCase
21
+ should "return modules array when called as class method" do
22
+ assert_equal [A::B::C, A::B, A], A::B::C::X.namespaces
23
+ end
24
+
25
+ should "return modules array when called as instance method" do
26
+ assert_equal [A::B::C, A::B, A], A::B::C::X.new.namespaces
27
+ end
28
+ end
29
+
30
+
31
+ ## USE CASES WHEN ONE MIGHT NEED IT
32
+
data/Rakefile CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rake/rdoctask'
4
3
 
5
4
  Rake::TestTask.new do |t|
6
5
  t.libs << "test"
@@ -8,17 +7,4 @@ Rake::TestTask.new do |t|
8
7
  t.verbose = true
9
8
  end
10
9
 
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
10
+ task :default => "test"
@@ -1,27 +1,22 @@
1
- require 'rubygems'
2
- require 'namespaces'
1
+ require 'rubygems'
2
+ require 'namespaces'
3
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
4
+ module Base
5
+ class Service
6
+ include Namespaces
7
+ def atom_header
8
+ header = {}
9
+ header["GData-Version"] = namespaces.first::API_VERSION
10
+ header
14
11
  end
12
+ end
13
+ end
15
14
 
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
15
 
16
+ module Contacts
17
+ API_VERSION = "1.0"
18
+ class Service < Base::Service
19
+ end
20
+ end
22
21
 
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
22
+ puts Contacts::Service.new.atom_header.inspect # -> {"GData-Version"=>"1.0"}
@@ -1,13 +1,13 @@
1
1
  module Namespaces
2
2
  def namespaces
3
3
  name = self.is_a?(Class) ? self.name : self.class.name
4
- namespaces = []
4
+ nesting = []
5
5
  name.split(/::/)[0..-2].inject([]) do |parts, name|
6
6
  fq_name = parts << name
7
- namespaces << eval(fq_name.join("::"))
7
+ nesting << eval(fq_name.join("::"))
8
8
  fq_name
9
9
  end
10
- namespaces
10
+ nesting.reverse
11
11
  end
12
12
  end
13
13
 
@@ -2,12 +2,12 @@ require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
4
 
5
- require File.expand_path(File.dirname(__FILE__)+ '/../lib/namespaces')
5
+ require File.expand_path('../../lib/namespaces.rb', __FILE__)
6
6
 
7
- module One
8
- module Two
9
- module Three
10
- class A
7
+ module A
8
+ module B
9
+ module C
10
+ class X
11
11
  include Namespaces
12
12
  extend Namespaces
13
13
  end
@@ -17,9 +17,10 @@ end
17
17
 
18
18
  class ModulesTest < Test::Unit::TestCase
19
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
20
+ assert_equal [A::B::C, A::B, A], A::B::C::X.namespaces
21
21
  end
22
+
22
23
  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
+ assert_equal [A::B::C, A::B, A], A::B::C::X.new.namespaces
24
25
  end
25
26
  end
metadata CHANGED
@@ -1,71 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: namespaces
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 25
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 2
9
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
10
11
  platform: ruby
11
12
  authors:
12
- - Fedor Kocherga
13
+ - Aleksandr Furmanov
13
14
  autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-07-04 00:00:00 -05:00
18
+ date: 2010-09-06 00:00:00 -05:00
18
19
  default_executable:
19
- dependencies: []
20
-
21
- description:
22
- email: fkocherga@gmail.com
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Get lexical context of self as array of modules
36
+ email:
37
+ - aleksandr.furmanov@gmail.com
23
38
  executables: []
24
39
 
25
40
  extensions: []
26
41
 
27
- extra_rdoc_files:
28
- - README.html
29
- - README.markdown
42
+ extra_rdoc_files: []
43
+
30
44
  files:
31
- - .gitignore
32
- - README.markdown
33
45
  - Rakefile
34
- - VERSION
35
- - examples/gdata-api.rb
46
+ - README.MD
36
47
  - lib/namespaces.rb
37
- - namespaces.gemspec
38
48
  - test/test.rb
39
- - README.html
49
+ - examples/gdata-api.rb
40
50
  has_rdoc: true
41
- homepage: http://github.com/fkocherga/namespaces
51
+ homepage: http://github.com/afurmanov/namespaces
42
52
  licenses: []
43
53
 
44
54
  post_install_message:
45
- rdoc_options:
46
- - --charset=UTF-8
55
+ rdoc_options: []
56
+
47
57
  require_paths:
48
58
  - lib
49
59
  required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
50
61
  requirements:
51
62
  - - ">="
52
63
  - !ruby/object:Gem::Version
64
+ hash: 3
53
65
  segments:
54
66
  - 0
55
67
  version: "0"
56
68
  required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
57
70
  requirements:
58
71
  - - ">="
59
72
  - !ruby/object:Gem::Version
73
+ hash: 23
60
74
  segments:
61
- - 0
62
- version: "0"
75
+ - 1
76
+ - 3
77
+ - 6
78
+ version: 1.3.6
63
79
  requirements: []
64
80
 
65
81
  rubyforge_project:
66
- rubygems_version: 1.3.6
82
+ rubygems_version: 1.3.7
67
83
  signing_key:
68
84
  specification_version: 3
69
- summary: "Gives access to enclosing modules through #namespaces."
70
- test_files:
71
- - test/test.rb
85
+ summary: Get lexical context of self as array of modules
86
+ test_files: []
87
+
data/.gitignore DELETED
@@ -1,5 +0,0 @@
1
- pkg
2
- doc
3
- Manifest
4
- README.html
5
- *.log
@@ -1,46 +0,0 @@
1
- ## SYNOPSIS
2
-
3
- Provides *#namespaces* method returning list of modules a class declared within. (For explanations why one might ever need this, please see below):
4
-
5
- module One
6
- module Two
7
- module Three
8
- class A
9
- include Namespaces
10
- extend Namespaces
11
- end
12
- end
13
- end
14
- end
15
-
16
- assert_equal [One, One::Two, One::Two::Three], One::Two::Three::A.namespaces
17
- assert_equal [One, One::Two, One::Two::Three], One::Two::Three::A.new.namespaces
18
-
19
- ## Raison d'etre
20
-
21
- Modules are two fold constructs in Ruby - they are namespaces and mixins. Sometimes when several
22
- classes get placed under one namespace (in one module in Ruby) and they share something
23
- it makes a perfect sense to put that thing they share in the same namespace. For example:
24
-
25
- module A
26
- COMMON_CONST = "value common for all classes in A"
27
- class B; puts COMMON_CONST; end
28
- class C; puts COMMON_CONST; end
29
- end
30
-
31
- Now imagine if we building a library where that approach is common, and we want to write some
32
- generic code taking a class as an argument and printing a COMMON_CONST for module where that
33
- class is defined:
34
-
35
- def generic_code(klass)
36
- # How to do this? - klass.what?
37
- end
38
-
39
- In Ruby there is no way to ask class: "Please give modules where you defined in". The *namespaces* gem
40
- helps to bridge the gap:
41
-
42
- def generic_code(klass)
43
- puts klass.namespaces[-1].COMMON_CONST
44
- end
45
-
46
-
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.2
@@ -1,47 +0,0 @@
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.2"
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{2010-07-04}
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.6}
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
-