Class: Ruport::DataRow

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruport/data_row.rb

Overview

DataRows are Enumerable lists which can be accessed by field name or ordinal position.

They feature a tagging system, allowing them to be easily compared or recalled.

DataRows form the elements of DataSets

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



69
70
71
# File 'lib/ruport/data_row.rb', line 69

def fields
  @fields
end

#tagsObject

Returns the value of attribute tags.



69
70
71
# File 'lib/ruport/data_row.rb', line 69

def tags
  @tags
end

Instance Method Details

#+(other) ⇒ Object

Returns an array of values. Should probably return a DataRow. Loses field information.



73
74
75
# File 'lib/ruport/data_row.rb', line 73

def +(other)
  self.to_a + other.to_a
end

#==(other) ⇒ Object

Compares two DataRow objects. If values and fields are the same (and in the correct order) returns true. Otherwise returns false.



135
136
137
# File 'lib/ruport/data_row.rb', line 135

def ==(other)
  self.to_a.eql?(other.to_a) && @fields.eql?(other.fields)
end

#[](key) ⇒ Object

Lets you access individual fields

i.e. row or row



80
81
82
# File 'lib/ruport/data_row.rb', line 80

def [](key)
  key.kind_of?(Fixnum) ? @data[key] : @data[@fields.index(key)] 
end

#[]=(key, value) ⇒ Object

Lets you set field values

i.e. row = ‘2038291203’, row = “allen”



87
88
89
90
91
92
93
# File 'lib/ruport/data_row.rb', line 87

def []=(key,value)
  if key.kind_of?(Fixnum)
    @data[key] = value 
  else
    @data[@fields.index(key)] = value
  end                          
end

#each(&action) ⇒ Object

Iterates through DataRow elements. Accepts a block.



118
119
120
# File 'lib/ruport/data_row.rb', line 118

def each(&action)
  @data.each(&action)
end

#eql?Boolean

Synonym for DataRow#==

Returns:

  • (Boolean)


140
141
142
# File 'lib/ruport/data_row.rb', line 140

def eql?
  self == other
end

#has_tag?(tag) ⇒ Boolean

Checks to see row includes the tag given.

Example:

>> row.has_tag? :running_balance
=> true

Returns:

  • (Boolean)


113
114
115
# File 'lib/ruport/data_row.rb', line 113

def has_tag?(tag)
  @tags.include?(tag)
end

#tag_as(something) ⇒ Object

Allows you to add a tag to a row.

Examples:

row.tag_as(:jay_cross) if row["product"].eql?("im_courier")
row.tag_as(:running_balance) if row.fields.include?("RB")


129
130
131
# File 'lib/ruport/data_row.rb', line 129

def tag_as(something)
  @tags[something] = true
end

#to_aObject

Converts the DataRow to a plain old Array



96
97
98
# File 'lib/ruport/data_row.rb', line 96

def to_a
  @data
end

#to_sObject

Converts the DataRow to a string representation for outputting to screen.



102
103
104
# File 'lib/ruport/data_row.rb', line 102

def to_s
  "[" + @data.join(",") + "]"
end