Skip to content

Commit

Permalink
Added filtering, added sorting by record value
Browse files Browse the repository at this point in the history
  • Loading branch information
hsume2 committed Jun 10, 2010
1 parent 398ea67 commit 99ec0f6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -91,7 +91,7 @@ Inspired by `Machinist` and `factory_girl`
# Future

* Two-way reporting
* Filtering
* Filtering documentation
* Documentation

*****
Expand Down
20 changes: 17 additions & 3 deletions lib/rose/attribute.rb
Expand Up @@ -62,15 +62,29 @@ def column_names

# This is a value object for sort parameters
class Sort
attr_reader :column_name, :order
attr_reader :column_name, :order, :sort_block

def initialize(column_name, order)
def initialize(column_name, order, &sort_block)
@column_name = column_name
@order = order
@sort_block = sort_block
end

def on(table)
table.sort_rows_by!(@column_name, :order => @order)
if @sort_block
table.sort_rows_by!(nil, :order => @order) do |row|
@sort_block.call(row[@column_name])
end
else
table.sort_rows_by!(@column_name, :order => @order)
end
table
end
end

class Filter < Indirect
def on(table)
table.data.reject! { |record| !@value_block.call(record) }
table
end
end
Expand Down
9 changes: 7 additions & 2 deletions lib/rose/seedling.rb
Expand Up @@ -34,11 +34,16 @@ def rows(&blk)

# @param [String, Symbol] column_name the column to sort by
# @param [:ascending, :descending] order the order to sort by
def sort(column_name, order = :ascending)
@options[:sort] = Attribute::Sort.new(column_name, order)
def sort(column_name, order = :ascending, &sort_block)
@options[:sort] = Attribute::Sort.new(column_name, order, &sort_block)
@alterations << @options[:sort]
end

def filter(&filter_block)
@options[:filter] = Attribute::Filter.new(nil, nil, filter_block)
@alterations << @options[:filter]
end

# @param [String, Symbol] column_name the column to group by
# @yield SummaryProxy
def summary(column_name, &blk)
Expand Down
79 changes: 79 additions & 0 deletions spec/rose/object_spec.rb
Expand Up @@ -111,6 +111,25 @@ class Flower < Struct.new(:type, :color, :age)
end
sort("Age", :descending)
}

@negative_sort_block = negative_sort_block = lambda { |v| v.to_i * -1 }
@positive_sort_block = positive_sort_block = lambda { |v| v.to_i }

Rose.make(:with_sort_by_block_negative, :class => Flower) {
rows do
column(:type => "Type")
column(:age => "Age")
end
sort("Age", :descending, &negative_sort_block)
}

Rose.make(:with_sort_by_block_positive, :class => Flower) {
rows do
column(:type => "Type")
column(:age => "Age")
end
sort("Age", :descending, &positive_sort_block)
}

Rose.make(:with_ordered_execution_asc, :class => Flower) {
rows do
Expand All @@ -133,6 +152,17 @@ class Flower < Struct.new(:type, :color, :age)
column("Color") { |colors| colors.join(", ") }
end
}

@filter_block = filter_block = lambda { |row| row["Color"] != "blue" }

Rose.make(:with_filter, :class => Flower) {
rows do
column(:type => "Type")
column(:color => "Color")
column(:age => "Age")
end
filter(&filter_block)
}
end

after do
Expand Down Expand Up @@ -166,6 +196,14 @@ class Flower < Struct.new(:type, :color, :age)
report.options[:class].should == RoseyObject
end
end

it "should support sort by block" do
Rose(:with_sort_by_block_negative).tap do |report|
report.options[:sort].column_name.should == "Age"
report.options[:sort].order.should == :descending
report.options[:sort].sort_block.should == @negative_sort_block
end
end

it "should support summary" do
Rose(:with_summary).tap do |report|
Expand All @@ -181,6 +219,12 @@ class Flower < Struct.new(:type, :color, :age)
report.options[:pivot].value_block.should == @value_block
end
end

it "should support filtering" do
Rose(:with_filter).tap do |report|
report.options[:filter].value_block.should == @filter_block
end
end
end

context "run report" do
Expand Down Expand Up @@ -276,6 +320,30 @@ class Flower < Struct.new(:type, :color, :age)
+-----------------------+
eo_table
end

it "should sort by block (-)" do
Rose(:with_sort_by_block_negative).bloom(@flowers).should match_table <<-eo_table.gsub(%r{^ }, '')
+---------------+
| Type | Age |
+---------------+
| roses | 1 |
| violets | 2 |
| roses | 3 |
+---------------+
eo_table
end

it "should sort by block (+)" do
Rose(:with_sort_by_block_positive).bloom(@flowers).should match_table <<-eo_table.gsub(%r{^ }, '')
+---------------+
| Type | Age |
+---------------+
| roses | 3 |
| violets | 2 |
| roses | 1 |
+---------------+
eo_table
end

it "should summarize columns" do
Rose(:with_summary).bloom(@flowers).should match_table <<-eo_table.gsub(%r{^ }, '')
Expand Down Expand Up @@ -341,5 +409,16 @@ class Flower < Struct.new(:type, :color, :age)
+----------------------------+
eo_table
end

it "should filter rows" do
Rose(:with_filter).bloom(@flowers).should match_table <<-eo_table.gsub(%r{^ }, '')
+---------------------+
| Type | Color | Age |
+---------------------+
| roses | red | 1 |
| roses | red | 3 |
+---------------------+
eo_table
end
end
end
6 changes: 4 additions & 2 deletions spec/rose_spec.rb
Expand Up @@ -28,7 +28,9 @@
end
end

it "should not contain code smells" do
Dir['lib/**/*.rb'].should_not reek
if $reek
it "should not contain code smells" do
Dir['lib/**/*.rb'].should_not reek
end
end
end

0 comments on commit 99ec0f6

Please sign in to comment.