srizzo-irber 0.0.2 → 0.0.3

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,8 @@
1
+ === 0.0.3 2009-07-16
2
+
3
+ * 1 major enhancement:
4
+ * print source code - adds a src method to generate and print a class or method source code
5
+
1
6
  === 0.0.2 2009-07-16
2
7
 
3
8
  * 1 major enhancement:
data/README.rdoc CHANGED
@@ -26,14 +26,16 @@ Given a class:
26
26
 
27
27
  Generate source code on the fly:
28
28
 
29
- >> puts A.to_code
29
+ >> src A
30
30
  class A < Object
31
31
  def a()
32
32
  "a"
33
33
  end
34
34
  end
35
35
  => nil
36
- >> puts A.to_code :a
36
+
37
+
38
+ >> src A, :a
37
39
  def a()
38
40
  "a"
39
41
  end
@@ -42,20 +44,21 @@ Generate source code on the fly:
42
44
 
43
45
  * make an object print itself
44
46
 
45
- >> [1,2].puts_it
46
- 1
47
- 2
48
-
49
- >> [1,2].pp_it
50
- [1, 2]
47
+ >> [1,2].puts_it
48
+ 1
49
+ 2
50
+
51
+
52
+ >> [1,2].pp_it
53
+ [1, 2]
51
54
 
52
55
 
53
56
 
54
57
 
55
58
  == FEATURES/PROBLEMS:
56
59
 
