ripl 0.2.2 → 0.2.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/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.2.3
2
+ * Add Shell docs
3
+ * Sync shell binding with completion binding
4
+ * Removed Shell#options
5
+
1
6
  == 0.2.2
2
7
  * Add man page
3
8
 
data/README.rdoc CHANGED
@@ -61,7 +61,7 @@ modifying ~/.irbrc, wrap your irb-specific configuration in a block as follow:
61
61
  * 6 common commandline options: -f, -r, -I, -d, -h, -v
62
62
  * IRB.conf -> Ripl.config
63
63
  * Enhancements over irb
64
- * ~200 lines vs irb's 5000+ lines
64
+ * ~230 lines vs irb's 5000+ lines
65
65
  * Easily extendable with plugins
66
66
  * Tests and documentation!
67
67
  * Customizable completion and completion of method arguments (from bond)
@@ -83,7 +83,9 @@ loading them as early as possible and allowing them to extend most of ripl's fun
83
83
 
84
84
  As an example plugin, let's color error messages red:
85
85
 
86
- # Place in ~/.riplrc
86
+ require 'ripl'
87
+
88
+ # To try place in ~/.riplrc
87
89
  module Ripl
88
90
  module RedError
89
91
  def format_error(error)
data/deps.rip CHANGED
@@ -1 +1 @@
1
- bond ~>0.3.1
1
+ bond ~>0.3.3
@@ -3,7 +3,8 @@ require 'bond'
3
3
  module Ripl::Completion
4
4
  def before_loop
5
5
  super
6
- Bond.restart(config[:completion] || {})
6
+ options = {:eval_binding=>lambda { Ripl.shell.binding }}
7
+ Bond.restart((config[:completion] || {}).merge(options))
7
8
  end
8
9
  end
9
10
  Ripl::Shell.send :include, Ripl::Completion
