srizzo-irber 0.0.3 → 0.0.4

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/irber/papi.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'irber/api'
2
+
3
+ module Kernel
4
+
5
+ private
6
+
7
+ def papi object
8
+
9
+ signatures = object.api
10
+
11
+ max_length = signatures.collect {|signature| signature[0].size}.max
12
+
13
+ formatted_signatures = signatures.collect do |signature|
14
+ "#{signature[0].rjust(max_length)} #{signature[1]} #{signature[2]}#{signature[3]}"
15
+ end
16
+
17
+ puts formatted_signatures.join("\n")
18
+
19
+ end
20
+
21
+ end
22
+
data/lib/irber/psrc.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'irber/src'
2
+
3
+ module Kernel
4
+
5
+ private
6
+
7
+ def psrc object, method = nil
8
+ puts object.src method
9
+ end
10
+
11
+ end
@@ -1,8 +1,9 @@
1
1
  require 'pp'
2
-
2
+ require 'irber/psrc'
3
+ require 'irber/papi'
3
4
 
4
5
  module Irber
5
- module Printing
6
+ module SelfInspect
6
7
 
7
8
  module InstanceMethods
8
9
  def puts_it
@@ -14,6 +15,15 @@ module Irber
14
15
  def pp_it
15
16
  pp self
16
17
  end
18
+
19
+ def psrc_it *args
20
+ psrc self, *args
21
+ end
22
+
23
+ def papi_it
24
+ papi self
25
+ end
26
+
17
27
  end
18
28
 
19
29
  def self.included(receiver)
@@ -25,6 +35,6 @@ end
25
35
 
26
36
 
27
37
  class Object
28
- include Irber::Printing
38
+ include Irber::SelfInspect
29
39
  end
30
40
 