57
- * generate a class or method source code on the fly
58
- * limitation: won't work for all classes and methods (e.g: native classes)
60
+ * generate a class or method source code on the fly (won't work for all classes and methods e.g: native classes)
61
+ * make an object print itself
59
62
 
60
63
 
61
64
  == REQUIREMENTS:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -3,7 +3,8 @@ Feature: get a generated source code on the fly
3
3
  As a lazy programmer
4
4
  I want to get a generated source code on the fly
5
5
 
6
- Scenario: from a class
6
+
7
+ Background:
7
8
  Given the definition
8
9
  """
9
10
  class A
@@ -15,33 +16,35 @@ Feature: get a generated source code on the fly
15
16
  end
16
17
  end
17
18
  """
18
- And I have required "irber/to_code"
19
- When I run "A.to_code"
20
- Then I should get the code
19
+ And the definition
21
20
  """
22
- class A < Object
23
- def an_instance_method()
24
- \"result\"
21
+ module M
22
+ def a_method
23
+ "result"
25
24
  end
26
- def self.a_class_method()
27
- \"result\"
25
+ def self.a_module_method
26
+ "result"
28
27
  end
29
28
  end
30
29
  """
31
-
32
- Scenario: from an object
33
- Given the definition
30
+ And I have required "irber/to_code"
31
+
32
+
33
+ Scenario: get source for a class
34
+ When I run "A.to_code"
35
+ Then I should get the code
34
36
  """
35
- class A
36
- def an_instance_method
37
+ class A < Object
38
+ def an_instance_method()
37
39
  "result"
38
40
  end
39
- def self.a_class_method
41
+ def self.a_class_method()
40
42
  "result"
41
43
  end
42
44
  end
43
45
  """
44
- And I have required "irber/to_code"
46
+
47
+ Scenario: get source for an object
45
48
  When I run "A.new.to_code"
46
49
  Then I should get the code
47
50
  """
@@ -55,19 +58,7 @@ Feature: get a generated source code on the fly
55
58
  end
56
59
  """
57
60
 
58
- Scenario: from a module
59
- Given the definition
60
- """
61
- module M
62
- def a_method
63
- "result"
64
- end
65
- def self.a_module_method
66
- "result"
67
- end
68
- end
69
- """
70
- And I have required "irber/to_code"
61
+ Scenario: get source for a module
71
62
  When I run "M.to_code"
72
63
  Then I should get the code
73
64
  """
@@ -82,19 +73,7 @@ Feature: get a generated source code on the fly
82
73
  """
83
74
 
84
75
 
85
- Scenario: from a class method
86
- Given the definition
87
- """
88
- class A
89
- def an_instance_method
90
- "result"
91
- end
92
- def self.a_class_method
93
- "result"
94
- end
95
- end
96
- """
97
- And I have required "irber/to_code"
76
+ Scenario: get source for a class method
98
77
  When I run "A.to_code :a_class_method"
99
78
  Then I should get the code
100
79
  """
@@ -103,19 +82,7 @@ Feature: get a generated source code on the fly
103
82
  end
104
83
  """
105
84
 
106
- Scenario: from an object method
107
- Given the definition
108
- """
109
- class A
110
- def an_instance_method
111
- "result"
112
- end
113
- def self.a_class_method
114
- "result"
115
- end
116
- end
117
- """
118
- And I have required "irber/to_code"
85
+ Scenario: get source for an object method
119
86
  When I run "A.new.to_code :an_instance_method"
120
87
  Then I should get the code
121
88
  """
@@ -125,27 +92,10 @@ Feature: get a generated source code on the fly
125
92
  """
126
93
 
127
94
  Scenario: try to get an inexisting method
128
- Given the definition
129
- """
130
- class A
131
- def an_instance_method
132
- "result"
133
- end
134
- def self.a_class_method
135
- "result"
136
- end
137
- end
138
- """
139
- And I have required "irber/to_code"
140
95
  When I run "A.new.to_code :an_inexisting_method"
141
96
  Then I should get the error "NameError"
142
97
 
143
98
  Scenario: try on native code
144
99
  Given a native implemented class "String"
145
- And I have required "irber/to_code"
146
100
  When I run "String.to_code"
147
101
  Then I should get the error "ParseError"
148
-
149
-
150
-
151
-
@@ -0,0 +1,52 @@
1
+ Feature: print generated source code
2
+ In order to save time looking for source code
3
+ As a lazy programmer
4
+ I want a method that generates and prints source code
5
+
6
+
7
+
8
+ Background:
9
+ Given the definition
10
+ """
11
+ class A
12
+ def an_instance_method
13
+ "result"
14
+ end
15
+ def self.a_class_method
16
+ "result"
17
+ end
18
+ end
19
+ """
20
+ And I have required "irber/src"
21
+
22
+
23
+
24
+ Scenario: print source for a class
25
+ When I run "src A"
26
+ Then I should have the code printed
27
+ """
28
+ class A < Object
29
+ def an_instance_method()
30
+ "result"
31
+ end
32
+ def self.a_class_method()
33
+ "result"
34
+ end
35
+ end
36
+
37
+ """
38
+
39
+ Scenario: print source for a class
40
+ When I run "src A, :an_instance_method"
41
+ Then I should have the code printed
42
+ """
43
+ def an_instance_method()
44
+ "result"
45
+ end
46
+
47
+ """
48
+
49
+
50
+
51
+
52
+
@@ -35,3 +35,8 @@ end
35
35
  Then /^I should get the same stdout as "([^\"]*)"$/ do |code|
36
36
  stdout{ @result.call }.should == stdout{ eval(code) }
37
37
  end
38
+
39
+
40
+ Then /^I should have the code printed$/ do |code|
41
+ stdout{ @result.call }.should match_code code
42
+ end
@@ -1,5 +1,4 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
- require 'irber'
3
2
 
4
3
  require 'spec/expectations'
5
4
 
data/irber.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{irber}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["srizzo"]
@@ -22,15 +22,18 @@ Gem::Specification.new do |s|
22
22
  "VERSION",
23
23
  "features/get_a_generated_source_code_on_the_fly.feature",
24
24
  "features/make_an_object_print_itself.feature",
25
+ "features/print_generated_source_code.feature",
25
26
  "features/step_definitions/irber_steps.rb",
26
27
  "features/support/env.rb",
27
28
  "irber.gemspec",
28
29
  "lib/irber.rb",
29
30
  "lib/irber/printing.rb",
31
+ "lib/irber/src.rb",
30
32
  "lib/irber/to_code.rb",
31
33
  "spec/custom_helpers.rb",
32
34
  "spec/custom_matchers.rb",
33
35
  "spec/irber/printing_spec.rb",
36
+ "spec/irber/src_spec.rb",
34
37
  "spec/irber/to_code_spec.rb",
35
38
  "spec/spec_helper.rb"
36
39
  ]
@@ -44,6 +47,7 @@ Gem::Specification.new do |s|
44
47
  "spec/custom_helpers.rb",
45
48
  "spec/custom_matchers.rb",
46
49
  "spec/irber/printing_spec.rb",
50
+ "spec/irber/src_spec.rb",
47
51
  "spec/irber/to_code_spec.rb",
48
52
  "spec/spec_helper.rb"
49
53
  ]
