Skip to content

Commit 1407886

Browse files
David MaloneyDavid Maloney
authored andcommitted
Revert "fix a major typo snaffu"
This reverts commit c639de7.
1 parent c639de7 commit 1407886

File tree

14 files changed

+725
-3
lines changed

14 files changed

+725
-3
lines changed

lib/gemcache/ruby/1.9.1/gems/method_source-0.7.1/.gemtest

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
rvm:
2+
- 1.8.7
3+
- 1.9.2
4+
- 1.9.3
5+
- ree
6+
- rbx-18mode
7+
- rbx-19mode
8+
- jruby
9+
10+
notifications:
11+
irc: "irc.freenode.org#pry"
12+
recipients:
13+
14+
15+
branches:
16+
only:
17+
- master
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-m markdown
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source :rubygems
2+
gemspec
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
License
2+
-------
3+
4+
(The MIT License)
5+
6+
Copyright (c) 2011 John Mair (banisterfiend)
7+
8+
Permission is hereby granted, free of charge, to any person obtaining
9+
a copy of this software and associated documentation files (the
10+
'Software'), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be
17+
included in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
20+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
method_source
2+
=============
3+
4+
(C) John Mair (banisterfiend) 2011
5+
6+
_retrieve the sourcecode for a method_
7+
8+
*NOTE:* This simply utilizes `Method#source_location`; it
9+
does not access the live AST.
10+
11+
`method_source` is a utility to return a method's sourcecode as a
12+
Ruby string. Also returns `Proc` and `Lambda` sourcecode.
13+
14+
Method comments can also be extracted using the `comment` method.
15+
16+
It is written in pure Ruby (no C).
17+
18+
* Some Ruby 1.8 support now available.
19+
* Support for MRI, RBX, JRuby, REE
20+
21+
`method_source` provides the `source` and `comment` methods to the `Method` and
22+
`UnboundMethod` and `Proc` classes.
23+
24+
* Install the [gem](https://rubygems.org/gems/method_source): `gem install method_source`
25+
* Read the [documentation](http://rdoc.info/github/banister/method_source/master/file/README.markdown)
26+
* See the [source code](http://github.com/banister/method_source)
27+
28+
Example: display method source
29+
------------------------------
30+
31+
Set.instance_method(:merge).source.display
32+
# =>
33+
def merge(enum)
34+
if enum.instance_of?(self.class)
35+
@hash.update(enum.instance_variable_get(:@hash))
36+
else
37+
do_with_enum(enum) { |o| add(o) }
38+
end
39+
40+
self
41+
end
42+
43+
Example: display method comments
44+
--------------------------------
45+
46+
Set.instance_method(:merge).comment.display
47+
# =>
48+
# Merges the elements of the given enumerable object to the set and
49+
# returns self.
50+
51+
Limitations:
52+
------------
53+
54+
* Occasional strange behaviour in Ruby 1.8
55+
* Cannot return source for C methods.
56+
* Cannot return source for dynamically defined methods.
57+
58+
Special Thanks
59+
--------------
60+
61+
[Adam Sanderson](https://github.com/adamsanderson) for `comment` functionality.
62+
63+
[Dmitry Elastic](https://github.com/dmitryelastic) for the brilliant Ruby 1.8 `source_location` hack.
64+
65+
[Samuel Kadolph](https://github.com/samuelkadolph) for the JRuby 1.8 `source_location`.
66+
67+
License
68+
-------
69+
70+
(The MIT License)
71+
72+
Copyright (c) 2011 John Mair (banisterfiend)
73+
74+
Permission is hereby granted, free of charge, to any person obtaining
75+
a copy of this software and associated documentation files (the
76+
'Software'), to deal in the Software without restriction, including
77+
without limitation the rights to use, copy, modify, merge, publish,
78+
distribute, sublicense, and/or sell copies of the Software, and to
79+
permit persons to whom the Software is furnished to do so, subject to
80+
the following conditions:
81+
82+
The above copyright notice and this permission notice shall be
83+
included in all copies or substantial portions of the Software.
84+
85+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
88+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
89+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
90+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
91+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
dlext = Config::CONFIG['DLEXT']
2+
direc = File.dirname(__FILE__)
3+
4+
require 'rake/clean'
5+
require 'rake/gempackagetask'
6+
require "#{direc}/lib/method_source/version"
7+
8+
CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
9+
CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
10+
"ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*.rbc",
11+
"ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake")
12+
13+
def apply_spec_defaults(s)
14+
s.name = "method_source"
15+
s.summary = "retrieve the sourcecode for a method"
16+
s.version = MethodSource::VERSION
17+
s.date = Time.now.strftime '%Y-%m-%d'
18+
s.author = "John Mair (banisterfiend)"
19+
s.email = '[email protected]'
20+
s.description = s.summary
21+
s.require_path = 'lib'
22+
23+
s.add_development_dependency("bacon","~>1.1.0")
24+
s.add_development_dependency("rake", "~>0.9")
25+
s.homepage = "http://banisterfiend.wordpress.com"
26+
s.has_rdoc = 'yard'
27+
s.files = `git ls-files`.split("\n")
28+
s.test_files = `git ls-files -- test/*`.split("\n")
29+
end
30+
31+
task :test do
32+
sh "bacon -q #{direc}/test/test.rb"
33+
end
34+
35+
desc "reinstall gem"
36+
task :reinstall => :gems do
37+
sh "gem uninstall method_source" rescue nil
38+
sh "gem install #{direc}/pkg/method_source-#{MethodSource::VERSION}.gem"
39+
end
40+
41+
desc "Set up and run tests"
42+
task :default => [:test]
43+
44+
namespace :ruby do
45+
spec = Gem::Specification.new do |s|
46+
apply_spec_defaults(s)
47+
s.platform = Gem::Platform::RUBY
48+
end
49+
50+
Rake::GemPackageTask.new(spec) do |pkg|
51+
pkg.need_zip = false
52+
pkg.need_tar = false
53+
end
54+
55+
desc "Generate gemspec file"
56+
task :gemspec do
57+
File.open("#{spec.name}.gemspec", "w") do |f|
58+
f << spec.to_ruby
59+
end
60+
end
61+
end
62+
63+
desc "build all platform gems at once"
64+
task :gems => [:rmgems, "ruby:gem"]
65+
66+
desc "remove all platform gems"
67+
task :rmgems => ["ruby:clobber_package"]
68+
69+
desc "build and push latest gems"
70+
task :pushgems => :gems do
71+
chdir("#{direc}/pkg") do
72+
Dir["*.gem"].each do |gemfile|
73+
sh "gem push #{gemfile}"
74+
end
75+
end
76+
end
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# (C) John Mair (banisterfiend) 2011
2+
# MIT License
3+
4+
direc = File.dirname(__FILE__)
5+
6+
require "#{direc}/method_source/version"
7+
require "#{direc}/method_source/source_location"
8+
9+
module MethodSource
10+
# Determine if a string of code is a valid Ruby expression.
11+
# @param [String] code The code to validate.
12+
# @return [Boolean] Whether or not the code is a valid Ruby expression.
13+
# @example
14+
# valid_expression?("class Hello") #=> false
15+
# valid_expression?("class Hello; end") #=> true
16+
def self.valid_expression?(str)
17+
if defined?(Rubinius::Melbourne19) && RUBY_VERSION =~ /^1\.9/
18+
Rubinius::Melbourne19.parse_string(str)
19+
elsif defined?(Rubinius::Melbourne)
20+
Rubinius::Melbourne.parse_string(str)
21+
else
22+
catch(:valid) {
23+
eval("BEGIN{throw :valid}\n#{str}")
24+
}
25+
end
26+
true
27+
rescue SyntaxError
28+
false
29+
end
30+
31+
# Helper method responsible for extracting method body.
32+
# Defined here to avoid polluting `Method` class.
33+
# @param [Array] source_location The array returned by Method#source_location
34+
# @return [File] The opened source file
35+
def self.source_helper(source_location)
36+
return nil if !source_location.is_a?(Array)
37+
38+
file_name, line = source_location
39+
File.open(file_name) do |file|
40+
(line - 1).times { file.readline }
41+
42+
code = ""
43+
loop do
44+
val = file.readline
45+
code << val
46+
47+
return code if valid_expression?(code)
48+
end
49+
end
50+
end
51+
52+
# Helper method responsible for opening source file and buffering up
53+
# the comments for a specified method. Defined here to avoid polluting
54+
# `Method` class.
55+
# @param [Array] source_location The array returned by Method#source_location
56+
# @return [String] The comments up to the point of the method.
57+
def self.comment_helper(source_location)
58+
return nil if !source_location.is_a?(Array)
59+
60+
file_name, line = source_location
61+
File.open(file_name) do |file|
62+
buffer = ""
63+
(line - 1).times do
64+
line = file.readline
65+
# Add any line that is a valid ruby comment,
66+
# but clear as soon as we hit a non comment line.
67+
if (line =~ /^\s*#/) || (line =~ /^\s*$/)
68+
buffer << line.lstrip
69+
else
70+
buffer.replace("")
71+
end
72+
end
73+
74+
buffer
75+
end
76+
end
77+
78+
# This module is to be included by `Method` and `UnboundMethod` and
79+
# provides the `#source` functionality
80+
module MethodExtensions
81+
82+
# We use the included hook to patch Method#source on rubinius.
83+
# We need to use the included hook as Rubinius defines a `source`
84+
# on Method so including a module will have no effect (as it's
85+
# higher up the MRO).
86+
# @param [Class] klass The class that includes the module.
87+
def self.included(klass)
88+
if klass.method_defined?(:source) && Object.const_defined?(:RUBY_ENGINE) &&
89+
RUBY_ENGINE =~ /rbx/
90+
91+
klass.class_eval do
92+
orig_source = instance_method(:source)
93+
94+
define_method(:source) do
95+
begin
96+
super
97+
rescue
98+
orig_source.bind(self).call
99+
end
100+
end
101+
102+
end
103+
end
104+
end
105+
106+
# Return the sourcecode for the method as a string
107+
# (This functionality is only supported in Ruby 1.9 and above)
108+
# @return [String] The method sourcecode as a string
109+
# @example
110+
# Set.instance_method(:clear).source.display
111+
# =>
112+
# def clear
113+
# @hash.clear
114+
# self
115+
# end
116+
def source
117+
if respond_to?(:source_location)
118+
source = MethodSource.source_helper(source_location)
119+
120+
raise "Cannot locate source for this method: #{name}" if !source
121+
else
122+
raise "#{self.class}#source not supported by this Ruby version (#{RUBY_VERSION})"
123+
end
124+
125+
source
126+
end
127+
128+
# Return the comments associated with the method as a string.
129+
# (This functionality is only supported in Ruby 1.9 and above)
130+
# @return [String] The method's comments as a string
131+
# @example
132+
# Set.instance_method(:clear).comment.display
133+
# =>
134+
# # Removes all elements and returns self.
135+
def comment
136+
if respond_to?(:source_location)
137+
comment = MethodSource.comment_helper(source_location)
138+
139+
raise "Cannot locate source for this method: #{name}" if !comment
140+
else
141+
raise "#{self.class}#comment not supported by this Ruby version (#{RUBY_VERSION})"
142+
end
143+
144+
comment
145+
end
146+
end
147+
end
148+
149+
class Method
150+
include MethodSource::SourceLocation::MethodExtensions
151+
include MethodSource::MethodExtensions
152+
end
153+
154+
class UnboundMethod
155+
include MethodSource::SourceLocation::UnboundMethodExtensions
156+
include MethodSource::MethodExtensions
157+
end
158+
159+
class Proc
160+
include MethodSource::SourceLocation::ProcExtensions
161+
include MethodSource::MethodExtensions
162+
end
163+

0 commit comments

Comments
 (0)