Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Used WithPath decorator to clean up destination

  • Loading branch information...
commit 85a27267187a730505409843632efc26a31dc56a 1 parent a9bf1e6
@avdi authored
Showing with 13 additions and 5 deletions.
  1. +13 −5 lib/cowsay.rb
View
18 lib/cowsay.rb
@@ -1,6 +1,18 @@
require 'active_support'
require 'logger'
+require 'delegate'
+
module Cowsay
+ class WithPath < SimpleDelegator
+ def path
+ case __getobj__
+ when File then super
+ when nil then "return value"
+ else inspect
+ end
+ end
+ end
+
class Cow
def initialize(options={})
@io_class = options.fetch(:io_class){IO}
@@ -12,6 +24,7 @@ def say(message, options={})
if options[:strings] && options[:strings][:eyes]
command << " -e '#{options[:strings][:eyes]}'"
end
+ destination = WithPath.new(options[:out]).path
messages = case message
when Array then message
@@ -34,11 +47,6 @@ def say(message, options={})
if options[:out]
options[:out] << output
end
- destination = case options[:out]
- when nil then "return value"
- when File then options[:out].path
- else options[:out].inspect
- end
@logger.info "Wrote to #{destination}"
if $? && ![0,172].include?($?.exitstatus)
raise ArgumentError, "Command exited with status #{$?.exitstatus.to_s}"
Please sign in to comment.
Something went wrong with that request. Please try again.