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/History.txt CHANGED
@@ -1,3 +1,16 @@
1
+ === 0.0.4 2009-07-19
2
+
3
+ * 3 major enhancement:
4
+ * print an object's api - adds a papi method to print a class public methods (filtering low level methods from Object, Kernel, Module and Class)
5
+ * make any object print its api - adds a method papi_it to make an object print its class public methods (filtering low level methods from Object, Kernel, Module and Class)
6
+ * make any object print its src - adds a method psrc_it to make an object print its (generated) source code
7
+
8
+ * 2 major api changes:
9
+ * renamed #to_code to #src to make it consistent with its printing equivalent #psrc
10
+ * renamed #src to #psrc to maintain consistency with other printing methods
11
+
12
+
13
+
1
14
  === 0.0.3 2009-07-16
2
15
 
3
16
  * 1 major enhancement:
data/README.markdown ADDED
@@ -0,0 +1,95 @@
1
+ ## DESCRIPTION:
2
+
3
+ Little tools to enhance your irb experience
4
+
5
+ ## INSTALL:
6
+
7
+ * sudo gem install srizzo-irber
8
+ * add to your ~/.irbrc
9
+
10
+ require 'irber'
11
+
12
+
13
+
14
+ ## BASIC USAGE:
15
+
16
+
17
+ E.g. a class:
18
+
19
+ class A
20
+ def a
21
+ "a"
22
+ end
23
+ end
24
+
25
+
26
+ * generate a class or method's source code on the fly
27
+
28
+
29
+ >> psrc A
30
+ class A < Object
31
+ def a()
32
+ "a"
33
+ end
34
+ end
35
+ => nil
36
+
37
+
38
+
39
+ >> psrc A, :a
40
+ def a()
41
+ "a"
42
+ end
43
+ => nil
44
+
45
+
46
+ * print its api
47
+
48
+ >> papi A
49
+ A # a()
50
+
51
+
52
+ * make any object inspect itself
53
+
54
+ >> [1,2].puts_it
55
+ 1
56
+ 2
57
+
58
+
59
+ >> [1,2].pp_it
60
+ [1, 2]
61
+
62
+
63
+ >> A.psrc_it
64
+ class A < Object
65
+ def a()
66
+ "a"
67
+ end
68
+ end
69
+
70
+
71
+ >> A.papi_it
72
+ A # a()
73
+
74
+
75
+
76
+
77
+
78
+ ## FEATURES/PROBLEMS:
79
+
80
+ * generate a class or method source code on the fly (won't work for all classes and methods e.g: native classes)
81
+ * print any object's formatted api (filters Object, Kernel, Module and Class methods)
82
+ * make an object inspect itself
83
+
84
+
85
+ ## REQUIREMENTS:
86
+
87
+ * Ruby 1.8.x only
88
+ * ParseTree
89
+ * Ruby2Ruby
90
+
91
+
92
+
93
+ ## Copyright
94
+
95
+ Copyright (c) 2009 Samuel Rizzo. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -27,11 +27,11 @@ Feature: get a generated source code on the fly
27
27
  end
28
28
  end
29
29
  """
30
- And I have required "irber/to_code"
30
+ And I have required "irber/src"
31
31
 
32
32
 
33
33
  Scenario: get source for a class
34
- When I run "A.to_code"
34
+ When I run "A.src"
35
35
  Then I should get the code
36
36
  """
37
37
  class A < Object
@@ -45,7 +45,7 @@ Feature: get a generated source code on the fly
45
45
  """
46
46
 
47
47
  Scenario: get source for an object
48
- When I run "A.new.to_code"
48
+ When I run "A.new.src"
49
49
  Then I should get the code
50
50
  """
51
51
  class A < Object
@@ -59,7 +59,7 @@ Feature: get a generated source code on the fly
59
59
  """
60
60
 
61
61
  Scenario: get source for a module
62
- When I run "M.to_code"
62
+ When I run "M.src"
63
63
  Then I should get the code
64
64
  """
65
65
  module M
@@ -74,7 +74,7 @@ Feature: get a generated source code on the fly
74
74
 
75
75
 
76
76
  Scenario: get source for a class method
77
- When I run "A.to_code :a_class_method"
77
+ When I run "A.src :a_class_method"
78
78
  Then I should get the code
79
79
  """
80
80
  def a_class_method()
@@ -83,7 +83,7 @@ Feature: get a generated source code on the fly
83
83
  """
84
84
 
85
85
  Scenario: get source for an object method
86
- When I run "A.new.to_code :an_instance_method"
86
+ When I run "A.new.src :an_instance_method"
87
87
  Then I should get the code
88
88
  """
89
89
  def an_instance_method()
@@ -92,10 +92,10 @@ Feature: get a generated source code on the fly
92
92
  """
93
93
 
94
94
  Scenario: try to get an inexisting method
95
- When I run "A.new.to_code :an_inexisting_method"
95
+ When I run "A.new.src :an_inexisting_method"
96
96
  Then I should get the error "NameError"
97
97
 
98
98
  Scenario: try on native code
99
99
  Given a native implemented class "String"
100
- When I run "String.to_code"
100
+ When I run "String.src"
101
101
  Then I should get the error "ParseError"
@@ -1,4 +1,4 @@
1
- Feature: make an object print itself
1
+ Feature: make any object inspect itself
2
2
  In order to save time going to the beginning of the line
3
3
  As a lazy programmer
4
4
  I want to print an object appending a method to it
@@ -7,6 +7,9 @@ Feature: make an object print itself
7
7
  Given the definition
8
8
  """
9
9
  class O
10
+ def a arg
11
+ "a"
12
+ end
10
13
  def to_s
11
14
  "O#to_s"
12
15
  end
@@ -15,7 +18,7 @@ Feature: make an object print itself
15
18
  end
16
19
  end
17
20
  """
18
- And I have required "irber/printing"
21
+ And I have required "irber/self_inspect"
19
22
  And I have run "@o = O.new"
20
23
 
21
24
  When I run "@o.puts_it"
@@ -26,4 +29,9 @@ Feature: make an object print itself
26
29
 
27
30
  When I run "@o.pp_it"
28
31
  Then I should get the same stdout as "pp @o"
29
-
32
+
33
+ When I run "@o.psrc_it"
34
+ Then I should get the same stdout as "psrc @o"
35
+
36
+ When I run "@o.papi_it"
37
+ Then I should get the same stdout as "papi @o"
@@ -0,0 +1,67 @@
1
+ Feature print an objects api
2
+ In order to save time looking for documentation
3
+ As a lazy programmer
4
+ I want a method that prints the objects api
5
+
6
+ Background:
7
+ Given the definition
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
+ And the definition
19
+ """
20
+ class C
21
+ def method_with_args some_arg
22
+ "result"
23
+ end
24
+ def method_with_var_args req_arg, opt_arg1=nil, opt_arg2=nil
25
+ "result"
26
+ end
27
+ end
28
+ """
29
+ And I have required "irber/papi"
30
+
31
+
32
+ Scenario: print api for a class
33
+ When I run "papi A"
34
+ Then I should have printed its class method
35
+ """
36
+ a_class_method()
37
+ """
38
+ And I should have printed its instance method
39
+ """
40
+ an_instance_method()
41
+ """
42
+
43
+ Scenario: print api for an object
44
+ When I run "papi A.new"
45
+ Then I should have printed its instance method
46
+ """
47
+ an_instance_method()
48
+ """
49
+
50
+
51
+ Scenario: print api with method args
52
+ When I run "papi C"
53
+ Then I should have printed its instance method
54
+ """
55
+ method_with_args(some_arg)
56
+ """
57
+ And I should have printed its instance method
58
+ """
59
+ method_with_var_args(req_arg, opt_arg1 = nil, opt_arg2 = nil)
60
+ """
61
+
62
+ Scenario: print api for a native class with method args
63
+ When I run "papi String"
64
+ And I should have printed its instance method
65
+ """
66
+ between?(arg1, arg2)
67
+ """
@@ -17,12 +17,12 @@ Feature: print generated source code
17
17
  end
18
18
  end
19
19
  """
20
- And I have required "irber/src"
20
+ And I have required "irber/psrc"
21
21
 
22
22
 
23
23
 
24
24
  Scenario: print source for a class
25
- When I run "src A"
25
+ When I run "psrc A"
26
26
  Then I should have the code printed
27
27
  """
28
28
  class A < Object
@@ -37,7 +37,7 @@ Feature: print generated source code
37
37
  """
38
38
 
39
39
  Scenario: print source for a class
40
- When I run "src A, :an_instance_method"
40
+ When I run "psrc A, :an_instance_method"
41
41
  Then I should have the code printed
42
42
  """
43
43
  def an_instance_method()
@@ -40,3 +40,12 @@ end
40
40
  Then /^I should have the code printed$/ do |code|
41
41
  stdout{ @result.call }.should match_code code
42
42
  end
43
+
44
+
45
+ Then /^I should have printed its class method$/ do |string|
46
+ stdout{ @result.call }.should include(string.unindented)
47
+ end
48
+
49
+ Then /^I should have printed its instance method$/ do |string|
50
+ stdout{ @result.call }.should include(string.unindented)
51
+ end
data/irber.gemspec CHANGED
@@ -2,39 +2,46 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{irber}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["srizzo"]
9
- s.date = %q{2009-07-16}
9
+ s.date = %q{2009-07-19}
10
10
  s.email = %q{rizzolabs@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
13
- "README.rdoc"
13
+ "README.markdown"
14
14
  ]
15
15
  s.files = [
16
16
  ".document",
17
17
  ".gitignore",
18
18
  "History.txt",
19
19
  "LICENSE",
20
- "README.rdoc",
20
+ "README.markdown",
21
21
  "Rakefile",
22
22
  "VERSION",
23
23
  "features/get_a_generated_source_code_on_the_fly.feature",
24
- "features/make_an_object_print_itself.feature",
24
+ "features/make_any_object_inspect_itself.feature",
25
+ "features/print_an_objects_api.feature",
25
26
  "features/print_generated_source_code.feature",
26
27
  "features/step_definitions/irber_steps.rb",
27
28
  "features/support/env.rb",
28
29
  "irber.gemspec",
29
30
  "lib/irber.rb",
30
- "lib/irber/printing.rb",
31
+ "lib/irber/api.rb",
32
+ "lib/irber/papi.rb",
33
+ "lib/irber/psrc.rb",
34
+ "lib/irber/self_inspect.rb",
35
+ "lib/irber/signature.rb",
31
36
  "lib/irber/src.rb",
32
- "lib/irber/to_code.rb",
33
37
  "spec/custom_helpers.rb",
34
38
  "spec/custom_matchers.rb",
35
- "spec/irber/printing_spec.rb",
39
+ "spec/irber/api_spec.rb",
40
+ "spec/irber/papi_spec.rb",
41
+ "spec/irber/psrc_spec.rb",
42
+ "spec/irber/self_inspect_spec.rb",
43
+ "spec/irber/signature_spec.rb",
36
44
  "spec/irber/src_spec.rb",
37
- "spec/irber/to_code_spec.rb",
38
45
  "spec/spec_helper.rb"
39
46
  ]
40
47
  s.has_rdoc = true
@@ -46,9 +53,12 @@ Gem::Specification.new do |s|
46
53
  s.test_files = [
47
54
  "spec/custom_helpers.rb",
48
55
  "spec/custom_matchers.rb",
49
- "spec/irber/printing_spec.rb",
56
+ "spec/irber/api_spec.rb",
57
+ "spec/irber/papi_spec.rb",
58
+ "spec/irber/psrc_spec.rb",
59
+ "spec/irber/self_inspect_spec.rb",
60
+ "spec/irber/signature_spec.rb",
50
61
  "spec/irber/src_spec.rb",
51
- "spec/irber/to_code_spec.rb",
52
62
  "spec/spec_helper.rb"
53
63
  ]
54
64
 
data/lib/irber.rb CHANGED
@@ -1,3 +1,4 @@
1
- require 'irber/to_code'
2
1
  require 'irber/src'
3
- require 'irber/printing'
2
+ require 'irber/psrc'
3
+ require 'irber/self_inspect'
4
+ require 'irber/papi'
data/lib/irber/api.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'irber/src'
2
+ require 'irber/signature'
3
+
4
+
5
+ module Irber
6
+ module Api
7
+ module InstanceMethods
8
+ def api
9
+
10
+ all_methods = []
11
+
12
+ methods = self.methods
13
+
14
+ methods -= Object.methods
15
+ methods -= Module.methods
16
+ methods -= Class.methods
17
+ methods -= Kernel.methods
18
+
19
+ all_methods += methods.collect { |method_name| self.method(method_name) }
20
+
21
+ if self.respond_to? :instance_methods
22
+
23
+ instance_methods = self.instance_methods
24
+
25
+ instance_methods -= Object.instance_methods
26
+ instance_methods -= Module.instance_methods
27
+ instance_methods -= Class.instance_methods
28
+ instance_methods -= Kernel.instance_methods
29
+
30
+ all_methods += instance_methods.collect { |method_name| self.instance_method(method_name) }
31
+
32
+ end
33
+
34
+ all_methods.sort! { |x, y| x.name <=> y.name }
35
+
36
+ signatures = all_methods.collect { |method| method.signature }
37
+
38
+ signatures
39
+ end
40
+
41
+ end
42
+
43
+ def self.included(receiver)
44
+ receiver.send :include, InstanceMethods
45
+ end
46
+
47
+ end
48
+ end
49
+
50
+
51
+
52
+ class Object
53
+ include Irber::Api
54
+ end
55
+
56
+
57
+