Skip to content

Commit

Permalink
added in create_rag_group
Browse files Browse the repository at this point in the history
git-svn-id: http://stonecode.svnrepository.com/svn/ruport/trunk@296 bb2e8eb0-7117-0410-aac4-c024b40ed5f7
  • Loading branch information
sandal committed Oct 21, 2006
1 parent 6144702 commit 0100532
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/line_graph_report.rb
Expand Up @@ -19,6 +19,6 @@ class GraphSample < Ruport::Report
end
end

GraphSample.run { |r| puts r.write }
GraphSample.run(:tries => 3, :timeout => 1) { |r| sleep 2; puts r.results }


2 changes: 1 addition & 1 deletion lib/ruport.rb
Expand Up @@ -65,6 +65,6 @@ def self.configure(&block)
end


%w[attempt config meta_tools report format query data mailer].each { |lib|
%w[attempt config data meta_tools report format query mailer].each { |lib|
require "ruport/#{lib}"
}
1 change: 0 additions & 1 deletion lib/ruport/attempt.rb
Expand Up @@ -52,7 +52,6 @@ def attempt
if @tries > 0
msg = "Error on attempt # #{count}: #{error}; retrying"
count += 1
#warn msg if @warnings
Ruport.log(msg, :level => log_level)
@interval += @increment if @increment
sleep @interval
Expand Down
2 changes: 1 addition & 1 deletion lib/ruport/data.rb
@@ -1 +1 @@
%w[taggable record collection table set groupable ].each { |l| require "ruport/data/#{l}" }
%w[groupable taggable record collection table set].each { |l| require "ruport/data/#{l}" }
8 changes: 7 additions & 1 deletion lib/ruport/data/groupable.rb
@@ -1,11 +1,17 @@
module Ruport::Data
module Groupable

def group_by_tag
r_tags = data.map {|row| row.tags}.flatten.uniq
d = r_tags.map do |t|
select {|row| row.tags.include? t }.to_table(column_names)
select {|row| row.tags.include? t }.to_table(column_names)
end
Record.new d, :attributes => r_tags
end

def create_tag_group(label,&block)
select(&block).each { |r| r.tag label }
end

end
end
10 changes: 9 additions & 1 deletion lib/ruport/data/table.rb
Expand Up @@ -33,7 +33,7 @@ module Ruport::Data
#
# For building a table using ActiveRecord, have a look at Ruport::Reportable.
class Table < Collection

include Groupable
# Creates a new table based on the supplied options.
# Valid options are :data and :column_names.
#
Expand Down Expand Up @@ -327,4 +327,12 @@ def sigma(column=nil)
alias_method :sum, :sigma

end

end

module Ruport::Data::TableHelper
def table(names=[])
t = [].to_table(names)
yield(t) if block_given?; t
end
end
4 changes: 3 additions & 1 deletion lib/ruport/report.rb
Expand Up @@ -11,6 +11,7 @@
require "forwardable"

module Ruport

# === Overview
#
# The Ruport::Report class povides a high level interface to most of Ruport's
Expand Down Expand Up @@ -71,6 +72,7 @@ module Ruport
class Report
extend Forwardable

include Ruport::Data::TableHelper
# When initializing a report, you can provide a default mailer and source by
# giving a name of a valid source or mailer you've defined via
# Ruport::Config
Expand Down Expand Up @@ -183,7 +185,7 @@ def process_text(string, options)
#
# E.g
#
# text_processor(:unix_newlines) { results.gsub!(/\r\n/,"\n") }
# text_processor(:unix_newlines) { |r| r.gsub(/\r\n/,"\n") }
def text_processor(label,&block)
Format.register_filter(label, &block)
end
Expand Down
11 changes: 11 additions & 0 deletions test/test_groupable.rb
Expand Up @@ -25,6 +25,17 @@ def test_simple
:attributes => ["foo","bar"] )
assert_equal expected, a.group_by_tag
end

def test_create_tag_group
a = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
expected = Ruport::Data::Record.new( [ [[1,2,3],[7,8,9]].to_table(%w[a b c]),
[[4,5,6]].to_table(%w[a b c]) ],
:attributes => %w[starts_odd starts_even])
a.create_tag_group(:starts_odd) { |r| (r[0] % 2) != 0 }
a.create_tag_group(:starts_even) { |r| (r[0] % 2).zero? }

assert_equal expected, a.group_by_tag
end
end


11 changes: 11 additions & 0 deletions test/test_table.rb
Expand Up @@ -294,6 +294,17 @@ def test_ensure_coerce_sum

end

def test_table_shortcut
self.class.send(:include,Ruport::Data::TableHelper)

a = table(%w[a b c]) do |t|
[[1,2,3],[4,5,6]].each { |r| t << r }
end

assert_equal([[1,2,3],[4,5,6]].to_table(%w[a b c]),a)

end

def test_setting_column_names_changes_record_attributes
table = Ruport::Data::Table.new :column_names => %w[a b c],
:data => [[1,2,3],[4,5,6]]
Expand Down

0 comments on commit 0100532

Please sign in to comment.