@@ -0,0 +1,45 @@
1
+ require 'irber/src'
2
+
3
+ module Irber
4
+ module Signature
5
+ module InstanceMethods
6
+ def signature
7
+ owner, level, method_name = self.inspect.match(/<(?:Unbound)?Method: ([^.#]*)(\.|#)(.*)>/).captures
8
+ owner = owner[/^(?:.*\()?([^()]*)\)?$/, 1]
9
+
10
+ begin
11
+ params = "(#{self.owner.src(self.name)[/def [^(]*\(([^)]*)\)\n/, 1]})"
12
+ rescue Irber::Src::ParseError
13
+ if self.arity > 0
14
+ req_args = self.arity
15
+ params = "(#{(1..req_args).collect {|i| "arg#{i}"}.join(", ")})"
16
+ elsif self.arity < 0
17
+ req_args = (-self.arity) -1
18
+ params = "("
19
+ params += "#{(1..req_args).collect {|i| "arg#{i}"}.join(", ")}, " if req_args > 0
20
+ params += "var_args = nil"
21
+ params += ")"
22
+ else
23
+ params = "()"
24
+ end
25
+
26
+ end
27
+
28
+ [owner, level, method_name, params]
29
+ end
30
+ end
31
+
32
+ def self.included(receiver)
33
+ receiver.send :include, InstanceMethods
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ class Method
40
+ include Irber::Signature
41
+ end
42
+
43
+ class UnboundMethod
44
+ include Irber::Signature
45
+ end
data/lib/irber/src.rb CHANGED
@@ -1,12 +1,50 @@
1
- require 'irber/to_code'
1
+ require 'parse_tree'
2
+ require 'ruby2ruby'
2
3
 
4
+ module Irber
5
+ module Src
6
+
7
+ class ParseError < StandardError; end
8
+
9
+ module InstanceMethods
10
+ def src method = nil
11
+
12
+ if method
13
+
14
+ if (the_method = self.method(method) rescue nil)
15
+ receiver = the_method.owner
16
+ elsif (the_method = self.instance_method(method) if self.respond_to? :instance_method)
17
+ receiver = the_method.owner
18
+ else
19
+ throw NameError.new "couldn't find method `#{method}' for `#{self}' "
20
+ end
21
+
22
+ Ruby2Ruby.new.process(ParseTree.translate(receiver, method)) rescue raise ParseError.new("couldn't parse method")
23
+
24
+ else
25
+ if self.class == Class || self.class == Module
26
+ receiver = self
27
+ else
28
+ receiver = self.class
29
+ end
3
30
 
4
- module Kernel
5
-
6
- private
7
-
8
- def src object, method = nil
9
- puts object.to_code method
31
+ Ruby2Ruby.new.process(ParseTree.translate(receiver)) rescue raise ParseError.new("couldn't parse class")
32
+
33
+ end
34
+
35
+ end
36
+ end
37
+
38
+ def self.included(receiver)
39
+ receiver.send :include, InstanceMethods
40
+ end
10
41
  end
11
42
 
12
43
  end
44
+
45
+
46
+ class Object
47
+ include Irber::Src
48
+ end
49
+
50
+
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require "irber/api"
4
+
5
+ module Irber
6
+ module Api
7
+
8
+ class A
9
+ def an_instance_method; end
10
+ def self.a_class_method; end
11
+
12
+ def a_method_with_args an_arg, another_arg; end
13
+
14
+ private
15
+
16
+ def a_private_method; end
17
+ def self.a_private_class_method; end
18
+
19
+ end
20
+
21
+ describe "#api" do
22
+
23
+ it "should be available on all objects" do
24
+ Object.new.should respond_to(:api)
25
+ end
26
+
27
+ describe "on objects" do
28
+
29
+ it "should include instance methods" do
30
+ A.new.api.to_s.should include("an_instance_method")
31
+ end
32
+
33
+ it "should not include private methods" do
34
+ A.new.api.to_s.should_not include("a_private_class_method()")
35
+ end
36
+
37
+ end
38
+
39
+ describe "on classes" do
40
+
41
+ it "should include class methods" do
42
+ A.api.to_s.should include("a_class_method()")
43
+ end
44
+
45
+ it "should include instance methods" do
46
+ A.api.to_s.should include("an_instance_method()")
47
+ end
48
+
49
+ it "should not include Module methods" do
50
+ A.api.to_s.should_not match(/Module/)
51
+ end
52
+
53
+ it "should not include Class methods" do
54
+ A.api.to_s.should_not match(/Class/)
55
+ end
56
+
57
+ it "should not include Kernel methods" do
58
+ A.api.to_s.should_not match(/Kernel/)
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require "irber/papi"
4
+
5
+ module Irber
6
+ module Papi
7
+
8
+ class B
9
+ def c; end
10
+ def b; end
11
+ def self.a; end
12
+ end
13
+
14
+
15
+ module E
16
+ def e; end;
17
+ end
18
+
19
+ class Ddd < B
20
+ include E
21
+ def d; end
22
+ end
23
+
24
+ describe "#papi" do
25
+
26
+ describe "formating" do
27
+
28
+ it "should order by method name, showing owner, level (instance, class) and signature, and putting methods on the same column" do
29
+ stdout{ papi Ddd }.should == "
30
+ Irber::Papi::B . a()
31
+ Irber::Papi::B # b()
32
+ Irber::Papi::B # c()
33
+ Irber::Papi::Ddd # d()
34
+ Irber::Papi::E # e()
35
+ ".unindented
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require "irber/psrc"
4
+
5
+
6
+ module Irber
7
+ module Psrc
8
+
9
+ class A
10
+ def an_instance_method
11
+ "result"
12
+ end
13
+ def self.a_class_method
14
+ "result"
15
+ end
16
+ end
17
+
18
+
19
+ describe "#psrc" do
20
+
21
+ it "should print a class source" do
22
+ @a = A.new
23
+ stdout{ psrc @a }.should == stdout{ puts @a.src }
24
+ end
25
+
26
+ it "should print a method source" do
27
+ @a = A.new
28
+ stdout{ psrc @a, :an_instance_method }.should == stdout{ puts @a.src :an_instance_method }
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,90 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require "irber/self_inspect"
4
+
5
+
6
+
7
+ module Irber
8
+
9
+ module SelfInspect
10
+
11
+ class A
12
+ def a arg
13
+ "a"
14
+ end
15
+ def to_s
16
+ "O#to_s"
17
+ end
18
+ def inspect
19
+ "O#inspect"
20
+ end
21
+ end
22
+
23
+
24
+ describe Irber::SelfInspect do
25
+
26
+ before(:each) do
27
+ @a = A.new
28
+ end
29
+
30
+ describe "#puts_it" do
31
+ it "should be available on all objects" do
32
+ Object.new.should respond_to(:puts_it)
33
+ end
34
+
35
+ it "should puts itself" do
36
+ stdout{ @a.puts_it }.should == stdout{ puts @a }
37
+ end
38
+ end
39
+
40
+ describe "#p_it" do
41
+ it "should be available on all objects" do
42
+ Object.new.should respond_to(:p_it)
43
+ end
44
+
45
+ it "should puts itself" do
46
+ stdout{ @a.p_it }.should == stdout{ p @a }
47
+ end
48
+ end
49
+
50
+ describe "#pp_it" do
51
+ it "should be available on all objects" do
52
+ Object.new.should respond_to(:pp_it)
53
+ end
54
+
55
+ it "should puts itself" do
56
+ stdout{ @a.pp_it }.should == stdout{ pp @a }
57
+ end
58
+ end
59
+
60
+
61
+ describe "#psrc_it" do
62
+ it "should be available on all objects" do
63
+ Object.new.should respond_to(:psrc_it)
64
+ end
65
+
66
+ it "should psrc itself" do
67
+ stdout{ @a.psrc_it }.should == stdout{ psrc @a }
68
+ end
69
+
70
+ it "should psrc itself with method arg" do
71
+ stdout{ @a.psrc_it :a }.should == stdout{ psrc @a, :a }
72
+ end
73
+
74
+ end
75
+
76
+ describe "#papi_it" do
77
+ it "should be available on all objects" do
78
+ Object.new.should respond_to(:papi_it)
79
+ end
80
+
81
+ it "should papi itself" do
82
+ stdout{ @a.papi_it }.should == stdout{ papi @a }
83
+ end
84
+ end
85
+
86
+
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require 'irber/signature'
4
+
5
+
6
+ module Irber
7
+ module Signature
8
+
9
+ class A
10
+ def a(param); end
11
+ def self.b; end
12
+ end
13
+
14
+
15
+ describe Irber::Signature do
16
+
17
+ it "should be available on all unbound methods" do
18
+ A.instance_method(:a).should respond_to(:signature)
19
+ end
20
+
21
+
22
+ it "should be available on all methods" do
23
+ A.new.method(:a).should respond_to(:signature)
24
+ end
25
+
26
+
27
+ describe "returned data" do
28
+
29
+ before(:each) do
30
+ @method = A.instance_method(:a)
31
+ end
32
+
33
+ it "should include the owner" do
34
+ @method.signature[0].should == "Irber::Signature::A"
35
+ end
36
+
37
+ it "should include the level (class/instance)" do
38
+ @method.signature[1].should == "#"
39
+ end
40
+
41
+ it "should include the method name" do
42
+ @method.signature[2].should == "a"
43
+ end
44
+
45
+ it "should include the params" do
46
+ @method.signature[3].should == "(param)"
47
+ end
48
+ end
49
+
50
+
51
+ describe "method signatures creation" do
52
+
53
+ it "should include arg names when possible" do
54
+ A.instance_method(:a).signature[3].should == "(param)"
55
+ end
56
+
57
+ it "should create a signature when not possible" do
58
+ String.instance_method(:between?).signature[3].should == "(arg1, arg2)"
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end