lib/hirb/helpers/table.rb in hirb-0.2.7 vs lib/hirb/helpers/table.rb in hirb-0.2.8

- old
+ new

@@ -40,10 +40,12 @@ # length than it's truncated and has a ... appended to it. Fields that aren't specified here have no maximum allowed # length. # [:max_width] The maximum allowed width of all fields put together. This option is enforced except when the field_lengths option is set. # This doesn't count field borders as part of the total. # [:number] When set to true, numbers rows by adding a :hirb_number column as the first column. Default is false. + # [:change_fields] A hash to change old field names to new field names. This is useful when wanting to change auto-generated keys to + # more user-friendly names i.e. for array of arrays. # [:filters] A hash of fields and the filters that each row in the field must run through. The filter converts the cell's value by applying # a given proc or an array containing a method and optional arguments to it. # [:vertical] When set to true, renders a vertical table using Hirb::Helpers::VerticalTable. Default is false. # [:all_fields] When set to true, renders fields in all rows. Valid only in rows that are hashes. Default is false. # [:description] When set to true, renders row count description at bottom. Default is true. @@ -62,11 +64,11 @@ end end #:stopdoc: def initialize(rows, options={}) - @options = {:description=>true, :filters=>{}}.merge(options) + @options = {:description=>true, :filters=>{}, :change_fields=>{}}.merge(options) @fields = set_fields(rows) @rows = setup_rows(rows) @headers = @fields.inject({}) {|h,e| h[e] = e.to_s; h} if @options.has_key?(:headers) @headers = @options[:headers].is_a?(Hash) ? @headers.merge(@options[:headers]) : @@ -77,31 +79,40 @@ @fields.unshift :hirb_number end end def set_fields(rows) - if @options[:fields] + fields = if @options[:fields] @options[:fields].dup else if rows[0].is_a?(Hash) keys = @options[:all_fields] ? rows.map {|e| e.keys}.flatten.uniq : rows[0].keys keys.sort {|a,b| a.to_s <=> b.to_s} else rows[0].is_a?(Array) ? (0..rows[0].length - 1).to_a : [] end end + @options[:change_fields].each do |oldf, newf| + (index = fields.index(oldf)) ? fields[index] = newf : fields << newf + end + fields end def setup_rows(rows) - rows ||= [] - rows = [rows] unless rows.is_a?(Array) + rows = Array(rows) if rows[0].is_a?(Array) rows = rows.inject([]) {|new_rows, row| new_rows << array_to_indices_hash(row) } end + @options[:change_fields].each do |oldf, newf| + rows.each {|e| e[newf] = e.delete(oldf) if e.key?(oldf) } + end rows = filter_values(rows) rows.each_with_index {|e,i| e[:hirb_number] = (i + 1).to_s} if @options[:number] + methods.grep(/_callback$/).sort.each do |meth| + rows = send(meth, rows, @options.dup) + end validate_values(rows) rows end def render