Class: Hirb::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/hirb/formatter.rb

Overview

A Formatter object formats an output object (using Formatter.format_output) into a string based on the views defined for its class and/or ancestry.

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(additional_config = {}) ⇒ Formatter

:nodoc:



17
18
19
20
# File 'lib/hirb/formatter.rb', line 17

def initialize(additional_config={}) #:nodoc:
  @klass_config = {}
  @config = additional_config || {}
end

Class Attribute Details

.dynamic_configObject

This config is used by Formatter.format_output to lazily load dynamic views defined with Hirb::DynamicView. This hash has the same format as Formatter.config.



8
9
10
# File 'lib/hirb/formatter.rb', line 8

def dynamic_config
  @dynamic_config
end

.to_a_classesObject

Array of classes whose objects respond to :to_a and allow the first element of the converted array to determine the output class.



12
13
14
# File 'lib/hirb/formatter.rb', line 12

def to_a_classes
  @to_a_classes
end

Instance Method Details

#_format_output(output, options, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hirb/formatter.rb', line 64

def _format_output(output, options, &block)
  output = options[:output_method] ? (output.is_a?(Array) ?
    output.map {|e| call_output_method(options[:output_method], e) } :
    call_output_method(options[:output_method], output) ) : output
  args = [output]
  args << options[:options] if options[:options] && !options[:options].empty?
  if options[:method]
    send(options[:method],*args)
  elsif options[:class] && (helper_class = Helpers.helper_class(options[:class]))
    helper_class.render(*args, &block)
  elsif options[:output_method]
    output
  end
end

#add_klass_config_if_true(hash, klass) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/hirb/formatter.rb', line 110

def add_klass_config_if_true(hash, klass)
  if yield(@config, klass)
    Util.recursive_hash_merge hash, @config[klass]
  elsif yield(self.class.dynamic_config, klass)
    @config[klass] = self.class.dynamic_config[klass].dup # copy to local
    Util.recursive_hash_merge hash, self.class.dynamic_config[klass]
  else
    hash
  end
end

#add_view(klass, view_config) ⇒ Object

Adds the view for the given class and view hash config. See Formatter.config for valid keys for view hash.



42
43
44
45
46
# File 'lib/hirb/formatter.rb', line 42

def add_view(klass, view_config)
  @klass_config.delete(klass)
  @config[klass.to_s] = view_config
  true
end

#build_klass_config(output_class) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/hirb/formatter.rb', line 101

def build_klass_config(output_class)
  output_ancestors = output_class.ancestors.map {|e| e.to_s}.reverse
  output_ancestors.pop
  hash = output_ancestors.inject({}) {|h, klass|
    add_klass_config_if_true(h, klass) {|c,klass| c[klass] && c[klass][:ancestor] }
  }
  add_klass_config_if_true(hash, output_class.to_s) {|c,klass| c[klass] }
end

#call_output_method(output_method, output) ⇒ Object



91
92
93
# File 'lib/hirb/formatter.rb', line 91

def call_output_method(output_method, output)
  output_method.is_a?(Proc) ? output_method.call(output) : output.send(output_method)
end

#configObject

A hash of Ruby class strings mapped to view hashes. A view hash must have at least a :method, :output_method or :class option for a view to be applied to an output. A view hash has the following keys:

:method

Specifies a global (Kernel) method to do the formatting.

:class

Specifies a class to do the formatting, using its render() class method. If a symbol it’s converted to a corresponding Hirb::Helpers::* class if it exists.

:output_method

Specifies a method or proc to call on output before passing it to a helper. If the output is an array, it’s applied to every element in the array.

:options

Options to pass the helper method or class.

:ancestor

Boolean which when true causes subclasses of the output class to inherit its config. This doesn’t effect the current output class. Defaults to false. This is used by ActiveRecord classes.

Examples:
  {'WWW::Delicious::Element'=>{:class=>'Hirb::Helpers::ObjectTable', :ancestor=>true, :options=>{:max_width=>180}}}
  {'Date'=>{:class=>:auto_table, :ancestor=>true}}
  {'Hash'=>{:method=>:puts}}


37
38
39
# File 'lib/hirb/formatter.rb', line 37

def config
  @config
end

#determine_output_class(output) ⇒ Object



86
87
88
89
# File 'lib/hirb/formatter.rb', line 86

def determine_output_class(output)
  output.respond_to?(:to_a) && to_a_classes.any? {|e| output.is_a?(e) } ?
    Array(output)[0].class : output.class
end

#format_output(output, options = {}, &block) ⇒ Object

This method looks for an output object’s view in Formatter.config and then Formatter.dynamic_config. If a view is found, a stringified view is returned based on the object. If no view is found, nil is returned. The options this class takes are a view hash as described in Formatter.config. These options will be merged with any existing helper config hash an output class has in Formatter.config. Any block given is passed along to a helper class.



52
53
54
55
56
57
# File 'lib/hirb/formatter.rb', line 52

def format_output(output, options={}, &block)
  output_class = determine_output_class(output)
  options = parse_console_options(options) if options.delete(:console)
  options = Util.recursive_hash_merge(klass_config(output_class), options)
  _format_output(output, options, &block)
end

#klass_config(output_class) ⇒ Object

Internal view options built from user-defined ones. Options are built by recursively merging options from oldest ancestors to the most recent ones.



97
98
99
# File 'lib/hirb/formatter.rb', line 97

def klass_config(output_class)
  @klass_config[output_class] ||= build_klass_config(output_class)
end

#parse_console_options(options) ⇒ Object

:nodoc:



79
80
81
82
83
84
# File 'lib/hirb/formatter.rb', line 79

def parse_console_options(options) #:nodoc:
  real_options = [:method, :class, :output_method].inject({}) do |h, e|
    h[e] = options.delete(e) if options[e]; h
  end
  real_options.merge! :options=>options
end

#reset_klass_configObject



121
122
123
# File 'lib/hirb/formatter.rb', line 121

def reset_klass_config
  @klass_config = {}
end

#to_a_classesObject

:stopdoc:



60
61
62
# File 'lib/hirb/formatter.rb', line 60

def to_a_classes
  @to_a_classes ||= self.class.to_a_classes.map {|e| Util.any_const_get(e) }.compact
end