function_importer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,11 @@
1
+ commit 1e41d415433a45ecac051021ac62fd88345c428c
2
+ Author: Keiji, Yoshimi <walf443@gmail.com>
3
+ Date: Sat Mar 28 20:00:10 2009 +0900
4
+
5
+ added ChangeLog file.
6
+
7
+ commit f365f447c2c9342400da03db57e012811a229900
8
+ Author: Keiji, Yoshimi <walf443@gmail.com>
9
+ Date: Sat Mar 28 19:44:15 2009 +0900
10
+
11
+ initial import.
data/README ADDED
@@ -0,0 +1,62 @@
1
+ = function_importer
2
+ writing explicitly need function for a module. and support rename.
3
+
4
+ == Example
5
+ require 'rubygems'
6
+ require 'function_importer'
7
+ module Utils
8
+ extend FunctionExporter
9
+
10
+ def escape str
11
+ "escaped_#{str}"
12
+ end
13
+ end
14
+
15
+ module Foo
16
+ Utils.export self, :escape
17
+
18
+ def run
19
+ p(escape('str')) #=> "escaped_str"
20
+ end
21
+ end
22
+
23
+ # you can rename methods when argument is Hash.
24
+ module Bar
25
+ Utils.export self, :escape => :my_escape
26
+
27
+ def run
28
+ p(my_escape('str')) #=> "escaped_str"
29
+ end
30
+ end
31
+
32
+ # or syntax suger.
33
+ module Baz
34
+ extend FunctionImporter
35
+ import_function Utils, :escape
36
+
37
+ def run
38
+ p(escape('str')) #=> "escaped_str"
39
+ end
40
+ end
41
+
42
+ # apply to class method.
43
+ module Baz
44
+ extend FunctionImporter
45
+ import_module_function Utils, :escape
46
+
47
+ def self.run
48
+ p(escape('str')) #=> "escaped_str"
49
+ end
50
+ end
51
+
52
+ == Descripotin
53
+ restrict importing methods and rename methods when include module.
54
+
55
+ == SEE ALSO
56
+ +module-import+: I'm not like this module interface. (For example, pollute Kernel#import ).
57
+
58
+ == Copyright
59
+
60
+ Author:: Keiji, Yoshimi <walf443 at gmail.com>
61
+ License:: you can redistribute it and/or modify it under the same terms as Ruby itself.
62
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'rake/contrib/sshpublisher'
10
+ require 'fileutils'
11
+ include FileUtils
12
+
13
+ load File.join(File.dirname(__FILE__), 'tasks', 'basic_config.rake')
14
+
15
+ NAME = "function_importer"
16
+ DESCRIPTION = <<-"END_DESCRIPTION"
17
+ restrict importing methods and rename methods when include module.
18
+ END_DESCRIPTION
19
+ BIN_FILES = %w( )
20
+ VERS = "0.0.1"
21
+
22
+ EXTRA_RDOC_FILES = []
23
+ HECKLE_ROOT_MODULES = ["Classx"]
24
+
25
+ SPEC = Gem::Specification.new do |s|
26
+ s.name = NAME
27
+ s.version = VERS
28
+ s.platform = Gem::Platform::RUBY
29
+ s.has_rdoc = true
30
+ s.extra_rdoc_files = DEFAULT_EXTRA_RDOC_FILES + EXTRA_RDOC_FILES
31
+ s.rdoc_options += RDOC_OPTS + ['--title', "#{NAME} documentation", ]
32
+ s.summary = DESCRIPTION
33
+ s.description = DESCRIPTION
34
+ s.author = AUTHOR
35
+ s.email = EMAIL
36
+ s.homepage = HOMEPATH
37
+ s.executables = BIN_FILES
38
+ s.rubyforge_project = RUBYFORGE_PROJECT
39
+ s.bindir = "bin"
40
+ s.require_path = "lib"
41
+ s.autorequire = ""
42
+ s.test_files = Dir["spec/*_spec.rb"]
43
+
44
+ if Gem::RubyGemsVersion >= "1.2"
45
+ s.add_development_dependency('rspec', '>=1.1.4')
46
+ end
47
+ #s.required_ruby_version = '>= 1.8.2'
48
+
49
+ s.files = PKG_FILES + EXTRA_RDOC_FILES
50
+ s.extensions = EXTENSIONS
51
+ end
52
+
53
+ import File.join(File.dirname(__FILE__), 'tasks', 'basic_tasks.rake')
@@ -0,0 +1,86 @@
1
+ # add module a method to export function.
2
+ module FunctionExporter
3
+ # Example:
4
+ # module Utils
5
+ # extend FunctionExporter
6
+ #
7
+ # def escape str
8
+ # "escaped_#{str}"
9
+ # end
10
+ # end
11
+ #
12
+ # module Foo
13
+ # Utils.export self, :escape
14
+ #
15
+ # def run
16
+ # p(escape('str')) #=> "escaped_str"
17
+ # end
18
+ # end
19
+ #
20
+ # # you can rename method when argument is Hash.
21
+ # module Bar
22
+ # Utils.export self, :escape => :my_escape
23
+ #
24
+ # def run
25
+ # p(my_escape('str')) #=> "escaped_str"
26
+ # end
27
+ # end
28
+ #
29
+ def export context, *args
30
+ parent_mod = create_export_module(*args)
31
+ context.module_eval do
32
+ include parent_mod
33
+ end
34
+ end
35
+
36
+ def create_export_module *args
37
+ parent_mod = self.dup
38
+
39
+ method_of = {};
40
+ case args.first
41
+ when Hash
42
+ args.first.each do |key, val|
43
+ method_of[key.to_s] = val
44
+ end
45
+ else
46
+ args.each do |i|
47
+ method_of[i.to_s] = true
48
+ end
49
+ end
50
+
51
+ no_need_methods = []
52
+ parent_mod.instance_methods.each do |meth|
53
+ unless method_of[meth.to_s]
54
+ no_need_methods.push meth
55
+ end
56
+ end
57
+
58
+ parent_mod.module_eval do
59
+ # renate method.
60
+ method_of.each do |key, val|
61
+ next if val.kind_of? TrueClass
62
+ alias_method val, key
63
+ undef_method key
64
+ end
65
+ no_need_methods.each do |meth|
66
+ undef_method meth
67
+ end
68
+ end
69
+
70
+ parent_mod
71
+ end
72
+ end
73
+
74
+ # syntax suger for import function.
75
+ module FunctionImporter
76
+
77
+ def import_function mod, *funcs
78
+ include(mod.create_export_module(*funcs))
79
+ end
80
+
81
+ def import_module_function mod, *funcs
82
+ extend(mod.create_export_module(*funcs))
83
+ end
84
+
85
+ private :import_function, :import_module_function
86
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe FunctionExporter do
4
+ before :all do
5
+ @module = Module.new
6
+ @module.module_eval do
7
+ extend FunctionExporter
8
+
9
+ define_method :escape do |str|
10
+ "escaped_#{str}"
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+ describe 'export a function' do
17
+ before do
18
+ @export_mod = @module.create_export_module :escape
19
+ end
20
+
21
+ it 'should return Module' do
22
+ @export_mod.should be_kind_of(Module)
23
+ end
24
+
25
+ it 'should have #escape as public' do
26
+ @export_mod.public_instance_methods.any? {|i| i.to_s == 'escape' }.should be_true
27
+ end
28
+ end
29
+
30
+ describe 'export a function with rename' do
31
+ before do
32
+ @export_mod = @module.create_export_module :escape => :my_escape
33
+ end
34
+
35
+ it 'should not have #escape' do
36
+ @export_mod.instance_methods.all? {|i| i.to_s != 'escape' }.should be_true
37
+ end
38
+
39
+ it 'should have #my_escape as public' do
40
+ @export_mod.instance_methods.any? {|i| i.to_s == 'my_escape' }.should be_true
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '../lib')))
2
+ require 'function_importer'
3
+
4
+ require 'rubygems'
5
+ require 'spec'
@@ -0,0 +1,27 @@
1
+ AUTHOR = "Keiji, Yoshimi"
2
+ EMAIL = "walf443 at gmail.com"
3
+ RUBYFORGE_PROJECT = "akasakarb"
4
+ RUBYFORGE_PROJECT_ID = 4314
5
+ HOMEPATH = "http://walf443.github.com/function_importer/"
6
+ RDOC_OPTS = [
7
+ "--charset", "utf-8",
8
+ "--opname", "index.html",
9
+ "--line-numbers",
10
+ "--main", "README",
11
+ "--inline-source",
12
+ "--accessor", "has=rw",
13
+ '--exclude', '^(example|extras)/'
14
+ ]
15
+ DEFAULT_EXTRA_RDOC_FILES = ['README', 'ChangeLog']
16
+ PKG_FILES = [ 'Rakefile' ] +
17
+ DEFAULT_EXTRA_RDOC_FILES +
18
+ Dir.glob('{bin,lib,test,spec,doc,bench,example,tasks,script,generator,templates,extras,website}/**/*') +
19
+ Dir.glob('ext/**/*.{h,c,rb}') +
20
+ Dir.glob('examples/**/*.rb') +
21
+ Dir.glob('tools/*.rb')
22
+
23
+ [ %r{^doc/.+\.pdf}, %r{^doc/.+\.key} ].each do |ignore|
24
+ PKG_FILES.delete_if {|file| file =~ ignore }
25
+ end
26
+
27
+ EXTENSIONS = FileList['ext/**/extconf.rb'].to_a
@@ -0,0 +1,144 @@
1
+
2
+ REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
3
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
4
+
5
+ Rake::GemPackageTask.new(SPEC) do |p|
6
+ p.need_tar = true
7
+ p.gem_spec = SPEC
8
+ end
9
+
10
+ task :default => [:spec]
11
+ task :test => [:spec]
12
+ task :package => [:clean]
13
+
14
+ require 'spec/rake/spectask'
15
+ Spec::Rake::SpecTask.new(:spec) do |t|
16
+ t.spec_files = FileList['spec/**/*_spec.rb']
17
+ t.spec_opts = ['--options', 'spec/spec.opts']
18
+ t.warning = true
19
+ # t.rcov = true
20
+ t.rcov_dir = 'doc/output/coverage'
21
+ t.rcov_opts = ['--exclude', 'spec,\.autotest,/Library/']
22
+ end
23
+
24
+ desc "Heckle each module and class in turn"
25
+ task :heckle => :spec do
26
+ root_modules = HECKLE_ROOT_MODULES
27
+ spec_files = FileList['spec/**/*_spec.rb']
28
+
29
+ current_module, current_method = nil, nil
30
+ heckle_caught_modules = Hash.new { |hash, key| hash[key] = [] }
31
+ unhandled_mutations = 0
32
+
33
+ root_modules.each do |root_module|
34
+ IO.popen("heckle #{root_module} -t #{spec_files}") do |pipe|
35
+ while line = pipe.gets
36
+ line = line.chomp
37
+
38
+ if line =~ /^\*\*\* ((?:\w+(?:::)?)+)#(\w+)/
39
+ current_module, current_method = $1, $2
40
+ elsif line == "The following mutations didn't cause test failures:"
41
+ heckle_caught_modules[current_module] << current_method
42
+ elsif line == "+++ mutation"
43
+ unhandled_mutations += 1
44
+ end
45
+
46
+ puts line
47
+ end
48
+ end
49
+ end
50
+
51
+ if unhandled_mutations > 0
52
+ error_message_lines = ["*************\n"]
53
+
54
+ error_message_lines <<
55
+ "Heckle found #{unhandled_mutations} " +
56
+ "mutation#{"s" unless unhandled_mutations == 1} " +
57
+ "that didn't cause spec violations\n"
58
+
59
+ heckle_caught_modules.each do |mod, methods|
60
+ error_message_lines <<
61
+ "#{mod} contains the following poorly-specified methods:"
62
+ methods.each do |m|
63
+ error_message_lines << " - #{m}"
64
+ end
65
+ error_message_lines << ""
66
+ end
67
+
68
+ error_message_lines <<
69
+ "Get your act together and come back " +
70
+ "when your specs are doing their job!"
71
+
72
+ puts "*************"
73
+ raise error_message_lines.join("\n")
74
+ else
75
+ puts "Well done! Your code withstood a heckling."
76
+ end
77
+ end
78
+
79
+ require 'spec/rake/verify_rcov'
80
+ RCov::VerifyTask.new(:rcov => :spec) do |t|
81
+ t.index_html = "doc/output/coverage/index.html"
82
+ t.threshold = 100
83
+ end
84
+
85
+ task :install do
86
+ name = "#{NAME}-#{VERS}.gem"
87
+ sh %{rake package}
88
+ sh %{sudo gem install pkg/#{name}}
89
+ end
90
+
91
+ task :uninstall => [:clean] do
92
+ sh %{sudo gem uninstall #{NAME}}
93
+ end
94
+
95
+ begin
96
+ allison_path = `allison --path`.chomp
97
+ Rake::RDocTask.new do |rdoc|
98
+ rdoc.rdoc_dir = 'html'
99
+ rdoc.options += RDOC_OPTS
100
+ rdoc.template = allison_path
101
+ if ENV['DOC_FILES']
102
+ rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
103
+ else
104
+ rdoc.rdoc_files.include('README', 'ChangeLog')
105
+ rdoc.rdoc_files.include('lib/**/*.rb')
106
+ rdoc.rdoc_files.include('ext/**/*.c')
107
+ end
108
+ end
109
+ rescue Exception => e
110
+ warn e
111
+ warn "skipping rdoc task"
112
+ ensure
113
+ end
114
+
115
+ desc "Publish to RubyForge"
116
+ task :rubyforge => [:rdoc, :package] do
117
+ require 'rubyforge'
118
+ Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, 'yoshimi').upload
119
+ end
120
+
121
+ desc 'Package and upload the release to rubyforge.'
122
+ task :release => [:clean, :package] do |t|
123
+ require 'rubyforge'
124
+ v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z"
125
+ abort "Versions don't match #{v} vs #{VERS}" unless v == VERS
126
+ pkg = "pkg/#{NAME}-#{VERS}"
127
+
128
+ rf = RubyForge.new
129
+ puts "Logging in"
130
+ rf.login
131
+
132
+ c = rf.userconfig
133
+ # c["release_notes"] = description if description
134
+ # c["release_changes"] = changes if changes
135
+ c["preformatted"] = true
136
+
137
+ files = [
138
+ "#{pkg}.tgz",
139
+ "#{pkg}.gem"
140
+ ].compact
141
+
142
+ puts "Releasing #{NAME} v. #{VERS}"
143
+ rf.add_release RUBYFORGE_PROJECT_ID, RUBYFORGE_PACKAGE_ID, VERS, *files
144
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: function_importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Keiji, Yoshimi
8
+ autorequire: ""
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-28 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.4
24
+ version:
25
+ description: restrict importing methods and rename methods when include module.
26
+ email: walf443 at gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - ChangeLog
34
+ files:
35
+ - Rakefile
36
+ - README
37
+ - ChangeLog
38
+ - lib/function_importer.rb
39
+ - spec/function_exporter_spec.rb
40
+ - spec/spec_helper.rb
41
+ - tasks/basic_config.rake
42
+ - tasks/basic_tasks.rake
43
+ has_rdoc: true
44
+ homepage: http://walf443.github.com/function_importer/
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset
48
+ - utf-8
49
+ - --opname
50
+ - index.html
51
+ - --line-numbers
52
+ - --main
53
+ - README
54
+ - --inline-source
55
+ - --accessor
56
+ - has=rw
57
+ - --exclude
58
+ - ^(example|extras)/
59
+ - --title
60
+ - function_importer documentation
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project: akasakarb
78
+ rubygems_version: 1.3.1
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: restrict importing methods and rename methods when include module.
82
+ test_files:
83
+ - spec/function_exporter_spec.rb