data/lib/ripl/shell.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Ripl::Shell
2
- OPTIONS = {:name=>'ripl', :line=>1, :result_prompt=>'=> ', :prompt=>'>> ',
2
+ OPTIONS = {:name=>'ripl', :result_prompt=>'=> ', :prompt=>'>> ',
3
3
  :binding=>TOPLEVEL_BINDING, :irbrc=>'~/.irbrc'}
4
4
 
5
5
  def self.create(options={})
@@ -10,13 +10,15 @@ class Ripl::Shell
10
10
  new(options)
11
11
  end
12
12
 
13
- attr_accessor :line, :binding, :result_prompt, :result, :options
13
+ attr_accessor :line, :binding, :result_prompt, :result, :name
14
14
  def initialize(options={})
15
- @options = OPTIONS.merge options
16
- @name, @binding, @line = @options.values_at(:name, :binding, :line)
17
- @irbrc = @options[:irbrc]
15
+ options = OPTIONS.merge options
16
+ @name, @binding = options.values_at(:name, :binding)
17
+ @prompt, @result_prompt = options.values_at(:prompt, :result_prompt)
18
+ @irbrc, @line = options[:irbrc], 1
18
19
  end
19
20
 
21
+ # Loops shell until user exits
20
22
  def loop
21
23
  before_loop
22
24
  catch(:ripl_exit) { while(true) do; loop_once; end }
@@ -25,56 +27,72 @@ class Ripl::Shell
25
27
 
26
28
  def config; Ripl.config; end
27
29
 
30
+ # Runs through one loop iteration: gets input, evals and prints result
31
+ def loop_once
32
+ @error_raised = nil
33
+ @input = get_input
34
+ throw(:ripl_exit) if !@input || @input == 'exit'
35
+ eval_input(@input)
36
+ print_result(@result)
37
+ end
38
+
39
+ # Sets @result to result of evaling input and print unexpected errors
40
+ def eval_input(input)
41
+ @result = loop_eval(input)
42
+ eval("_ = Ripl.shell.result", @binding)
43
+ rescue Exception => e
44
+ @error_raised = true
45
+ print_eval_error(e)
46
+ ensure
47
+ @line += 1
48
+ end
49
+
28
50
  module API
51
+ # Sets up shell before looping by loading ~/.irbrc. Can be extended to
52
+ # initialize plugins and their instance variables.
29
53
  def before_loop
30
54
  Ripl::Runner.load_rc(@irbrc) if @irbrc
31
55
  end
32
56
 
33
- def loop_once
34
- @error_raised = nil
35
- @input = get_input
36
- throw(:ripl_exit) if !@input || @input == 'exit'
37
- eval_input(@input)
38
- print_result(@result)
39
- end
40
-
57
+ # @return [String, nil] Prints #prompt and returns input given by user
41
58
  def get_input
42
59
  print prompt
43
60
  $stdin.gets.chomp
44
61
  end
45
62
 
63
+ # @return [String]
46
64
  def prompt
47
- @options[:prompt].respond_to?(:call) ? @options[:prompt].call : @options[:prompt]
48
- end
49
-
50
- def eval_input(input)
51
- @result = loop_eval(input)
52
- eval("_ = Ripl.shell.result", @binding)
53
- rescue Exception => e
54
- @error_raised = true
55
- print_eval_error(e)
56
- ensure
57
- @line += 1
65
+ @prompt.respond_to?(:call) ? @prompt.call : @prompt
58
66
  end
59
67
 
68
+ # Evals user input using @binding, @name and @line
60
69
  def loop_eval(str)
61
70
  eval(str, @binding, "(#{@name})", @line)
62
71
  end
63
72
 
73
+ # Prints error formatted by #format_error to STDERR. Could be extended to
74
+ # handle certain exceptions.
75
+ # @param [Exception]
64
76
  def print_eval_error(err)
65
77
  warn format_error(err)
66
78
  end
67
79
 
80
+ # Prints result using #format_result
68
81
  def print_result(result)
69
82
  puts(format_result(result)) unless @error_raised
70
83
  end
71
84
 
85
+ # Formats errors raised by eval of user input
86
+ # @param [Exception]
87
+ # @return [String]
72
88
  def format_error(err); Ripl::Runner.format_error(err); end
73
89
 
90
+ # @return [String] Formats result using result_prompt
74
91
  def format_result(result)
75
- @options[:result_prompt] + result.inspect
92
+ @result_prompt + result.inspect
76
93
  end
77
94
 
95
+ # Called after shell finishes looping.
78
96
  def after_loop; end
79
97
  end
80
98
  include API
data/lib/ripl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ripl
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
data/man/ripl.1 CHANGED
@@ -112,7 +112,9 @@ For an example shell plugin, let\'s color error messages red:
112
112
  .
113
113
  .nf
114
114
 
115
- # Place in ~/\.riplrc
115
+ require \'ripl\'
116
+
117
+ # To try place in ~/\.riplrc
116
118
  module Ripl
117
119
  module RedError
118
120
  def format_error(error)
@@ -136,7 +138,7 @@ Points to consider when creating plugins:
136
138
  When adding functionality to a method, make sure to call \fBsuper\fR to preserve existing functionality\.
137
139
  .
138
140
  .IP "\(bu" 4
139
- When replacing functionality for a method, make sure the method\'s expectations are met i\.e\. setting a specific instance variable\. Failure to do so, will \fBbreak\fR ripl for you and anyone else who uses your plugin!
141
+ When replacing functionality for a method, make sure the method\'s expectations are met i\.e\. setting a specific instance variable or calling certain methods\. Failure to do so can \fBbreak\fR ripl for you and anyone else who uses your plugin!
140
142
  .
141
143
  .IP "\(bu" 4
142
144
  Plugins can setup and teardown anything around a shell by extending Shell#before_loop and Shell#after_loop:
@@ -150,14 +152,12 @@ module Ripl::MyPlugin
150
152
 
151
153
  def before_loop
152
154
  super
153
- Ripl\.config[:my_plugin] ||= :web_scale
155
+ # Open file, open connection \.\.\.
154
156
  end
155
157
 
156
158
  def after_loop
157
159
  super
158
- # Write to files
159
- # close a connection
160
- # \.\.\.
160
+ # Write to file, close a connection \.\.\.
161
161
  end
162
162
  .
163
163
  .fi
@@ -168,7 +168,7 @@ end
168
168
  end
169
169
  .
170
170
  .IP "\(bu" 4
171
- To add configuration for a plugin, add a key to Ripl\.config that matches the underscored version of the plugin name i\.e\. Ripl::RedError \-> Ripl\.config[:red_error]\. To set a default config value, see the previous example\.
171
+ To add configuration for a plugin, add a key to Ripl\.config that matches the underscored version of the plugin name i\.e\. Ripl::RedError \-> Ripl\.config[:red_error]\. To set a default config value, just set it after including the plugin into Ripl::Shell\.
172
172
  .
173
173
  .IP "\(bu" 4
174
174
  For more examples of plugins, see gems I\'ve made that start with \'ripl\-\'\.
data/man/ripl.1.ronn CHANGED
@@ -84,7 +84,9 @@ be required in ~/.irbrc.
84
84
 
85
85
  For an example shell plugin, let's color error messages red:
86
86
 
87
- # Place in ~/.riplrc
87
+ require 'ripl'
88
+
89
+ # To try place in ~/.riplrc
88
90
  module Ripl
89
91
  module RedError
90
92
  def format_error(error)
@@ -102,28 +104,26 @@ Points to consider when creating plugins:
102
104
  * When adding functionality to a method, make sure to call `super` to preserve existing functionality.
103
105
 
104
106
  * When replacing functionality for a method, make sure the method's expectations are met i.e.
105
- setting a specific instance variable. Failure to do so, will `break` ripl for you and anyone else
106
- who uses your plugin!
107
+ setting a specific instance variable or calling certain methods. Failure to do so can `break` ripl
108
+ for you and anyone else who uses your plugin!
107
109
 
108
110
  * Plugins can setup and teardown anything around a shell by extending Shell#before_loop and Shell#after_loop:
109
111
 
110
112
  module Ripl::MyPlugin
111
113
  def before_loop
112
114
  super
113
- Ripl.config[:my_plugin] ||= :web_scale
115
+ # Open file, open connection ...
114
116
  end
115
117
 
116
118
  def after_loop
117
119
  super
118
- # Write to files
119
- # close a connection
120
- # ...
120
+ # Write to file, close a connection ...
121
121
  end
122
122
  end
123
123
 
124
124
  * To add configuration for a plugin, add a key to Ripl.config that matches the underscored version
125
- of the plugin name i.e. Ripl::RedError -> Ripl.config[:red_error]. To set a default config value, see
126
- the previous example.
125
+ of the plugin name i.e. Ripl::RedError -> Ripl.config[:red_error]. To set a default config value, just set it
126
+ after including the plugin into Ripl::Shell.
127
127
 
128
128
  * For more examples of plugins, see gems I've made that start with 'ripl-'.
129
129
 
data/test/runner_test.rb CHANGED
@@ -10,18 +10,18 @@ describe "Runner" do
10
10
  Ripl.start
11
11
  end
12
12
 
13
- it "sets a shell's options" do
13
+ it "sets a shell's variables" do
14
14
  mock_riplrc
15
15
  mock(Shell).create(anything) {|e| shell = Shell.new(e); mock(shell).loop; shell }
16
- Ripl.start(:prompt=>'$')
17
- Ripl.shell.options[:prompt].should == '$'
16
+ Ripl.start(:name=>'shh')
17
+ Ripl.shell.name.should == 'shh'
18
18
  end
19
19
 
20
- it "overrides options set in riplrc" do
21
- mock_riplrc { Ripl.config[:readline] = false }
20
+ it "overrides config set in riplrc" do
21
+ mock_riplrc { Ripl.config[:name] = 'blah' }
22
22
  mock(Shell).create(anything) {|e| shell = Shell.new(e); mock(shell).loop; shell }
23
- Ripl.start(:readline=>true)
24
- Ripl.shell.options[:readline].should == true
23
+ Ripl.start(:name=>'dude')
24
+ Ripl.shell.name.should == 'dude'
25
25
  end
26
26
  end
27
27
 
@@ -29,11 +29,11 @@ describe "Runner" do
29
29
  describe "riplrc" do
30
30
  before { reset_ripl }
31
31
 
32
- it "sets a shell's options" do
32
+ it "sets config" do
33
33
  mock_riplrc { Ripl.config[:blah] = true }
34
34
  mock(Shell).create(anything) {|e| shell = Shell.new(e); mock(shell).loop; shell }
35
35
  Runner.run([])
36
- Ripl.shell.options[:blah].should == true
36
+ Ripl.config[:blah].should == true
37
37
  end
38
38
 
39
39
  it "catches and prints error" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ripl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gabriel Horner
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-14 00:00:00 -05:00
18
+ date: 2010-11-15 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency