Class: Ruport::DataRow
- Inherits:
-
Object
- Object
- Ruport::DataRow
- 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
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Returns an array of values.
-
#==(other) ⇒ Object
Compares two DataRow objects.
-
#[](key) ⇒ Object
Lets you access individual fields.
-
#[]=(key, value) ⇒ Object
Lets you set field values.
-
#each(&action) ⇒ Object
Iterates through DataRow elements.
-
#eql? ⇒ Boolean
Synonym for DataRow#==.
-
#has_tag?(tag) ⇒ Boolean
Checks to see row includes the tag given.
-
#initialize(data, fields, options = {}) ⇒ DataRow
constructor
Takes data and field names as well as some optional parameters and constructs a DataRow.
-
#tag_as(something) ⇒ Object
Allows you to add a tag to a row.
-
#to_a ⇒ Object
Converts the DataRow to a plain old Array.
-
#to_s ⇒ Object
Converts the DataRow to a string representation for outputting to screen.
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, ={} ) @fields = fields @tags = [:tags] || {} @data = [] nr_action = if data.kind_of?(Array) lambda { |key, index| @data[index] = data.shift || [:filler] } elsif data.kind_of?(DataRow) lambda { |key, index| @data = data.to_a } else lambda { |key, index| @data[index] = data[key] || [:filler] } end @fields.each_with_index { |key, index| nr_action.call(key,index) } end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
69 70 71 |
# File 'lib/ruport/data_row.rb', line 69 def fields @fields end |
#tags ⇒ Object
Returns the value of attribute tags.
69 70 71 |
# File 'lib/ruport/data_row.rb', line 69 def @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
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
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#==
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
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_a ⇒ Object
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_s ⇒ Object
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 |