ruby-nuggets 0.2.7.259 → 0.2.8.262

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to ruby-nuggets version 0.2.7
5
+ This documentation refers to ruby-nuggets version 0.2.8
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -1,3 +1,30 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2008 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@uni-koeln.de> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU General Public License as published by the Free #
14
+ # Software Foundation; either version 3 of the License, or (at your option) #
15
+ # any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
20
+ # more details. #
21
+ # #
22
+ # You should have received a copy of the GNU General Public License along #
23
+ # with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
1
28
  begin
2
29
  require 'ruby2ruby'
3
30
  rescue LoadError
@@ -8,6 +35,33 @@ module Util
8
35
  # Watch for added methods and record them. Inspired by unroller,
9
36
  # <http://unroller.rubyforge.org/classes/Unroller.html#M000034>.
10
37
  #
38
+ # Example:
39
+ #
40
+ # require 'rubygems'
41
+ # require 'nuggets/util/added_methods/init'
42
+ #
43
+ # require 'some/library/or/whatever'
44
+ #
45
+ # matches = Util::AddedMethods.find(
46
+ # :name => 'method_name',
47
+ # :class => SomeClass # optional
48
+ # )
49
+ #
50
+ # # get the class(es) where matching method(s) were defined
51
+ # matches.each { |am| puts am[:class] # or am.klass }
52
+ #
53
+ # # assume the first one is the one we're looking for
54
+ # am = matches.first
55
+ #
56
+ # # is it a singleton method?
57
+ # puts am.singleton
58
+ #
59
+ # # where exactly has it been defined?
60
+ # puts "#{am.file}, line #{am.line}"
61
+ #
62
+ # # now get its source
63
+ # puts am # implies #to_s, you can also call #extract_source directly
64
+ #
11
65
  # TODO:
12
66
  # - multi-line statements in irb w/o ruby2ruby? (=> extract_source)
13
67
  # - polishing!
@@ -18,23 +72,39 @@ module Util
18
72
 
19
73
  HISTFILENAME = '(Readline::HISTORY)'.freeze
20
74
 
21
- class AddedMethod < Hash
75
+ class AddedMethod
76
+
77
+ attr_accessor :base, :klass, :name, :singleton, :file, :line, :def
78
+
79
+ def initialize(args = {})
80
+ args.each { |key, value|
81
+ send("#{key}=", value)
82
+ }
83
+ end
84
+
85
+ alias_method 'class=', 'klass='
86
+ alias_method :singleton?, :singleton
22
87
 
23
- def initialize(*args)
24
- update(*args) unless args.empty?
88
+ def [](key)
89
+ send(key.to_sym == :class ? :klass : key)
90
+ end
91
+
92
+ def source
93
+ @source ||= extract_source
25
94
  end
26
95
 
27
96
  def extract_source(num_lines = nil)
28
97
  lines = extract_source_from_script_lines(num_lines)
29
98
 
30
- # try to make sure we correctly extracted the method definition
99
+ # try to make sure we correctly extracted the method
100
+ # definition, otherwise try to get it from Ruby2Ruby
31
101
  lines.first =~ /\b#{name}\b/ ? lines : extract_source_from_r2r || lines
32
102
  end
33
103
 
34
- def to_s(num_lines = nil)
104
+ def to_s
35
105
  str = "# File #{file}, line #{line}"
36
106
 
37
- case lines = extract_source(num_lines)
107
+ case lines = source
38
108
  when Array
39
109
  num = line - 1
40
110
  width = (num + lines.size).to_s.length
@@ -49,14 +119,6 @@ module Util
49
119
  end
50
120
  end
51
121
 
52
- def klass
53
- self[:class]
54
- end
55
-
56
- def method_missing(method, *args)
57
- has_key?(method) ? self[method] : super
58
- end
59
-
60
122
  private
61
123
 
62
124
  def extract_source_from_script_lines(num_lines = nil)
@@ -102,13 +164,13 @@ module Util
102
164
  script_lines[start, num_lines]
103
165
  end
104
166
 
