Browse files

Merge branch 'table'

  • Loading branch information...
2 parents b14bcc0 + d639603 commit 1b30849cfb6d7035bae31f7e32b85ef129bbad0c @Odaeus Odaeus committed May 14, 2010
Showing with 125 additions and 1 deletion.
  1. +46 −0 examples/add_row_table.rb
  2. +47 −0 lib/ruport/data/table.rb
  3. +32 −1 test/table_test.rb
View
46 examples/add_row_table.rb
@@ -0,0 +1,46 @@
+$: << File.join(File.dirname(__FILE__), '..', 'lib')
+require "ruport"
+
+# Example of adding a row inbetween two rows of a already created table.
+
+data = Table(:service,:rate)
+data << ["Mow The Lawn","50.00"]
+data << ["Sew Curtains", "120.00"]
+data << ["Fly To Mars", "10000.00"]
+
+puts data.to_text
+
+
+# Need to add a row
+# Use sub table to split where we want the new row
+# add the row to the first split
+# merge two splits back
+
+puts 'sub table1'
+sub1 = data.sub_table(0..0)
+puts sub1
+
+puts 'sub table2'
+sub2 = data.sub_table(1..-1)
+puts sub2
+
+puts 'Add a row to the first split'
+sub1 << ["Book-keeping", "90.00"]
+puts sub1
+
+puts 'Merge splits'
+data = sub1 + sub2
+puts data
+
+puts 'Add row'
+data.add_row(["Fix Car", "2000"], :position => 0)
+puts data
+
+
+puts 'Add Row after Sew Curtains'
+pos = data.row_search("Sew", :column => 0) # Search for Sew in column 0 and get position.
+pos += 1
+data.add_row(["Wash Car", "8"], :position => pos)
+puts data
+
+
View
47 lib/ruport/data/table.rb
@@ -394,6 +394,16 @@ def <<(row)
return self
end
+ # Add a row to a certain location within the existing table.
+ #
+ # data.add_row([8,9], :position => 0)
+ #
+ #
+ def add_row(row_data, options={})
+ @data.insert(options[:position] || @data.length, recordize(row_data))
+ return self
+ end
+
# Returns the record class constant being used by the table.
def record_class
@record_class.split("::").inject(Class) { |c,el| c.send(:const_get,el) }
@@ -845,6 +855,43 @@ def rows_with(columns,&block)
}
end
+ # Search row for a string and return the position
+ #
+ # Example:
+ #
+ # table = Table.new :data => [["Mow Lawn","50"], ["Sew","40"], ["Clean dishes","5"]],
+ # :column_names => %w[task cost]
+ # table.row_search("Sew", :column => 0) #=> [[1,2,3], [1,4,6]]
+ #
+ # Search for a number in column 0 greater than 999.
+ # result = table.row_search(999, :column => 0, :greater_than => true)
+ #
+ #
+ def row_search(search, options={})
+ position = 0
+
+ if column = options[:column]
+ self.each do |row|
+
+ if gt=options[:greater_than]
+ return position if row[column] > search
+ end
+
+ if lt=options[:less_than]
+ return position if row[column] < search
+ end
+
+ unless gt or lt
+ if row[column] =~ /#{search}/ # Search for part of or whole search text.
+ return position
+ end
+ end
+
+ position += 1
+ end
+ end
+ end
+
# Create a copy of the Table. Records will be copied as well.
#
# Example:
View
33 test/table_test.rb
@@ -269,7 +269,38 @@ def specify_sort_rows_by!
assert_equal sorted_table_bc, table_bc
end
- end
+ end
+
+ context "when adding rows" do
+ def setup
+ @columns = %w[a b c]
+ @data = [[1,2,3],[4,5,6],[7,8,9]]
+ @table = Ruport::Data::Table.new(:column_names => @columns, :data => @data)
+ @new_row = [-1,-2,-3]
+ end
+
+ def specify_insert_at_the_beginning
+ @table.add_row(@new_row, :position => 0)
+ assert_equal Ruport::Data::Table.new(:column_names => @columns,
+ :data => [@new_row] + @data), @table
+ end
+
+ def specify_insert_in_the_middle
+ @table.add_row(@new_row, :position => 2)
+ assert_equal Ruport::Data::Table.new(:column_names => @columns,
+ :data => [[1,2,3],[4,5,6],[-1,-2,-3],[7,8,9]]), @table
+ end
+
+ def specify_insert_at_the_end
+ @table.add_row(@new_row)
+ assert_equal Ruport::Data::Table.new(:column_names => @columns,
+ :data => @data + [@new_row]), @table
+ end
+
+ def should_return_itself
+ assert_equal @table.object_id, @table.add_row(@new_row).object_id
+ end
+ end
def test_record_class
a = Ruport::Data::Table.new( :column_names => %w[first_name last_name c],

0 comments on commit 1b30849

Please sign in to comment.