dyoder-autocode 0.9.9 → 1.0.0

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/lib/autocode.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  module AutoCode
2
2
 
3
3
  # always make sure we have a camel-cased symbol
4
- def AutoCode.normalize( cname )
5
- return cname unless cname.is_a? String
6
- cname.gsub(/(_)(\w)/) { $2.upcase }.gsub(/^([a-z])/) { $1.upcase }.intern
4
+ def AutoCode.normalize( cname )
5
+ return cname unless cname.is_a? Symbol or cname.is_a? String
6
+ camel_case( cname )
7
+ end
8
+
9
+ def AutoCode.camel_case( cname )
10
+ cname.to_s.gsub(/^([a-z])/) { $1.upcase }.gsub(/(_)(\w)/) { $2.upcase }.intern
7
11
  end
8
12
 
9
13
  def AutoCode.snake_case( cname )
@@ -31,7 +35,7 @@ module AutoCode
31
35
  @autocode[:constructors][ AutoCode.normalize( key ) ] << lambda do | cname |
32
36
  exemplar = ( options[:exemplar] || Module.new ).clone
33
37
  exemplar.module_eval( &block ) if block
34
- const_set( cname, exemplar )
38
+ const_set( cname, exemplar ) and true
35
39
  end
36
40
  end
37
41
 
@@ -39,14 +43,11 @@ module AutoCode
39
43
  def auto_load( key = true, options = {} )
40
44
  @autocode[:constructors][ AutoCode.normalize( key ) ] << lambda do | cname |
41
45
  filename = AutoCode.snake_case( cname ) << '.rb'
42
- if options[:directories].nil?
43
- Kernel.load( filename ) if File.exist?( filename )
44
- else
45
- path = options[:directories].
46
- map { |dir| File.join( dir.to_s, filename ) }.
47
- find { |path| File.exist?( path ) }
48
- Kernel.load( path ) unless path.nil?
49
- end
46
+ options[:directories] ||= '.'
47
+ path = options[:directories].
48
+ map { |dir| File.join( dir.to_s, filename ) }.
49
+ find { |path| File.exist?( path ) }
50
+ Kernel.load( path ) unless path.nil?
50
51
  end
51
52
  end
52
53
 
@@ -79,11 +80,12 @@ module AutoCode
79
80
  (class << self ; self ; end ).instance_eval do
80
81
  define_method( :const_missing ) do | cname |
81
82
  constructors = @autocode[:constructors][true] + @autocode[:constructors][cname]
82
- constructors.pop.call( cname ) until ( const_defined?( cname ) or constructors.empty? )
83
- return old.call( cname ) unless const_defined?( cname )
83
+ constructors.reverse.find { | c | c.call( cname ) and const_defined?( cname ) }
84
+ return old.call( cname ) unless const_defined?( cname )
84
85
  initializers = @autocode[:initializers][true] + @autocode[:initializers][cname]
85
- mod = const_get( cname ) ; initializers.pop.call( mod ) until initializers.empty?
86
- @autocode[:loaded] << cname ; const_get( cname )
86
+ mod = const_get( cname ); initializers.each { |init| init.call( mod ) }
87
+ @autocode[:loaded] << cname
88
+ mod
87
89
  end
88
90
  end
89
91
  end
data/test/auto_create.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), 'helpers.rb')
2
2
 
3
- describe "auto_create should" do
3
+ describe "auto_create" do
4
4
 
5
5
  before do
6
6
  Object.instance_eval { remove_const(:A) if const_defined?(:A) }
@@ -13,15 +13,15 @@ describe "auto_create should" do
13
13
  end
14
14
  end
15
15
 
16
- specify "allow you create and initialize a given const name" do
16
+ specify "allows you to create and initialize a given const name" do
17
17
  A::B.class.should == Module
18
18
  end
19
19
 
20
- specify "allow you create and initialize const using a wildcard" do
20
+ specify "allows you to create and initialize const using a wildcard" do
21
21
  A::B::C.class.should === Class
22
22
  end
23
23
 
24
- specify "should raise a NameError if a const doesn't match" do
24
+ specify "raises a NameError if a const doesn't match" do
25
25
  lambda{ A::C }.should.raise NameError
26
26
  end
27
27
 
data/test/auto_eval.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), 'helpers.rb')
2
2
 
3
- describe "auto_eval should" do
3
+ describe "auto_eval" do
4
4
 
5
5
  before do
6
6
  Object.instance_eval { remove_const(:A) if const_defined?(:A) }
@@ -20,11 +20,11 @@ describe "auto_eval should" do
20
20
  end
21
21
  end
22
22
 
23
- specify "allow you to run blocks after an object is first created" do
23
+ specify "allows you to run blocks after an object is first created" do
24
24
  A::B::C.class.should == Class
25
25
  end
26
26
 
27
- specify "allow you to define nested auto_eval declarations" do
27
+ specify "allows you to define nested auto_eval declarations" do
28
28
  A::B::C::D.should == true
29
29
  end
30
30
 
data/test/auto_load.rb CHANGED
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'helpers.rb')
2
2
  require 'extensions/io'
3
3
 
4
4
 
5
- describe "auto_load should" do
5
+ describe "auto_load" do
6
6
 
7
7
  before do