data/lib/irber/src.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'irber/to_code'
2
+
3
+
4
+ module Kernel
5
+
6
+ private
7
+
8
+ def src object, method = nil
9
+ puts object.to_code method
10
+ end
11
+
12
+ end
data/lib/irber/to_code.rb CHANGED
@@ -45,4 +45,6 @@ end
45
45
 
46
46
  class Object
47
47
  include Irber::ToCode
48
- end
48
+ end
49
+
50
+
data/lib/irber.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'irber/to_code'
2
- require 'irber/printing'
2
+ require 'irber/src'
3
+ require 'irber/printing'
@@ -11,3 +11,12 @@ def stdout &block
11
11
  captured_io.rewind
12
12
  captured_io.read
13
13
  end
14
+
15
+
16
+ class String
17
+ def unindented
18
+ self.lines.map do |line|
19
+ line[self.lines.to_a.last[/( *)/, 1].length..-1]
20
+ end.join
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ require "irber/src"
4
+
5
+ class A
6
+ def an_instance_method
7
+ "result"
8
+ end
9
+ def self.a_class_method
10
+ "result"
11
+ end
12
+ end
13
+
14
+
15
+ describe "Kernel#src" do
16
+
17
+ it "should print a class source" do
18
+ @a = A.new
19
+ stdout{ src @a }.should == stdout{ puts @a.to_code }
20
+ end
21
+
22
+ it "should print a method source" do
23
+ @a = A.new
24
+ stdout{ src @a, :an_instance_method }.should == stdout{ puts @a.to_code :an_instance_method }
25
+ end
26
+
27
+
28
+ end
@@ -23,119 +23,121 @@ end
23
23
 
24
24
  describe Irber::ToCode do
25
25
 
26
- describe "on classes" do
27
- it "should be available on all classes" do
28
- A.should respond_to(:to_code)
29
- end
30
26
 
31
- it "should generate its source code" do
32
- A.to_code.should match_code "
33
- class A < Object
34
- def an_instance_method()
35
- \"result\"
36
- end
37
- def self.a_class_method()
38
- \"result\"
27
+ describe "#to_code" do
28
+
29
+ describe "on classes" do
30
+ it "should be available on all classes" do
31
+ A.should respond_to(:to_code)
32
+ end
33
+
34
+ it "should generate its source code" do
35
+ A.to_code.should match_code "
36
+ class A < Object
37
+ def an_instance_method()
38
+ \"result\"
39
+ end
40
+ def self.a_class_method()
41
+ \"result\"
42
+ end
39
43
  end
40
- end
41
- "
44
+ "
45
+ end
42
46
  end
43
- end
44
47
 
45
- describe "on objects" do
46
- it "should be available on all objects" do
47
- A.new.should respond_to(:to_code)
48
- end
48
+ describe "on objects" do
49
+ it "should be available on all objects" do
50
+ A.new.should respond_to(:to_code)
51
+ end
49
52
 
50
- it "should generate its source code" do
51
- A.new.to_code.should match_code "
52
- class A < Object
53
- def an_instance_method()
54
- \"result\"
53
+ it "should generate its source code" do
54
+ A.new.to_code.should match_code "
55
+ class A < Object
56
+ def an_instance_method()
57
+ \"result\"
58
+ end
59
+ def self.a_class_method()
60
+ \"result\"
61
+ end
55
62
  end
56
- def self.a_class_method()
57
- \"result\"
63
+ "
64
+ end
65
+
66
+ end
67
+
68
+ describe "on modules" do
69
+ it "should generat its source code" do
70
+ M.to_code.should match_code "
71
+ module M
72
+ def a_method()
73
+ \"result\"
74
+ end
75
+ def self.a_module_method()
76
+ \"result\"
77
+ end
58
78
  end
59
- end
60
- "
79
+ "
80
+ end
61
81
  end
62
-
63
- end
64
82
 
65
- describe "on modules" do
66
- it "should generat its source code" do
67
- M.to_code.should match_code "
68
- module M
69
- def a_method()
83
+ describe "on class methods" do
84
+ it "should generate a class instance method source code " do
85
+ A.to_code(:an_instance_method).should match_code "
86
+ def an_instance_method()
70
87
  \"result\"
71
88
  end
72
- def self.a_module_method()
89
+ "
90
+ end
91
+
92
+ it "should generate a class method source code " do
93
+ A.to_code(:a_class_method).should match_code "
94
+ def a_class_method()
73
95
  \"result\"
