Method: Ruport::DataRow#initialize

Defined in:
lib/ruport/data_row.rb

#initialize(data, fields, options = {}) ⇒ DataRow

Takes data and field names as well as some optional parameters and constructs a DataRow.

data can be specified in Hash, Array, or DataRow form

Options:

:filler

this will be used as a default value for empty

:tags

an initial set of tags for the row

Examples:

>> Ruport::DataRow.new [1,2,3,4,5], [:a,:b,:c,:d,:e], 
   :tags => %w[cat dog]
=> #<Ruport::DataRow:0xb77e4b04 @fields=[:a, :b, :c, :d, :e], 
   @data=[1, 2, 3, 4, 5], @tags=["cat", "dog"]>

>> Ruport::DataRow.new({ :a => 'moo', :c => 'caw'} , [:a,:b,:c,:d,:e],
   :tags => %w[cat dog])
=> #<Ruport::DataRow:0xb77c298c @fields=[:a, :b, :c, :d, :e],
   @data=["moo", nil, "caw", nil, nil], @tags=["cat", "dog"]>

>> Ruport::DataRow.new [1,2,3], [:a,:b,:c,:d,:e], :tags => %w[cat dog],
   :filler => 0
=> #<Ruport::DataRow:0xb77bb4d4 @fields=[:a, :b, :c, :d, :e], 
    @data=[1, 2, 3, 0, 0], @tags=["cat", "dog"]>


54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruport/data_row.rb', line 54

def initialize( data, fields, options={} )
  @fields   = fields
  @tags     = options[:tags] || {}
  @data     = []
  nr_action =  
    if data.kind_of?(Array)
      lambda { |key, index| @data[index] = data.shift || options[:filler] }
    elsif data.kind_of?(DataRow)
      lambda { |key, index| @data = data.to_a }
    else
      lambda { |key, index| @data[index] = data[key] || options[:filler] }
    end     
  @fields.each_with_index { |key, index| nr_action.call(key,index) }
end