Skip to content

Commit

Permalink
organized auto-rendering from cmdline
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Nov 13, 2009
1 parent 3a5b81d commit 01ece63
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
16 changes: 11 additions & 5 deletions lib/boson/runners/bin_runner.rb
Expand Up @@ -20,8 +20,16 @@ module Boson
# [:index] Updates index for given libraries allowing you to use them. This is useful if Boson's autodetection of
# changed libraries isn't picking up your changes. Since this option has a :bool_default attribute, arguments
# passed to this option need to be passed with '=' i.e. '--index=my_lib'.
# [:render] Pretty formats the results of commands without options. Handy for commands that return arrays.
# [:render] Toggles the auto-rendering done for commands that don't have views. Doesn't affect commands that already have views.
# Default is false. Also see Auto Rendering section below.
# [:pager_toggle] Toggles Hirb's pager in case you'd like to pipe to another command.
#
# ==== Auto Rendering
# Commands that don't have views (defined via render_options) have their return value auto-rendered as a view as follows:
# * nil,false and true aren't rendered
# * arrays are rendered with Hirb's tables
# * non-arrays are printed with inspect()
# * Any of these cases can be toggled to render/not render with the global option :render
class BinRunner < Runner
GLOBAL_OPTIONS = {
:verbose=>{:type=>:boolean, :desc=>"Verbose description of loading libraries or help"},
Expand Down Expand Up @@ -107,10 +115,8 @@ def parse_args(args)
end

def render_output(output)
if Scientist.global_options && !Scientist.rendered && !View.silent_object?(output)
puts output.inspect
elsif !Scientist.global_options && @options[:render]
View.render(output, :silence_booleans=>true)
if (!Scientist.rendered && !View.inspected_object?(output)) ^ @options[:render]
View.render(output, :inspect=>!output.is_a?(Array) || (Scientist.global_options || {})[:render])
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/boson/scientist.rb
Expand Up @@ -75,7 +75,8 @@ module Scientist
class Error < StandardError; end
class EscapeGlobalOption < StandardError; end

attr_reader :global_options, :rendered, :option_parsers, :command_options
attr_reader :option_parsers, :command_options
attr_accessor :global_options, :rendered
@no_option_commands ||= []
GLOBAL_OPTIONS = {
:help=>{:type=>:boolean, :desc=>"Display a command's help"},
Expand Down
6 changes: 3 additions & 3 deletions lib/boson/view.rb
Expand Up @@ -12,8 +12,8 @@ def enable
# Renders any object via Hirb. Options are passed directly to
# {Hirb::Console.render_output}[http://tagaholic.me/hirb/doc/classes/Hirb/Console.html#M000011].
def render(object, options={}, return_obj=false)
if silent_object?(object)
puts(object.inspect) unless options[:silence_booleans]
if options[:inspect] || inspected_object?(object)
puts(object.inspect)
else
render_object(object, options, return_obj)
end
Expand All @@ -34,7 +34,7 @@ def toggle_pager
Hirb::View.toggle_pager
end

def silent_object?(obj)
def inspected_object?(obj)
[nil,false,true].include?(obj)
end

Expand Down
36 changes: 36 additions & 0 deletions test/bin_runner_test.rb
Expand Up @@ -154,6 +154,42 @@ def index(options={})
end
end

context "render_output" do
before(:each) { Scientist.rendered = false; BinRunner.instance_eval "@options = {}" }

test "doesn't render when nil, false or true" do
View.expects(:render).never
[nil, false, true].each do |e|
BinRunner.render_output e
end
end

test "doesn't render when rendered with Scientist" do
Scientist.rendered = true
View.expects(:render).never
BinRunner.render_output 'blah'
end

test "renders with inspect when non-array" do
['man', {:a=>true}, :ok].each do |e|
View.expects(:puts).with(e.inspect)
BinRunner.render_output e
end
end

test "renders with inspect when Scientist rendering toggled off with :render" do
Scientist.global_options = {:render=>true}
View.expects(:puts).with([1,2].inspect)
BinRunner.render_output [1,2]
Scientist.global_options = nil
end

test "renders with hirb when array" do
View.expects(:render_object)
BinRunner.render_output [1,2,3]
end
end

test "parse_args only translates options before command" do
BinRunner.parse_args(['-v', 'com', '-v']).should == ["com", {:verbose=>true}, ['-v']]
BinRunner.parse_args(['com', '-v']).should == ["com", {}, ['-v']]
Expand Down

0 comments on commit 01ece63

Please sign in to comment.