Skip to content

Commit

Permalink
see CHANGELOG and examples
Browse files Browse the repository at this point in the history
git-svn-id: http://stonecode.svnrepository.com/svn/ruport/trunk@216 bb2e8eb0-7117-0410-aac4-c024b40ed5f7
  • Loading branch information
sandal committed Sep 22, 2006
1 parent b3c14e1 commit e2a1cf9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
8 changes: 8 additions & 0 deletions examples/basic_grouping.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "ruport"
a = [ ['a',7],['b',5],['c',11],
['d',9],['a',3],['b',2] ].to_table(%w[letter num])
puts "Initial Data:\n#{a}"
b = a.split(:group => "letter")
totals = [].to_table(%w[group sum])
b.each_group { |x| totals << [x,b[x].sum("num")] }
puts "After grouping:\n#{totals}"
2 changes: 1 addition & 1 deletion lib/ruport/data/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def method_missing(id,*args)
end

attr_reader :data
def_delegators :@data, :each, :length, :size, :[], :empty?
def_delegators :@data, :each, :length, :size, :empty?
end
end

3 changes: 1 addition & 2 deletions lib/ruport/data/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ def attributes; @attributes && @attributes.dup; end
# Sets the attribute list for this Record
#
# my_record.attributes = %w[foo bar baz]
def attributes=(a); @attributes=a; end

attr_writer :attributes
# Allows you to change the order of or reduce the number of columns in a
# Record. Example:
#
Expand Down
12 changes: 8 additions & 4 deletions lib/ruport/data/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def initialize(options={})
end

attr_reader :column_names

def_delegator :@data, :[]
# Sets the column names for this table. The single parameter should be
# an array listing the names of the columns.
#
# tbl = Table.new({:data => [1,2,3], [3,4,5], :column_names => %w[a b c]})
# tbl.column_names = %w[e f g]
def column_names=(other)
@column_names = other.dup
map { |r| r.attributes = @column_names }
@column_names ||= other.dup
@column_names.replace(other.dup)
end

# Compares this table to another table and returns true if
Expand All @@ -66,6 +66,7 @@ def column_names=(other)
def eql?(other)
data.eql?(other.data) && column_names.eql?(other.column_names)
end

alias_method :==, :eql?

# Uses Ruport's built-in text plugin to render this table into a string
Expand Down Expand Up @@ -252,12 +253,15 @@ def split(options={})
:column_names => c
)
}
if options[:group].kind_of? Array
rec = if options[:group].kind_of? Array
Ruport::Data::Record.new(data,
:attributes => group.map { |e| e.join("_") } )
else
Ruport::Data::Record.new data, :attributes => group
end
class << rec
def each_group; attributes.each { |a| yield(a) }; end
end; rec
end

# Calculates sums. If a column name or index is given, it will try to
Expand Down
12 changes: 11 additions & 1 deletion test/test_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_constructors
end

a = Ruport::Data::Record.new [1,2,3]
assert a.respond_to?(:[])
b = a.dup
b.attributes = %w[col1 col2 col3]
tables.zip([[],[],[a],[b]]).each { |t,n| assert_equal n, t.data }
Expand Down Expand Up @@ -116,7 +117,11 @@ def test_split
table << ['d',9,10]

group = table.split :group => "c1"

assert group.respond_to?(:each_group)
expected = %w[a d b]

group.each_group { |g| assert_equal(expected.shift, g) }

t = table.reorder("c2","c3")

data = [[t[0],t[2]],[t[1],t[4]],[t[3]]]
Expand All @@ -129,6 +134,11 @@ def test_split
table << ['d',9,11]

group = table.split :group => %w[c1 c2]
assert group.respond_to?(:each_group)
expected = %w[a_2 d_5 a_4 b_3 d_9]

group.each_group { |g| assert_equal(expected.shift, g) }

t = table.reorder("c3")
data = [[t[0],t[5]],[t[1]],[t[2]],[t[3]],[t[4],t[6]]]

Expand Down

0 comments on commit e2a1cf9

Please sign in to comment.