@@ -51,13 +51,15 @@ def handler
51
51
def source
52
52
handler . read ( filepath )
53
53
end
54
+
55
+ def writable?
56
+ File . writable? ( filepath )
57
+ end
54
58
end
55
59
56
60
# An item of work that corresponds to a script content passed via the
57
61
# command line.
58
62
class ScriptItem
59
- FILEPATH = :script
60
-
61
63
attr_reader :source
62
64
63
65
def initialize ( source )
@@ -69,7 +71,30 @@ def handler
69
71
end
70
72
71
73
def filepath
72
- FILEPATH
74
+ :script
75
+ end
76
+
77
+ def writable?
78
+ false
79
+ end
80
+ end
81
+
82
+ # An item of work that correspond to the content passed in via stdin.
83
+ class STDINItem
84
+ def handler
85
+ HANDLERS [ ".rb" ]
86
+ end
87
+
88
+ def filepath
89
+ :stdin
90
+ end
91
+
92
+ def source
93
+ $stdin. read
94
+ end
95
+
96
+ def writable?
97
+ false
73
98
end
74
99
end
75
100
@@ -196,7 +221,7 @@ def run(item)
196
221
197
222
source = item . source
198
223
formatted = item . handler . format ( source , options . print_width )
199
- File . write ( filepath , formatted ) if item . filepath != :script
224
+ File . write ( filepath , formatted ) if item . writable?
200
225
201
226
color = source == formatted ? Color . gray ( filepath ) : filepath
202
227
delta = ( ( Time . now - start ) * 1000 ) . round
@@ -386,7 +411,7 @@ def run(argv)
386
411
return 1
387
412
end
388
413
389
- # If we're not reading from stdin and the user didn't supply and
414
+ # If we're not reading from stdin and the user didn't supply any
390
415
# filepaths to be read, then we exit with the usage message.
391
416
if $stdin. tty? && arguments . empty? && options . scripts . empty?
392
417
warn ( HELP )
@@ -403,12 +428,13 @@ def run(argv)
403
428
Dir
404
429
. glob ( pattern )
405
430
. each do |filepath |
406
- queue << FileItem . new ( filepath ) if File . file ?( filepath )
431
+ queue << FileItem . new ( filepath ) if File . readable ?( filepath )
407
432
end
408
433
end
434
+
409
435
options . scripts . each { |script | queue << ScriptItem . new ( script ) }
410
436
else
411
- queue << ScriptItem . new ( $stdin . read )
437
+ queue << STDINItem . new
412
438
end
413
439
414
440
# At the end, we're going to return whether or not this worker ever
0 commit comments