8
8
  Object.instance_eval { remove_const(:A) if const_defined?(:A) }
@@ -28,7 +28,7 @@ describe "auto_load should" do
28
28
  FileUtils.rmdir( 'tmp' )
29
29
  end
30
30
 
31
- specify "allow you to load a file to define a const" do
31
+ specify "allows you to load a file to define a const" do
32
32
  A::B.class.should == Module
33
33
  end
34
34
 
@@ -36,16 +36,8 @@ describe "auto_load should" do
36
36
  A::B.class.should == Module
37
37
  end
38
38
 
39
- specify "should raise a NameError if a const doesn't match" do
39
+ specify "raises a NameError if a const doesn't match" do
40
40
  lambda{ A::C }.should.raise NameError
41
41
  end
42
-
43
- specify "snake case the constant name which is used to map a constant to a filename" do
44
- AutoCode.snake_case(:Post).should == "post"
45
- AutoCode.snake_case(:GitHub).should == "git_hub"
46
- AutoCode.snake_case(:GITRepository).should == "git_repository"
47
- AutoCode.snake_case(:Git42Repository).should == "git42_repository"
48
- AutoCode.snake_case(:GIT42Repository).should == "git42_repository"
49
- end
50
42
 
51
43
  end
data/test/helpers.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- %w{ bacon metaid }.each { |dep| require dep }
2
+ %w{ bacon }.each { |dep| require dep }
3
3
  # Bacon.extend Bacon::TestUnitOutput
4
4
  Bacon.summary_on_exit
5
5
 
@@ -12,4 +12,4 @@ Bacon::Context.instance_eval do
12
12
  alias_method :specify, :it
13
13
  end
14
14
 
15
- require '../lib/autocode'
15
+ require "#{File.dirname(__FILE__)}/../lib/autocode"
data/test/normalize.rb ADDED
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'helpers.rb')
2
+
3
+ describe "AutoCode should normalize const names" do
4
+
5
+ before do
6
+ Object.instance_eval { remove_const(:A) if const_defined?(:A) }
7
+ module A
8
+ include AutoCode
9
+ auto_create_module :foo_bar
10
+ auto_create_module :foo_bar_baz
11
+ auto_create_module :weird_Casing
12
+ auto_create_module :catch_22
13
+ auto_create_module :front242
14
+ end
15
+ A.auto_eval( :foo_bar ) { self::D = true }
16
+ A.auto_eval( :foo_bar_baz ) { self::D = true }
17
+ A.auto_eval( :weird_Casing ) { self::D = true }
18
+ A.auto_eval( :Catch22 ) { self::D = true }
19
+ A.auto_eval( :Front242 ) { self::D = true }
20
+ end
21
+
22
+ specify "converting from snake-case to camel-case" do
23
+ A::FooBar::D.should == true
24
+ A::FooBarBaz::D.should == true
25
+ A::WeirdCasing::D.should == true
26
+ A::Catch22::D.should == true
27
+ A::Front242::D.should == true
28
+ end
29
+
30
+ end
metadata CHANGED
@@ -1,28 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dyoder-autocode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.9
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Yoder
8
+ - Matthew King
9
+ - Lawrence Pitt
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
13
 
12
- date: 2008-05-15 00:00:00 -07:00
14
+ date: 2008-06-09 00:00:00 -07:00
13
15
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: metaid
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: "0"
23
- version:
16
+ dependencies: []
17
+
24
18
  description:
25
- email:
19
+ email: dan@zeraweb.com
26
20
  executables: []
27
21
 
28
22
  extensions: []
@@ -31,12 +25,11 @@ extra_rdoc_files: []
31
25
 
32
26
  files:
33
27
  - lib/autocode.rb
34
- - test/auto_code.gemspec
35
28
  - test/auto_create.rb
36
29
  - test/auto_eval.rb
37
30
  - test/auto_load.rb
38
- - test/autocode.gemspec
39
31
  - test/helpers.rb
32
+ - test/normalize.rb
40
33
  has_rdoc: true
41
34
  homepage: http://dev.zeraweb.com/
42
35
  post_install_message:
@@ -58,8 +51,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
51
  version:
59
52
  requirements: []
60
53
 
61
- rubyforge_project:
62
- rubygems_version: 1.0.1
54
+ rubyforge_project: autocode
55
+ rubygems_version: 1.2.0
63
56
  signing_key:
64
57
  specification_version: 2
65
58
  summary: Utility for auto-including, reloading, and generating classes and modules.
@@ -1,19 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = %q{auto_code}
3
- s.version = "0.9.8"
4
-
5
- s.specification_version = 2 if s.respond_to? :specification_version=
6
-
7
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Dan Yoder"]
9
- s.date = %q{2008-05-11}
10
- s.files = ["lib/auto_code.rb", "test/auto_create.rb", "test/auto_eval.rb", "test/auto_load", "test/auto_load.rb", "test/helpers.rb"]
11
- s.has_rdoc = true
12
- s.homepage = %q{http://dev.zeraweb.com/}
13
- s.require_paths = ["lib"]
14
- s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
15
- s.rubygems_version = %q{1.0.1}
16
- s.summary = %q{Utility for auto-including, reloading, and generating classes and modules.}
17
-
18
- s.add_dependency(%q<metaid>, [">= 0"])
19
- end