rubysh 0.1.2 → 0.2.0

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.
@@ -128,46 +128,32 @@ module Rubysh
128
128
  FD.new(2)
129
129
  end
130
130
 
131
- def self.>(target=nil, opts=nil)
132
- # Might want to DRY this logic up at some point. Right now seems
133
- # like it'd just sacrifice clarity though.
134
- if !opts && target.kind_of?(Hash)
135
- opts = target
136
- target = nil
137
- end
131
+ def self.>(target=(t=true; nil), opts=(o=true; nil))
132
+ target, opts = handle_redirect_args(target, t, opts, o)
138
133
  target ||= :stdout
139
134
 
140
135
  Redirect.new(1, '>', target, opts)
141
136
  end
142
137
 
143
- def self.>>(target=nil, opts=nil)
144
- if !opts && target.kind_of?(Hash)
145
- opts = target
146
- target = nil
147
- end
138
+ def self.>>(target=(t=true; nil), opts=(o=true; nil))
139
+ target, opts = handle_redirect_args(target, t, opts, o)
148
140
  target ||= :stdout
149
141
 
150
142
  Redirect.new(1, '>>', target, opts)
151
143
  end
152
144
 
153
- def self.<(target=nil, opts=nil)
154
- if !opts && target.kind_of?(Hash)
155
- opts = target
156
- target = nil
157
- end
145
+ def self.<(target=(t=true; nil), opts=(o=true; nil))
146
+ target, opts = handle_redirect_args(target, t, opts, o)
158
147
  target ||= :stdin
159
148
 
160
149
  Redirect.new(0, '<', target, opts)
161
150
  end
162
151
 
163
152
  # Hack to implement <<<
164
- def self.<<(fd=nil, opts=nil)
165
- if !opts && fd.kind_of?(Hash)
166
- opts = fd
167
- fd = nil
168
- end
169
-
153
+ def self.<<(fd=(f=true; nil), opts=(o=true; nil))
154
+ fd, opts = handle_redirect_args(fd, f, opts, o)
170
155
  fd ||= FD.new(0)
156
+
171
157
  TripleLessThan::Shell.new(fd, opts)
172
158
  end
173
159
 
@@ -189,4 +175,24 @@ module Rubysh
189
175
  log.error(formatted)
190
176
  raise msg if hard
191
177
  end
178
+
179
+ def self.handle_redirect_args(target, target_omitted, opts, opts_omitted)
180
+ if opts_omitted && target.kind_of?(Hash)
181
+ # Shift over if user provided target as a hash but omitted opts.
182
+ opts = target
183
+ opts_omitted = target_omitted
184
+
185
+ target = nil
186
+ target_omitted = true
187
+ end
188
+
189
+ # User provided a false-y value for target. This probably
190
+ # indicates a bug in the user's code, where a variable is
191
+ # accidentally nil.
192
+ if !target_omitted && !target
193
+ raise Rubysh::Error::BaseError.new("You provided #{target.inspect} as your redirect target. This probably indicates a bug in your code. Either omit the target argument or provide a non-false-y value for it.")
194
+ end
195
+
196
+ return target, opts
197
+ end
192
198
  end
@@ -1,3 +1,3 @@
1
1
  module Rubysh
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -69,6 +69,19 @@ module RubyshTest::Unit
69
69
  assert_equal([Rubysh::Redirect.new(Rubysh::FD.new(2), '>', Rubysh::FD.new(1))], subprocess.directives)
70
70
  end
71
71
  end
72
+
73
+ # Rubysh('ls', '/tmp', Rubysh.stderr > Rubysh.stdout)
74
+ describe 'with a redirection' do
75
+ it 'raises if the argument is nil' do
76
+ assert_raises(Rubysh::Error::BaseError) {Rubysh.<(nil)}
77
+ assert_raises(Rubysh::Error::BaseError) {Rubysh.<(nil, :foo => :bar)}
78
+ end
79
+
80
+ it 'allows omitting target' do
81
+ Rubysh.<()
82
+ Rubysh.<(:foo => :bar)
83
+ end
84
+ end
72
85
  end
73
86
  end
74
87
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-25 00:00:00.000000000 Z
12
+ date: 2014-01-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -142,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  segments:
144
144
  - 0
145
- hash: 3657158191567713340
145
+ hash: 4577335666745550264
146
146
  required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  none: false
148
148
  requirements:
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  segments:
153
153
  - 0
154
- hash: 3657158191567713340
154
+ hash: 4577335666745550264
155
155
  requirements: []
156
156
  rubyforge_project:
157
157
  rubygems_version: 1.8.23