lib/hirb/view.rb in cldwalker-hirb-0.1.1 vs lib/hirb/view.rb in cldwalker-hirb-0.1.2
- old
+ new
@@ -41,10 +41,11 @@
# and returns true. Returns false if no formatter found.
# ==== Options:
# [:method] Specifies a global (Kernel) method to do the formatting.
# [:class] Specifies a class to do the formatting, using its render() class method. The render() method's arguments are the output and
# an options hash.
+ # [:output_method] Specifies a method to call on output before passing it to a formatter.
# [:options] Options to pass the formatter method or class.
def render_output(output, options={}, &block)
if block && block.arity > 0
formatted_output = block.call(output)
render_method.call(formatted_output)
@@ -68,11 +69,11 @@
@render_method = default_render_method
end
# Config hash which maps classes to view hashes. View hashes are the same as the options hash of render_output().
def output_config
- @config[:output]
+ config[:output]
end
def output_config=(value)
@config[:output] = value
end
@@ -81,24 +82,49 @@
# most recent config changes.
def reload_config
current_config = self.config.dup.merge(:output=>output_config)
load_config(current_config)
end
-
- #:stopdoc:
- def console_render_output(output, options={}, &block)
+
+ # A console version of render_output which takes its same options but allows for some shortcuts.
+ # Examples:
+ # console_render_output output, :tree, :type=>:directory
+ # # is the same as:
+ # render_output output, :class=>"Hirb::Helpers::Tree", :options=> {:type=>:directory}
+ #
+ def console_render_output(*args, &block)
+ load_config unless @config
+ output = args.shift
+ if args[0].is_a?(Symbol) && (view = args.shift)
+ symbol_options = find_view(view)
+ end
+ options = args[-1].is_a?(Hash) ? args[-1] : {}
+ options.merge!(symbol_options) if symbol_options
# iterates over format_output options that aren't :options
- real_options = [:method, :class].inject({}) do |h, e|
- h[e] = options.delete(e) if options[e]
- h
+ real_options = [:method, :class, :output_method].inject({}) do |h, e|
+ h[e] = options.delete(e) if options[e]; h
end
render_output(output, real_options.merge(:options=>options), &block)
end
+ #:stopdoc:
+ def find_view(name)
+ name = name.to_s
+ if (view_method = output_config.values.find {|e| e[:method] == name })
+ {:method=>view_method[:method]}
+ elsif (view_class = Hirb::Helpers.constants.find {|e| e == Util.camelize(name)})
+ {:class=>"Hirb::Helpers::#{view_class}"}
+ else
+ {}
+ end
+ end
+
def format_output(output, options={})
output_class = determine_output_class(output)
- options = output_class_options(output_class).merge(options)
+ options = Util.recursive_hash_merge(output_class_options(output_class), options)
+ output = options[:output_method] ? (output.is_a?(Array) ? output.map {|e| e.send(options[:output_method])} :
+ output.send(options[:output_method]) ) : output
args = [output]
args << options[:options] if options[:options] && !options[:options].empty?
if options[:method]
new_output = send(options[:method],*args)
elsif options[:class] && (view_class = Util.any_const_get(options[:class]))
@@ -113,15 +139,12 @@
else
output.class
end
end
- # Loads config
def load_config(additional_config={})
- new_config = default_config
- new_config[:output].merge!(additional_config.delete(:output) || {})
- self.config = new_config.merge(additional_config)
+ self.config = Util.recursive_hash_merge default_config, additional_config
true
end
# Stores all view config. Current valid keys:
# :output- contains value of output_config
@@ -153,12 +176,10 @@
def default_render_method
lambda {|output| puts output}
end
def default_config
- conf = Hirb.config[:view] || {}
- conf[:output] = default_output_config.merge(conf[:output] || {})
- conf
+ Hirb::Util.recursive_hash_merge({:output=>default_output_config}, Hirb.config[:view] || {} )
end
def default_output_config
Hirb::Views.constants.inject({}) {|h,e|
output_class = e.to_s.gsub("_", "::")