namespaces 0.0.1 → 0.0.2

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.
Files changed (4) hide show
  1. data/README.markdown +34 -33
  2. data/VERSION +1 -1
  3. data/namespaces.gemspec +3 -3
  4. metadata +12 -5
data/README.markdown CHANGED
@@ -1,45 +1,46 @@
1
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
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
18
12
  end
19
13
  end
20
- class Entry < Base; end
21
- class Feed < Base; end
22
14
  end
23
15
 
24
- module GData
25
- XML_NAMESPACE = 'http://schemas.google.com/g/2005'
26
- class Entry < Atom::Entry; end
27
- class Feed < Atom::Feed; end
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
28
29
  end
29
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:
30
34
 
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
+ def generic_code(klass)
36
+ # How to do this? - klass.what?
35
37
  end
36
38
 
37
- The output is:
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
38
45
 
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
46
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/namespaces.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{namespaces}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Fedor Kocherga"]
12
- s.date = %q{2009-12-28}
12
+ s.date = %q{2010-07-04}
13
13
  s.email = %q{fkocherga@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.html",
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.homepage = %q{http://github.com/fkocherga/namespaces}
29
29
  s.rdoc_options = ["--charset=UTF-8"]
30
30
  s.require_paths = ["lib"]
31
- s.rubygems_version = %q{1.3.5}
31
+ s.rubygems_version = %q{1.3.6}
32
32
  s.summary = %q{Gives access to enclosing modules through #namespaces.}
33
33
  s.test_files = [
34
34
  "test/test.rb"
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: namespaces
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
5
10
  platform: ruby
6
11
  authors:
7
12
  - Fedor Kocherga
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-12-28 00:00:00 -06:00
17
+ date: 2010-07-04 00:00:00 -05:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -45,18 +50,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
50
  requirements:
46
51
  - - ">="
47
52
  - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
48
55
  version: "0"
49
- version:
50
56
  required_rubygems_version: !ruby/object:Gem::Requirement
51
57
  requirements:
52
58
  - - ">="
53
59
  - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
54
62
  version: "0"
55
- version:
56
63
  requirements: []
57
64
 
58
65
  rubyforge_project:
59
- rubygems_version: 1.3.5
66
+ rubygems_version: 1.3.6
60
67
  signing_key:
61
68
  specification_version: 3
62
69
  summary: "Gives access to enclosing modules through #namespaces."