74
96
  end
75
- end
76
- "
77
- end
78
- end
79
-
80
- describe "on class methods" do
81
- it "should generate a class instance method source code " do
82
- A.to_code(:an_instance_method).should match_code "
83
- def an_instance_method()
84
- \"result\"
85
- end
86
- "
87
- end
88
-
89
- it "should generate a class method source code " do
90
- A.to_code(:a_class_method).should match_code "
91
- def a_class_method()
92
- \"result\"
93
- end
94
- "
97
+ "
98
+ end
95
99
  end
96
- end
97
100
 
98
101
 
99
- describe "on object methods" do
100
- it "should generate an object method source code" do
101
- A.new.to_code(:an_instance_method).should match_code "
102
- def an_instance_method()
103
- \"result\"
104
- end
105
- "
102
+ describe "on object methods" do
103
+ it "should generate an object method source code" do
104
+ A.new.to_code(:an_instance_method).should match_code "
105
+ def an_instance_method()
106
+ \"result\"
107
+ end
108
+ "
109
+ end
106
110
  end
107
- end
108
111
 
109
- describe "on module methods" do
110
- it "should generate a module method source code" do
111
- M.to_code(:a_method).should match_code "
112
- def a_method()
113
- \"result\"
114
- end
115
- "
112
+ describe "on module methods" do
113
+ it "should generate a module method source code" do
114
+ M.to_code(:a_method).should match_code "
115
+ def a_method()
116
+ \"result\"
117
+ end
118
+ "
119
+ end
116
120
  end
117
- end
118
121
 
119
122
 
120
- describe "on inexisting methods" do
121
- it "should raise NameError" do
122
- lambda{ A.new.to_code(:an_inexisting_method) }.should raise_error(NameError)
123
+ describe "on inexisting methods" do
124
+ it "should raise NameError" do
125
+ lambda{ A.new.to_code(:an_inexisting_method) }.should raise_error(NameError)
126
+ end
123
127
  end
124
- end
125
128
 
126
129
 
127
- describe "on native classes" do
128
- it "should raise ParseError" do
129
- lambda{ String.to_code }.should raise_error(ParseError)
130
+ describe "on native classes" do
131
+ it "should raise ParseError" do
132
+ lambda{ String.to_code }.should raise_error(ParseError)
133
+ end
130
134
  end
131
- end
132
135
 
133
- describe "on native methods" do
134
- it "should raise ParseError" do
135
- lambda{ "".to_code :to_s }.should raise_error(ParseError)
136
+ describe "on native methods" do
137
+ it "should raise ParseError" do
138
+ lambda{ "".to_code :to_s }.should raise_error(ParseError)
139
+ end
136
140
  end
137
141
  end
138
142
 
139
-
140
-
141
143
  end
data/spec/spec_helper.rb CHANGED
@@ -4,7 +4,7 @@ require 'spec'
4
4
 
5
5
  $LOAD_PATH.unshift(File.dirname(__FILE__))
6
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
- require 'irber'
7
+
8
8
 
9
9
  Spec::Runner.configure do |config|
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srizzo-irber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - srizzo
@@ -51,15 +51,18 @@ files:
51
51
  - VERSION
52
52
  - features/get_a_generated_source_code_on_the_fly.feature
53
53
  - features/make_an_object_print_itself.feature
54
+ - features/print_generated_source_code.feature
54
55
  - features/step_definitions/irber_steps.rb
55
56
  - features/support/env.rb
56
57
  - irber.gemspec
57
58
  - lib/irber.rb
58
59
  - lib/irber/printing.rb
60
+ - lib/irber/src.rb
59
61
  - lib/irber/to_code.rb
60
62
  - spec/custom_helpers.rb
61
63
  - spec/custom_matchers.rb
62
64
  - spec/irber/printing_spec.rb
65
+ - spec/irber/src_spec.rb
63
66
  - spec/irber/to_code_spec.rb
64
67
  - spec/spec_helper.rb
65
68
  has_rdoc: true
@@ -92,5 +95,6 @@ test_files:
92
95
  - spec/custom_helpers.rb
93
96
  - spec/custom_matchers.rb
94
97
  - spec/irber/printing_spec.rb
98
+ - spec/irber/src_spec.rb
95
99
  - spec/irber/to_code_spec.rb
96
100
  - spec/spec_helper.rb