open_namespace 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg
2
+ doc
3
+ .DS_Store
4
+ .yardoc
5
+ *.swp
6
+ *~
data/ChangeLog.md CHANGED
@@ -1,6 +1,13 @@
1
+ ### 0.4.0 / 2011-11-19
2
+
3
+ * Added {OpenNamespace.const_lookup}.
4
+ * Added {OpenNamespace.const_search}.
5
+ * Added {OpenNamespace::ClassMethods#const_lookup}.
6
+ * Added {OpenNamespace::ClassMethods#const_defined?}.
7
+
1
8
  ### 0.3.2 / 2011-07-12
2
9
 
3
- * Fixed a bug in {OpenNamespace.constant_path} where the argument was
10
+ * Fixed a bug in {OpenNamespace.const_path} where the argument was
4
11
  being modified in-place.
5
12
 
6
13
  ### 0.3.1 / 2011-03-08
@@ -9,7 +16,7 @@
9
16
 
10
17
  ### 0.3.0 / 2010-04-28
11
18
 
12
- * Added {OpenNamespace.constant_path}.
19
+ * Added {OpenNamespace.const_path}.
13
20
  * Added {OpenNamespace::ClassMethods#require_file}.
14
21
  * Added {OpenNamespace::ClassMethods#const_search}.
15
22
  * Improved searching for constants with oddly capitalized names, such as
data/README.md CHANGED
@@ -13,6 +13,7 @@ load plugin modules/classes from other gems.
13
13
 
14
14
  ## Features
15
15
 
16
+ * Provides implicit loading of constants via `const_defined?`.
16
17
  * Provides implicit loading of constants via `const_missing`.
17
18
  * Provides explicit loading of constants via `require_const`.
18
19
 
data/Rakefile CHANGED
@@ -25,7 +25,7 @@ task :test => :spec
25
25
  task :default => :spec
26
26
 
27
27
  begin
28
- gem 'yard', '~> 0.7.0'
28
+ gem 'yard', '~> 0.7'
29
29
  require 'yard'
30
30
 
31
31
  YARD::Rake::YardocTask.new
data/gemspec.yml CHANGED
@@ -13,4 +13,4 @@ has_yard: true
13
13
  development_dependencies:
14
14
  ore-tasks: ~> 0.4
15
15
  rspec: ~> 2.4
16
- yard: ~> 0.7.0
16
+ yard: ~> 0.7
@@ -7,7 +7,7 @@ module OpenNamespace
7
7
  # The file path used to load constants into the namespace.
8
8
  #
9
9
  def namespace_root
10
- @namespace_root ||= OpenNamespace.constant_path(self.name)
10
+ @namespace_root ||= OpenNamespace.const_path(self.name)
11
11
  end
12
12
 
13
13
  #
@@ -79,6 +79,45 @@ module OpenNamespace
79
79
  return true
80
80
  end
81
81
 
82
+ #
83
+ # Checks if a constant is defined or attempts loading the constant.
84
+ #
85
+ # @param [String] name
86
+ # The name of the constant.
87
+ #
88
+ # @param [Boolean] inherit
89
+ # Specifies whether to search the ancestors for the constant.
90
+ #
91
+ # @return [Boolean]
92
+ # Specifies whether the constant is defined.
93
+ #
94
+ def const_defined?(name,*inherit)
95
+ if super(name,*inherit)
96
+ true
97
+ else
98
+ # attempt to load the file that might have the constant
99
+ require_file(OpenNamespace.const_path(name))
100
+
101
+ # check for the constant again
102
+ return super(name,*inherit)
103
+ end
104
+ end
105
+
106
+ #
107
+ # Finds the exact constant.
108
+ #
109
+ # @param [String] name
110
+ # The name of the constant.
111
+ #
112
+ # @return [Object, nil]
113
+ # The exact constant or `nil` if the constant could not be found.
114
+ #
115
+ # @since 0.4.0
116
+ #
117
+ def const_lookup(name)
118
+ OpenNamespace.const_lookup(self,name)
119
+ end
120
+
82
121
  #
83
122
  # Finds the constant with a name similar to the given file name.
84
123
  #
@@ -92,28 +131,7 @@ module OpenNamespace
92
131
  # @since 0.3.0
93
132
  #
94
133
  def const_search(file_name)
95
- names = file_name.to_s.split(/[:\/]+/)
96
- scope = self
97
-
98
- until names.empty?
99
- name = names.shift
100
-
101
- # strip any dashes or underscores
102
- name.tr!('_-','')
103
-
104
- # perform a case insensitive search
105
- const_pattern = /^#{name}$/i
106
-
107
- # grep for the constant
108
- const_name = scope.constants.find { |name| name =~ const_pattern }
109
-
110
- # the constant search failed
111
- return nil unless const_name
112
-
113
- scope = scope.const_get(const_name)
114
- end
115
-
116
- return scope
134
+ OpenNamespace.const_search(self,file_name)
117
135
  end
118
136
 
119
137
  protected
@@ -133,18 +151,9 @@ module OpenNamespace
133
151
  # @see require_const.
134
152
  #
135
153
  def const_missing(name)
136
- file_name = OpenNamespace.constant_path(name)
137
-
138
- if require_file(file_name)
139
- #
140
- # If const_missing is being called, the constant is not present yet.
141
- # Therefor, only check for the constant if the file was
142
- # successfully loaded by require_file.
143
- #
144
- if self.const_defined?(name)
145
- # get the exact constant name that was requested
146
- return self.const_get(name)
147
- end
154
+ if self.const_defined?(name)
155
+ # get the exact constant name that was requested
156
+ return self.const_get(name)
148
157
  end
149
158
 
150
159
  return super(name)
@@ -16,7 +16,9 @@ module OpenNamespace
16
16
  #
17
17
  # @since 0.3.0
18
18
  #
19
- def OpenNamespace.constant_path(name)
19
+ # @api semipublic
20
+ #
21
+ def self.const_path(name)
20
22
  path = name.to_s.dup
21
23
 
22
24
  # back-ported from extlib's String#to_const_path
@@ -31,4 +33,75 @@ module OpenNamespace
31
33
  path.downcase!
32
34
  return path
33
35
  end
36
+
37
+ #
38
+ # Finds the exact constant.
39
+ #
40
+ # @param [Module, Class] scope
41
+ # The scope to begin searching within.
42
+ #
43
+ # @param [String] name
44
+ # The name of the constant.
45
+ #
46
+ # @return [Object, nil]
47
+ # The exact constant or `nil` if the constant could not be found.
48
+ #
49
+ # @since 0.4.0
50
+ #
51
+ # @api semipublic
52
+ #
53
+ def self.const_lookup(scope,name)
54
+ names = name.split('::')
55
+
56
+ until names.empty?
57
+ begin
58
+ scope = scope.const_get(names.shift)
59
+ rescue NameError
60
+ return nil
61
+ end
62
+ end
63
+
64
+ return scope
65
+ end
66
+
67
+ #
68
+ # Finds the constant with a name similar to the given file name.
69
+ #
70
+ # @param [Module, Class] scope
71
+ # The scope to begin searching within.
72
+ #
73
+ # @param [Symbol, String] file_name
74
+ # The file name that represents the constant.
75
+ #
76
+ # @return [Object, nil]
77
+ # Returns the found constants, or `nil` if a `NameError` exception
78
+ # was encountered.
79
+ #
80
+ # @since 0.4.0
81
+ #
82
+ # @api semipublic
83
+ #
84
+ def self.const_search(scope,file_name)
85
+ names = file_name.to_s.split(/[:\/]+/)
86
+
87
+ until names.empty?
88
+ name = names.shift
89
+
90
+ # strip any dashes or underscores
91
+ name.tr!('_-','')
92
+
93
+ # perform a case insensitive search
94
+ const_pattern = /^#{name}$/i
95
+
96
+ # grep for the constant
97
+ const_name = scope.constants.find { |name| name =~ const_pattern }
98
+
99
+ # the constant search failed
100
+ return nil unless const_name
101
+
102
+ scope = scope.const_get(const_name)
103
+ end
104
+
105
+ return scope
106
+ end
34
107
  end
@@ -1,4 +1,4 @@
1
1
  module OpenNamespace
2
2
  # open_namespace version
3
- VERSION = '0.3.2'
3
+ VERSION = '0.4.0'
4
4
  end
@@ -95,7 +95,7 @@ Gem::Specification.new do |gemspec|
95
95
  end
96
96
 
97
97
  if gemspec.respond_to?(:required_rubygems_version=)
98
- gemspec.required_rubygems_version = metadata['required_ruby_version']
98
+ gemspec.required_rubygems_version = metadata['required_rubygems_version']
99
99
  end
100
100
 
101
101
  parse_versions = lambda { |versions|
@@ -0,0 +1,6 @@
1
+ module Classes
2
+ module CustomNamespace
3
+ class ConstantThree
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Classes
2
+ module SimpleNamespace
3
+ class ConstantThree
4
+ end
5
+ end
6
+ end
@@ -66,6 +66,10 @@ describe OpenNamespace do
66
66
  const.should be_nil
67
67
  end
68
68
 
69
+ it "should attempt loading the constant when calling const_defined?" do
70
+ @module.const_defined?('ConstantThree').should == true
71
+ end
72
+
69
73
  it "should load constants transparently via const_missing" do
70
74
  const = @module::ConstantTwo
71
75
 
@@ -136,6 +140,10 @@ describe OpenNamespace do
136
140
  const.should be_nil
137
141
  end
138
142
 
143
+ it "should attempt loading the constant when calling const_defined?" do
144
+ @module.const_defined?('ConstantThree').should == true
145
+ end
146
+
139
147
  it "should load constants transparently via const_missing" do
140
148
  const = @module::ConstantTwo
141
149
 
metadata CHANGED
@@ -1,61 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: open_namespace
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
4
5
  prerelease:
5
- version: 0.3.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Postmodern
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-07-12 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: ore-tasks
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &10491560 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
18
+ requirements:
21
19
  - - ~>
22
- - !ruby/object:Gem::Version
23
- version: "0.4"
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
24
22
  type: :development
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: rspec
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *10491560
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &10494620 !ruby/object:Gem::Requirement
30
28
  none: false
31
- requirements:
29
+ requirements:
32
30
  - - ~>
33
- - !ruby/object:Gem::Version
34
- version: "2.4"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.4'
35
33
  type: :development
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: yard
39
34
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *10494620
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &10370760 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
40
+ requirements:
43
41
  - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 0.7.0
42
+ - !ruby/object:Gem::Version
43
+ version: '0.7'
46
44
  type: :development
47
- version_requirements: *id003
48
- description: OpenNamespace allows namespaces to require and find classes and modules from other RubyGems.
49
- email:
50
- - postmodern.mod3@gmail.com
45
+ prerelease: false
46
+ version_requirements: *10370760
47
+ description: OpenNamespace allows namespaces to require and find classes and modules
48
+ from other RubyGems.
49
+ email: postmodern.mod3@gmail.com
51
50
  executables: []
52
-
53
51
  extensions: []
54
-
55
- extra_rdoc_files:
52
+ extra_rdoc_files:
53
+ - ChangeLog.md
54
+ - LICENSE.txt
56
55
  - README.md
57
- files:
56
+ files:
58
57
  - .gemtest
58
+ - .gitignore
59
59
  - .rspec
60
60
  - .yardopts
61
61
  - ChangeLog.md
@@ -70,6 +70,7 @@ files:
70
70
  - open_namespace.gemspec
71
71
  - spec/classes/custom/bad_constant.rb
72
72
  - spec/classes/custom/constant_one.rb
73
+ - spec/classes/custom/constant_three.rb
73
74
  - spec/classes/custom/constant_two.rb
74
75
  - spec/classes/custom/odd_constant.rb
75
76
  - spec/classes/custom/sub/constant_four.rb
@@ -77,37 +78,37 @@ files:
77
78
  - spec/classes/simple_namespace.rb
78
79
  - spec/classes/simple_namespace/bad_constant.rb
79
80
  - spec/classes/simple_namespace/constant_one.rb
81
+ - spec/classes/simple_namespace/constant_three.rb
80
82
  - spec/classes/simple_namespace/constant_two.rb
81
83
  - spec/classes/simple_namespace/odd_constant.rb
82
84
  - spec/classes/simple_namespace/sub/constant_four.rb
83
85
  - spec/open_namespace_spec.rb
84
86
  - spec/spec_helper.rb
85
87
  homepage: http://github.com/postmodern/open_namespace
86
- licenses:
88
+ licenses:
87
89
  - MIT
88
90
  post_install_message:
89
91
  rdoc_options: []
90
-
91
- require_paths:
92
+ require_paths:
92
93
  - lib
93
- required_ruby_version: !ruby/object:Gem::Requirement
94
+ required_ruby_version: !ruby/object:Gem::Requirement
94
95
  none: false
95
- requirements:
96
- - - ">="
97
- - !ruby/object:Gem::Version
98
- version: "0"
99
- required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
101
  none: false
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: "0"
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
105
106
  requirements: []
106
-
107
- rubyforge_project: open_namespace
108
- rubygems_version: 1.8.5
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.10
109
109
  signing_key:
110
110
  specification_version: 3
111
111
  summary: Allows namespaces to load constants on-demand
112
- test_files:
112
+ test_files:
113
113
  - spec/open_namespace_spec.rb
114
+ has_rdoc: