Skip to content

Commit

Permalink
Extracted method to return a function default into a utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
dkubb committed May 26, 2011
1 parent eac4869 commit 73271e3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/veritas/optimizer/algebra/summarization.rb
Expand Up @@ -38,6 +38,19 @@ def optimize_summarize_per
# Optimize when the operand is Empty
class EmptyOperand < self

# Return the default value for a function
#
# @param [Object] function
#
# @return [Object]
#
# @api private
def self.extension_default(function)
if function.respond_to?(:default)
function.finalize(function.default)
end
end

# Test if the operand is empty
#
# @return [Boolean]
Expand All @@ -60,15 +73,13 @@ def optimize

# Return the extensions for the optimized relation
#
# @return [Hhash{Attribute => #call}]
# @return [Hash{Attribute => #call}]
#
# @api private
def extensions
extensions = {}
operation.summarizers.each do |attribute, function|
extensions[attribute] = if function.respond_to?(:default)
function.finalize(function.default)
end
extensions[attribute] = self.class.extension_default(function)
end
extensions
end
Expand Down
@@ -0,0 +1,22 @@
# encoding: utf-8

require 'spec_helper'

describe Optimizer::Algebra::Summarization::EmptyOperand, '.extension_default' do
subject { object.extension_default(function) }

let(:object) { described_class }

context 'when the function has a default' do
let(:operand) { mock('Operand') }
let(:function) { Aggregate::Count.new(operand) }

it { should eql(0) }
end

context 'when the function does not have a default' do
let(:function) { proc {} }

it { should be_nil }
end
end
1 change: 1 addition & 0 deletions veritas-optimizer.gemspec
Expand Up @@ -174,6 +174,7 @@ Gem::Specification.new do |s|
"spec/unit/veritas/optimizer/algebra/restriction/tautology/optimize_spec.rb",
"spec/unit/veritas/optimizer/algebra/restriction/unoptimized_operand/optimizable_spec.rb",
"spec/unit/veritas/optimizer/algebra/restriction/unoptimized_operand/optimize_spec.rb",
"spec/unit/veritas/optimizer/algebra/summarization/empty_operand/class_methods/extension_default_spec.rb",
"spec/unit/veritas/optimizer/algebra/summarization/empty_operand/optimizable_spec.rb",
"spec/unit/veritas/optimizer/algebra/summarization/empty_operand/optimize_spec.rb",
"spec/unit/veritas/optimizer/algebra/summarization/empty_summarize_per/optimizable_spec.rb",
Expand Down

0 comments on commit 73271e3

Please sign in to comment.