167
+ # Use Ruby2Ruby as a last resort. But note that it only
168
+ # ever finds the *latest*, i.e. currently active, method
169
+ # definition, not necessarily the one we're looking for.
105
170
  def extract_source_from_r2r
106
- return unless Object.const_defined?(:Ruby2Ruby)
107
-
108
- # use Ruby2Ruby as a last resort. but note that it only
109
- # ever finds the *latest*, i.e. currently active, method
110
- # definition, not necessarily the one we're looking for.
111
- " [R2R]\n#{Ruby2Ruby.translate(klass, name)}"
171
+ if Object.const_defined?(:Ruby2Ruby)
172
+ " [R2R]\n#{Ruby2Ruby.translate(klass, name)}"
173
+ end
112
174
  end
113
175
 
114
176
  end
@@ -139,7 +201,7 @@ module Util
139
201
  raise TypeError, "wrong argument type #{klasses.class} (expected container object)" unless klasses.respond_to?(:empty?) && klasses.respond_to?(:include?)
140
202
 
141
203
  callbacks << [name, lambda { |am, callstack, inner_block|
142
- method, klass = am.values_at(:name, :class)
204
+ method, klass = am.name, am.klass
143
205
 
144
206
  return if %w[method_added singleton_method_added].include?(method)
145
207
 
@@ -150,7 +212,7 @@ module Util
150
212
  outer_block[am] if outer_block
151
213
  inner_block[am] if inner_block
152
214
  else
153
- msg = "[#{am.base}] Adding #{'singleton ' if am.singleton}method #{klass}##{method}"
215
+ msg = "[#{am.base}] Adding #{'singleton ' if am.singleton?}method #{klass}##{method}"
154
216
 
155
217
  msg << if irb?(callstack)
156
218
  " in (irb:#{IRB.conf[:MAIN_CONTEXT].instance_variable_get(:@line_no)})"
@@ -204,15 +266,12 @@ module Util
204
266
  next unless file_condition.is_a?(Regexp) ? file =~ file_condition : file == file_condition
205
267
  end
206
268
 
207
- entries.each { |entry|
208
- results << entry.merge(
209
- :class => klass,
210
- :file => file
211
- ) if conditions.all? { |key, value|
269
+ entries.each { |am|
270
+ results << am if conditions.all? { |key, value|
212
271
  case value
213
- when Array: value.include?(entry[key])
214
- when Regexp: entry[key].to_s =~ value
215
- else entry[key] == value
272
+ when Array: value.include?(am[key])
273
+ when Regexp: am[key].to_s =~ value
274
+ else am[key] == value
216
275
  end
217
276
  }
218
277
  }
@@ -263,9 +322,7 @@ module Util
263
322
  def init_all_methods
264
323
  unless const_defined?(:ALL_METHODS)
265
324
  const_set(:ALL_METHODS, Hash.new { |h, k|
266
- h[k] = Hash.new { |i, j|
267
- i[j] = []
268
- }
325
+ h[k] = Hash.new { |i, j| i[j] = [] }
269
326
  })
270
327
  end
271
328
  end
@@ -327,18 +384,18 @@ module Util
327
384
 
328
385
  if irb?(callstack)
329
386
  am.update(
330
- :file => HISTFILENAME,
331
- :line => Readline::HISTORY.size,
332
- :source => begin Readline::HISTORY[-1] rescue IndexError end
387
+ :file => HISTFILENAME,
388
+ :line => Readline::HISTORY.size,
389
+ :def => begin Readline::HISTORY[-1] rescue IndexError end
333
390
  )
334
391
  else
335
392
  file, line, _ = where(callstack).split(':')
336
393
  line = line.to_i
337
394
 
338
395
  am.update(
339
- :file => file,
340
- :line => line,
341
- :source => (SCRIPT_LINES__[file] || [])[line - 1]
396
+ :file => file,
397
+ :line => line,
398
+ :def => (SCRIPT_LINES__[file] || [])[line - 1]
342
399
  )
343
400
  end
344
401
 
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 2
7
- TINY = 7
7
+ TINY = 8
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7.259
4
+ version: 0.2.8.262
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-24 00:00:00 +02:00
12
+ date: 2008-07-27 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15