added_methods 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +68 -81
- data/README +11 -10
- data/Rakefile +9 -17
- data/lib/added_methods/added_method.rb +142 -0
- data/lib/added_methods/version.rb +1 -1
- data/lib/added_methods.rb +10 -121
- data/spec/added_methods_spec.rb +120 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +4 -0
- metadata +34 -19
data/COPYING
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
= License for added_methods
|
2
|
-
|
3
|
-
GNU GENERAL PUBLIC LICENSE
|
4
|
-
Version 3,
|
2
|
+
|
3
|
+
GNU AFFERO GENERAL PUBLIC LICENSE
|
4
|
+
Version 3, 19 November 2007
|
5
5
|
|
6
6
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
7
7
|
Everyone is permitted to copy and distribute verbatim copies
|
@@ -9,17 +9,15 @@
|
|
9
9
|
|
10
10
|
Preamble
|
11
11
|
|
12
|
-
The GNU General Public License is a free, copyleft license for
|
13
|
-
software and other kinds of works
|
12
|
+
The GNU Affero General Public License is a free, copyleft license for
|
13
|
+
software and other kinds of works, specifically designed to ensure
|
14
|
+
cooperation with the community in the case of network server software.
|
14
15
|
|
15
16
|
The licenses for most software and other practical works are designed
|
16
17
|
to take away your freedom to share and change the works. By contrast,
|
17
|
-
|
18
|
+
our General Public Licenses are intended to guarantee your freedom to
|
18
19
|
share and change all versions of a program--to make sure it remains free
|
19
|
-
software for all its users.
|
20
|
-
GNU General Public License for most of our software; it applies also to
|
21
|
-
any other work released this way by its authors. You can apply it to
|
22
|
-
your programs, too.
|
20
|
+
software for all its users.
|
23
21
|
|
24
22
|
When we speak of free software, we are referring to freedom, not
|
25
23
|
price. Our General Public Licenses are designed to make sure that you
|
@@ -28,44 +26,34 @@
|
|
28
26
|
want it, that you can change the software or use pieces of it in new
|
29
27
|
free programs, and that you know you can do these things.
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
that
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
products. If such problems arise substantially in other domains, we
|
60
|
-
stand ready to extend this provision to those domains in future versions
|
61
|
-
of the GPL, as needed to protect the freedom of users.
|
62
|
-
|
63
|
-
Finally, every program is threatened constantly by software patents.
|
64
|
-
States should not allow patents to restrict development and use of
|
65
|
-
software on general-purpose computers, but in those that do, we wish to
|
66
|
-
avoid the special danger that patents applied to a free program could
|
67
|
-
make it effectively proprietary. To prevent this, the GPL assures that
|
68
|
-
patents cannot be used to render the program non-free.
|
29
|
+
Developers that use our General Public Licenses protect your rights
|
30
|
+
with two steps: (1) assert copyright on the software, and (2) offer
|
31
|
+
you this License which gives you legal permission to copy, distribute
|
32
|
+
and/or modify the software.
|
33
|
+
|
34
|
+
A secondary benefit of defending all users' freedom is that
|
35
|
+
improvements made in alternate versions of the program, if they
|
36
|
+
receive widespread use, become available for other developers to
|
37
|
+
incorporate. Many developers of free software are heartened and
|
38
|
+
encouraged by the resulting cooperation. However, in the case of
|
39
|
+
software used on network servers, this result may fail to come about.
|
40
|
+
The GNU General Public License permits making a modified version and
|
41
|
+
letting the public access it on a server without ever releasing its
|
42
|
+
source code to the public.
|
43
|
+
|
44
|
+
The GNU Affero General Public License is designed specifically to
|
45
|
+
ensure that, in such cases, the modified source code becomes available
|
46
|
+
to the community. It requires the operator of a network server to
|
47
|
+
provide the source code of the modified version running there to the
|
48
|
+
users of that server. Therefore, public use of a modified version, on
|
49
|
+
a publicly accessible server, gives the public access to the source
|
50
|
+
code of the modified version.
|
51
|
+
|
52
|
+
An older license, called the Affero General Public License and
|
53
|
+
published by Affero, was designed to accomplish similar goals. This is
|
54
|
+
a different license, not a version of the Affero GPL, but Affero has
|
55
|
+
released a new version of the Affero GPL which permits relicensing under
|
56
|
+
this license.
|
69
57
|
|
70
58
|
The precise terms and conditions for copying, distribution and
|
71
59
|
modification follow.
|
@@ -74,7 +62,7 @@
|
|
74
62
|
|
75
63
|
0. Definitions.
|
76
64
|
|
77
|
-
"This License" refers to version 3 of the GNU General Public License.
|
65
|
+
"This License" refers to version 3 of the GNU Affero General Public License.
|
78
66
|
|
79
67
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
80
68
|
works, such as semiconductor masks.
|
@@ -551,35 +539,45 @@
|
|
551
539
|
the Program, the only way you could satisfy both those terms and this
|
552
540
|
License would be to refrain entirely from conveying the Program.
|
553
541
|
|
554
|
-
13. Use with the GNU
|
542
|
+
13. Remote Network Interaction; Use with the GNU General Public License.
|
543
|
+
|
544
|
+
Notwithstanding any other provision of this License, if you modify the
|
545
|
+
Program, your modified version must prominently offer all users
|
546
|
+
interacting with it remotely through a computer network (if your version
|
547
|
+
supports such interaction) an opportunity to receive the Corresponding
|
548
|
+
Source of your version by providing access to the Corresponding Source
|
549
|
+
from a network server at no charge, through some standard or customary
|
550
|
+
means of facilitating copying of software. This Corresponding Source
|
551
|
+
shall include the Corresponding Source for any work covered by version 3
|
552
|
+
of the GNU General Public License that is incorporated pursuant to the
|
553
|
+
following paragraph.
|
555
554
|
|
556
555
|
Notwithstanding any other provision of this License, you have
|
557
556
|
permission to link or combine any covered work with a work licensed
|
558
|
-
under version 3 of the GNU
|
557
|
+
under version 3 of the GNU General Public License into a single
|
559
558
|
combined work, and to convey the resulting work. The terms of this
|
560
559
|
License will continue to apply to the part which is the covered work,
|
561
|
-
but the
|
562
|
-
|
563
|
-
combination as such.
|
560
|
+
but the work with which it is combined will remain governed by version
|
561
|
+
3 of the GNU General Public License.
|
564
562
|
|
565
563
|
14. Revised Versions of this License.
|
566
564
|
|
567
565
|
The Free Software Foundation may publish revised and/or new versions of
|
568
|
-
the GNU General Public License from time to time. Such new versions
|
569
|
-
be similar in spirit to the present version, but may differ in detail to
|
566
|
+
the GNU Affero General Public License from time to time. Such new versions
|
567
|
+
will be similar in spirit to the present version, but may differ in detail to
|
570
568
|
address new problems or concerns.
|
571
569
|
|
572
570
|
Each version is given a distinguishing version number. If the
|
573
|
-
Program specifies that a certain numbered version of the GNU General
|
571
|
+
Program specifies that a certain numbered version of the GNU Affero General
|
574
572
|
Public License "or any later version" applies to it, you have the
|
575
573
|
option of following the terms and conditions either of that numbered
|
576
574
|
version or of any later version published by the Free Software
|
577
575
|
Foundation. If the Program does not specify a version number of the
|
578
|
-
GNU General Public License, you may choose any version ever published
|
576
|
+
GNU Affero General Public License, you may choose any version ever published
|
579
577
|
by the Free Software Foundation.
|
580
578
|
|
581
579
|
If the Program specifies that a proxy can decide which future
|
582
|
-
versions of the GNU General Public License can be used, that proxy's
|
580
|
+
versions of the GNU Affero General Public License can be used, that proxy's
|
583
581
|
public statement of acceptance of a version permanently authorizes you
|
584
582
|
to choose that version for the Program.
|
585
583
|
|
@@ -637,40 +635,29 @@
|
|
637
635
|
Copyright (C) <year> <name of author>
|
638
636
|
|
639
637
|
This program is free software: you can redistribute it and/or modify
|
640
|
-
it under the terms of the GNU General Public License as published by
|
638
|
+
it under the terms of the GNU Affero General Public License as published by
|
641
639
|
the Free Software Foundation, either version 3 of the License, or
|
642
640
|
(at your option) any later version.
|
643
641
|
|
644
642
|
This program is distributed in the hope that it will be useful,
|
645
643
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
646
644
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
647
|
-
GNU General Public License for more details.
|
645
|
+
GNU Affero General Public License for more details.
|
648
646
|
|
649
|
-
You should have received a copy of the GNU General Public License
|
647
|
+
You should have received a copy of the GNU Affero General Public License
|
650
648
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
651
649
|
|
652
650
|
Also add information on how to contact you by electronic and paper mail.
|
653
651
|
|
654
|
-
If
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
The hypothetical commands `show w' and `show c' should show the appropriate
|
663
|
-
parts of the General Public License. Of course, your program's commands
|
664
|
-
might be different; for a GUI interface, you would use an "about box".
|
652
|
+
If your software can interact with users remotely through a computer
|
653
|
+
network, you should also make sure that it provides a way for users to
|
654
|
+
get its source. For example, if your program is a web application, its
|
655
|
+
interface could display a "Source" link that leads users to an archive
|
656
|
+
of the code. There are many ways you could offer source, and different
|
657
|
+
solutions will be better for different programs; see section 13 for the
|
658
|
+
specific requirements.
|
665
659
|
|
666
660
|
You should also get your employer (if you work as a programmer) or school,
|
667
661
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
668
|
-
For more information on this, and how to apply and follow the GNU
|
662
|
+
For more information on this, and how to apply and follow the GNU AGPL, see
|
669
663
|
<http://www.gnu.org/licenses/>.
|
670
|
-
|
671
|
-
The GNU General Public License does not permit incorporating your program
|
672
|
-
into proprietary programs. If your program is a subroutine library, you
|
673
|
-
may consider it more useful to permit linking proprietary applications with
|
674
|
-
the library. If this is what you want to do, use the GNU Lesser General
|
675
|
-
Public License instead of this License. But first, please read
|
676
|
-
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
== VERSION
|
4
4
|
|
5
|
-
This documentation refers to added_methods version 0.1.
|
5
|
+
This documentation refers to added_methods version 0.1.1
|
6
6
|
|
7
7
|
|
8
8
|
== DESCRIPTION
|
@@ -41,7 +41,7 @@ Example:
|
|
41
41
|
== LINKS
|
42
42
|
|
43
43
|
<b></b>
|
44
|
-
Source code::
|
44
|
+
Source code:: http://github.com/blackwinter/added_methods
|
45
45
|
|
46
46
|
|
47
47
|
== AUTHORS
|
@@ -51,16 +51,17 @@ Source code:: <http://github.com/blackwinter/added_methods>
|
|
51
51
|
|
52
52
|
== LICENSE AND COPYRIGHT
|
53
53
|
|
54
|
-
Copyright (C) 2007-
|
54
|
+
Copyright (C) 2007-2011 Jens Wille
|
55
55
|
|
56
|
-
added_methods is free software: you can redistribute it and/or modify it
|
57
|
-
the terms of the GNU General Public License as published by
|
58
|
-
Foundation, either version 3 of the License, or (at your
|
59
|
-
version.
|
56
|
+
added_methods is free software: you can redistribute it and/or modify it
|
57
|
+
under the terms of the GNU Affero General Public License as published by
|
58
|
+
the Free Software Foundation, either version 3 of the License, or (at your
|
59
|
+
option) any later version.
|
60
60
|
|
61
61
|
added_methods is distributed in the hope that it will be useful, but WITHOUT
|
62
62
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
63
|
-
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
63
|
+
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
64
|
+
details.
|
64
65
|
|
65
|
-
You should have received a copy of the GNU General Public License
|
66
|
-
added_methods. If not, see <http://www.gnu.org/licenses/>.
|
66
|
+
You should have received a copy of the GNU Affero General Public License
|
67
|
+
along with added_methods. If not, see <http://www.gnu.org/licenses/>.
|
data/Rakefile
CHANGED
@@ -1,26 +1,18 @@
|
|
1
|
-
require %q{lib/added_methods/version}
|
1
|
+
require File.expand_path(%q{../lib/added_methods/version}, __FILE__)
|
2
2
|
|
3
3
|
begin
|
4
4
|
require 'hen'
|
5
5
|
|
6
6
|
Hen.lay! {{
|
7
|
-
:rubyforge => {
|
8
|
-
:project => '', #%q{added_methods},
|
9
|
-
:package => %q{added_methods},
|
10
|
-
:rdoc_dir => nil
|
11
|
-
},
|
12
|
-
|
13
7
|
:gem => {
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
17
|
-
:
|
18
|
-
:
|
19
|
-
:
|
8
|
+
:name => %q{added_methods},
|
9
|
+
:version => AddedMethods::VERSION,
|
10
|
+
:summary => %q{Watches for added methods and records them.},
|
11
|
+
:author => %q{Jens Wille},
|
12
|
+
:email => %q{jens.wille@uni-koeln.de},
|
13
|
+
:homepage => :blackwinter
|
20
14
|
}
|
21
15
|
}}
|
22
|
-
rescue LoadError
|
23
|
-
|
16
|
+
rescue LoadError => err
|
17
|
+
warn "Please install the `hen' gem. (#{err})"
|
24
18
|
end
|
25
|
-
|
26
|
-
### Place your custom Rake tasks here.
|
@@ -0,0 +1,142 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# A component of added_methods, the added method watcher. #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@uni-koeln.de> #
|
10
|
+
# #
|
11
|
+
# added_methods is free software; you can redistribute it and/or modify it #
|
12
|
+
# under the terms of the GNU Affero General Public License as published by #
|
13
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
14
|
+
# option) any later version. #
|
15
|
+
# #
|
16
|
+
# added_methods is distributed in the hope that it will be useful, but #
|
17
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
|
18
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
|
19
|
+
# License for more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with added_methods. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module AddedMethods
|
28
|
+
|
29
|
+
class AddedMethod
|
30
|
+
|
31
|
+
attr_accessor :base, :klass, :name, :singleton, :file, :line, :def, :time
|
32
|
+
|
33
|
+
def initialize(args = {})
|
34
|
+
self.time = Time.now
|
35
|
+
|
36
|
+
args.each { |key, value|
|
37
|
+
send("#{key}=", value)
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method 'class=', 'klass='
|
42
|
+
alias_method :singleton?, :singleton
|
43
|
+
|
44
|
+
def [](key)
|
45
|
+
send(key.to_sym == :class ? :klass : key)
|
46
|
+
end
|
47
|
+
|
48
|
+
def source
|
49
|
+
@source ||= extract_source
|
50
|
+
end
|
51
|
+
|
52
|
+
def r2r_source
|
53
|
+
@r2r_source ||= extract_source_from_r2r
|
54
|
+
end
|
55
|
+
|
56
|
+
def extract_source(num_lines = nil)
|
57
|
+
lines = extract_source_from_script_lines(num_lines)
|
58
|
+
|
59
|
+
# try to make sure we correctly extracted the method
|
60
|
+
# definition, otherwise try to get it from Ruby2Ruby
|
61
|
+
if lines && lines.first =~ /\b#{name}\b/
|
62
|
+
lines
|
63
|
+
else
|
64
|
+
extract_source_from_r2r || lines
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_s
|
69
|
+
str = "# File #{file}, line #{line}"
|
70
|
+
|
71
|
+
case lines = source
|
72
|
+
when Array
|
73
|
+
num = line - 1
|
74
|
+
width = (num + lines.size).to_s.length
|
75
|
+
|
76
|
+
lines.map! { |l| "%0#{width}d: %s" % [num += 1, l] }
|
77
|
+
|
78
|
+
"#{' ' * width} #{str}\n#{lines}"
|
79
|
+
when String
|
80
|
+
"#{str}#{lines}"
|
81
|
+
else
|
82
|
+
str
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def extract_source_from_script_lines(num_lines = nil)
|
89
|
+
return unless Object.const_defined?(:SCRIPT_LINES__)
|
90
|
+
return unless script_lines = SCRIPT_LINES__[file]
|
91
|
+
|
92
|
+
start, from, to = line - 1, line, script_lines.size - 1
|
93
|
+
|
94
|
+
# suppose we're already in a block
|
95
|
+
in_block = 1
|
96
|
+
|
97
|
+
num_lines ||= case definition = script_lines[start]
|
98
|
+
# def ... end, or do ... end style block
|
99
|
+
when /\b(?:def|do)\b/
|
100
|
+
definition =~ /\bend\b/ ? 1 : begin
|
101
|
+
from.upto(to) { |i|
|
102
|
+
case line = script_lines[i]
|
103
|
+
when /[^;\s]\s+(?:if|unless)\b/
|
104
|
+
# probably postfix conditional, ignore
|
105
|
+
when /\b(?:if|unless|while|until|def|do)\b/
|
106
|
+
in_block += 1
|
107
|
+
when /\bend\b/
|
108
|
+
in_block -= 1
|
109
|
+
end
|
110
|
+
|
111
|
+
break i - start + 1 if in_block.zero?
|
112
|
+
}
|
113
|
+
end
|
114
|
+
# { ... } style block
|
115
|
+
when /\bdefine_method\b/
|
116
|
+
from.upto(to) { |i|
|
117
|
+
line = script_lines[i]
|
118
|
+
|
119
|
+
in_block += line.count('{')
|
120
|
+
in_block -= line.count('}')
|
121
|
+
|
122
|
+
break i - start + 1 if in_block.zero?
|
123
|
+
}
|
124
|
+
else
|
125
|
+
1
|
126
|
+
end
|
127
|
+
|
128
|
+
script_lines[start, num_lines]
|
129
|
+
end
|
130
|
+
|
131
|
+
# Use Ruby2Ruby as a last resort. But note that it only
|
132
|
+
# ever finds the *latest*, i.e. currently active, method
|
133
|
+
# definition, not necessarily the one we're looking for.
|
134
|
+
def extract_source_from_r2r
|
135
|
+
if Object.const_defined?(:Ruby2Ruby)
|
136
|
+
" [R2R]\n#{Ruby2Ruby.translate(klass, name)}"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
data/lib/added_methods.rb
CHANGED
@@ -3,23 +3,23 @@
|
|
3
3
|
# #
|
4
4
|
# added_methods - watch for added methods and record them. #
|
5
5
|
# #
|
6
|
-
# Copyright (C) 2007-
|
6
|
+
# Copyright (C) 2007-2011 Jens Wille #
|
7
7
|
# #
|
8
8
|
# Authors: #
|
9
9
|
# Jens Wille <jens.wille@uni-koeln.de> #
|
10
10
|
# #
|
11
11
|
# added_methods is free software; you can redistribute it and/or modify it #
|
12
|
-
# under the terms of the GNU General Public License as published by
|
13
|
-
# Software Foundation; either version 3 of the License, or (at your
|
14
|
-
# any later version.
|
12
|
+
# under the terms of the GNU Affero General Public License as published by #
|
13
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
14
|
+
# option) any later version. #
|
15
15
|
# #
|
16
16
|
# added_methods is distributed in the hope that it will be useful, but #
|
17
17
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
|
18
|
-
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
19
|
-
# for more details.
|
18
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
|
19
|
+
# License for more details. #
|
20
20
|
# #
|
21
|
-
# You should have received a copy of the GNU General Public License
|
22
|
-
# with added_methods. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with added_methods. If not, see <http://www.gnu.org/licenses/>. #
|
23
23
|
# #
|
24
24
|
###############################################################################
|
25
25
|
#++
|
@@ -29,6 +29,8 @@ begin
|
|
29
29
|
rescue LoadError
|
30
30
|
end
|
31
31
|
|
32
|
+
require 'added_methods/added_method'
|
33
|
+
|
32
34
|
# TODO:
|
33
35
|
#
|
34
36
|
# - multi-line statements in irb w/o ruby2ruby? (=> extract_source)
|
@@ -40,119 +42,6 @@ module AddedMethods
|
|
40
42
|
|
41
43
|
HISTFILENAME = '(Readline::HISTORY)'.freeze unless const_defined?(:HISTFILENAME)
|
42
44
|
|
43
|
-
class AddedMethod
|
44
|
-
|
45
|
-
attr_accessor :base, :klass, :name, :singleton, :file, :line, :def, :time
|
46
|
-
|
47
|
-
def initialize(args = {})
|
48
|
-
self.time = Time.now
|
49
|
-
|
50
|
-
args.each { |key, value|
|
51
|
-
send("#{key}=", value)
|
52
|
-
}
|
53
|
-
end
|
54
|
-
|
55
|
-
alias_method 'class=', 'klass='
|
56
|
-
alias_method :singleton?, :singleton
|
57
|
-
|
58
|
-
def [](key)
|
59
|
-
send(key.to_sym == :class ? :klass : key)
|
60
|
-
end
|
61
|
-
|
62
|
-
def source
|
63
|
-
@source ||= extract_source
|
64
|
-
end
|
65
|
-
|
66
|
-
def r2r_source
|
67
|
-
@r2r_source ||= extract_source_from_r2r
|
68
|
-
end
|
69
|
-
|
70
|
-
def extract_source(num_lines = nil)
|
71
|
-
lines = extract_source_from_script_lines(num_lines)
|
72
|
-
|
73
|
-
# try to make sure we correctly extracted the method
|
74
|
-
# definition, otherwise try to get it from Ruby2Ruby
|
75
|
-
if lines && lines.first =~ /\b#{name}\b/
|
76
|
-
lines
|
77
|
-
else
|
78
|
-
extract_source_from_r2r || lines
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def to_s
|
83
|
-
str = "# File #{file}, line #{line}"
|
84
|
-
|
85
|
-
case lines = source
|
86
|
-
when Array
|
87
|
-
num = line - 1
|
88
|
-
width = (num + lines.size).to_s.length
|
89
|
-
|
90
|
-
lines.map! { |l| "%0#{width}d: %s" % [num += 1, l] }
|
91
|
-
|
92
|
-
"#{' ' * width} #{str}\n#{lines}"
|
93
|
-
when String
|
94
|
-
"#{str}#{lines}"
|
95
|
-
else
|
96
|
-
str
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
private
|
101
|
-
|
102
|
-
def extract_source_from_script_lines(num_lines = nil)
|
103
|
-
return unless Object.const_defined?(:SCRIPT_LINES__)
|
104
|
-
return unless script_lines = SCRIPT_LINES__[file]
|
105
|
-
|
106
|
-
start, from, to = line - 1, line, script_lines.size - 1
|
107
|
-
|
108
|
-
# suppose we're already in a block
|
109
|
-
in_block = 1
|
110
|
-
|
111
|
-
num_lines ||= case definition = script_lines[start]
|
112
|
-
# def ... end, or do ... end style block
|
113
|
-
when /\b(?:def|do)\b/
|
114
|
-
definition =~ /\bend\b/ ? 1 : begin
|
115
|
-
from.upto(to) { |i|
|
116
|
-
case line = script_lines[i]
|
117
|
-
when /[^;\s]\s+(?:if|unless)\b/
|
118
|
-
# probably postfix conditional, ignore
|
119
|
-
when /\b(?:if|unless|while|until|def|do)\b/
|
120
|
-
in_block += 1
|
121
|
-
when /\bend\b/
|
122
|
-
in_block -= 1
|
123
|
-
end
|
124
|
-
|
125
|
-
break i - start + 1 if in_block.zero?
|
126
|
-
}
|
127
|
-
end
|
128
|
-
# { ... } style block
|
129
|
-
when /\bdefine_method\b/
|
130
|
-
from.upto(to) { |i|
|
131
|
-
line = script_lines[i]
|
132
|
-
|
133
|
-
in_block += line.count('{')
|
134
|
-
in_block -= line.count('}')
|
135
|
-
|
136
|
-
break i - start + 1 if in_block.zero?
|
137
|
-
}
|
138
|
-
else
|
139
|
-
1
|
140
|
-
end
|
141
|
-
|
142
|
-
script_lines[start, num_lines]
|
143
|
-
end
|
144
|
-
|
145
|
-
# Use Ruby2Ruby as a last resort. But note that it only
|
146
|
-
# ever finds the *latest*, i.e. currently active, method
|
147
|
-
# definition, not necessarily the one we're looking for.
|
148
|
-
def extract_source_from_r2r
|
149
|
-
if Object.const_defined?(:Ruby2Ruby)
|
150
|
-
" [R2R]\n#{Ruby2Ruby.translate(klass, name)}"
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
end
|
155
|
-
|
156
45
|
def init(regexp = nil, klasses = [], &block)
|
157
46
|
init_script_lines
|
158
47
|
patch_readline_history
|
@@ -0,0 +1,120 @@
|
|
1
|
+
describe AddedMethods do
|
2
|
+
|
3
|
+
describe "with an added instance method" do
|
4
|
+
|
5
|
+
before :all do
|
6
|
+
class SomeClass; def some_method; end; end
|
7
|
+
|
8
|
+
@added_methods = AddedMethods.all_methods[SomeClass].values.flatten
|
9
|
+
@added_method = @added_methods.last
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should record it" do
|
13
|
+
@added_methods.should have(1).item
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should find it by name" do
|
17
|
+
AddedMethods.find(:name => 'some_method').first.should be_eql(@added_method)
|
18
|
+
AddedMethods.find_by_name('some_method').first.should be_eql(@added_method)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should find it by class" do
|
22
|
+
AddedMethods.find(:class => SomeClass).first.should be_eql(@added_method)
|
23
|
+
AddedMethods.find_by_class(SomeClass).first.should be_eql(@added_method)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should find it by either name or class" do
|
27
|
+
AddedMethods.find_one_by_name_or_class('some_method').should be_eql(@added_method)
|
28
|
+
AddedMethods.find_one_by_name_or_class(SomeClass).should be_eql(@added_method)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should find it conveniently by either name or class" do
|
32
|
+
AddedMethods['some_method'].should be_eql(@added_method)
|
33
|
+
AddedMethods[SomeClass].should be_eql(@added_method)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should find it by file" do
|
37
|
+
AddedMethods.find(:file => __FILE__).first.should be_eql(@added_method)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should find it by regexp" do
|
41
|
+
AddedMethods.find(:name => /some_method/).first.should be_eql(@added_method)
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "added method" do
|
45
|
+
|
46
|
+
it "should be an AddedMethod" do
|
47
|
+
@added_method.should be_an_instance_of(AddedMethods::AddedMethod)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should know its name" do
|
51
|
+
@added_method.name.should == 'some_method'
|
52
|
+
@added_method[:name].should == 'some_method'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should know its class" do
|
56
|
+
@added_method.klass.should == SomeClass
|
57
|
+
@added_method[:klass].should == SomeClass
|
58
|
+
@added_method[:class].should == SomeClass
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should know its file" do
|
62
|
+
@added_method.file.should == __FILE__
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should know if it's a singleton method" do
|
66
|
+
@added_method.should_not be_singleton
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should know its source" do
|
70
|
+
@added_method.source.should == [" class SomeClass; def some_method; end; end\n"]
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should know its source from Ruby2Ruby" do
|
74
|
+
@added_method.r2r_source.should == " [R2R]\ndef some_method\n # do nothing\nend"
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "with an added class method" do
|
82
|
+
|
83
|
+
before :all do
|
84
|
+
class SomeClass; def self.some_class_method; end; end
|
85
|
+
|
86
|
+
@added_methods = AddedMethods.all_methods[SomeClass].values.flatten
|
87
|
+
@added_method = @added_methods.last
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should record it" do
|
91
|
+
@added_methods.should have_at_least(1).item
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "added method" do
|
95
|
+
|
96
|
+
it "should be an AddedMethod" do
|
97
|
+
@added_method.should be_an_instance_of(AddedMethods::AddedMethod)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should know its name" do
|
101
|
+
@added_method.name.should == 'some_class_method'
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should know its class" do
|
105
|
+
@added_method.klass.should == SomeClass
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should know its file" do
|
109
|
+
@added_method.file.should == __FILE__
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should know if it's a singleton method" do
|
113
|
+
@added_method.should be_singleton
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: added_methods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Jens Wille
|
@@ -9,8 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
13
|
-
default_executable:
|
18
|
+
date: 2011-04-29 00:00:00 Z
|
14
19
|
dependencies: []
|
15
20
|
|
16
21
|
description: Watches for added methods and records them.
|
@@ -20,50 +25,60 @@ executables: []
|
|
20
25
|
extensions: []
|
21
26
|
|
22
27
|
extra_rdoc_files:
|
28
|
+
- README
|
23
29
|
- COPYING
|
24
30
|
- ChangeLog
|
25
|
-
- README
|
26
31
|
files:
|
27
|
-
- lib/added_methods.rb
|
28
|
-
- lib/added_methods/version.rb
|
32
|
+
- lib/added_methods/added_method.rb
|
29
33
|
- lib/added_methods/init.rb
|
34
|
+
- lib/added_methods/version.rb
|
35
|
+
- lib/added_methods.rb
|
36
|
+
- README
|
37
|
+
- ChangeLog
|
30
38
|
- Rakefile
|
31
39
|
- COPYING
|
32
|
-
-
|
33
|
-
-
|
34
|
-
|
40
|
+
- spec/spec.opts
|
41
|
+
- spec/added_methods_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
35
43
|
homepage: http://github.com/blackwinter/added_methods
|
44
|
+
licenses: []
|
45
|
+
|
36
46
|
post_install_message:
|
37
47
|
rdoc_options:
|
38
|
-
- --
|
39
|
-
-
|
48
|
+
- --charset
|
49
|
+
- UTF-8
|
40
50
|
- --title
|
41
|
-
- added_methods Application documentation
|
51
|
+
- added_methods Application documentation (v0.1.1)
|
42
52
|
- --main
|
43
53
|
- README
|
44
|
-
- --
|
45
|
-
- UTF-8
|
54
|
+
- --line-numbers
|
46
55
|
- --all
|
47
56
|
require_paths:
|
48
57
|
- lib
|
49
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
50
60
|
requirements:
|
51
61
|
- - ">="
|
52
62
|
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
53
66
|
version: "0"
|
54
|
-
version:
|
55
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
56
69
|
requirements:
|
57
70
|
- - ">="
|
58
71
|
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
59
75
|
version: "0"
|
60
|
-
version:
|
61
76
|
requirements: []
|
62
77
|
|
63
|
-
rubyforge_project:
|
64
|
-
rubygems_version: 1.
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.7.2
|
65
80
|
signing_key:
|
66
|
-
specification_version:
|
81
|
+
specification_version: 3
|
67
82
|
summary: Watches for added methods and records them.
|
68
83
|
test_files: []
|
69
84
|
|