Permalink
Browse files

call compute_<computable> so we can enforce the calculator supporting

dynamic compute methods
  • Loading branch information...
peterberkenbosch committed Nov 27, 2013
1 parent df7f607 commit 38654ddb2046e6cc043997716be7c3b9fb61ed58
Showing with 91 additions and 1 deletion.
  1. +16 −1 core/app/models/spree/calculator.rb
  2. +75 −0 core/spec/models/spree/calculator_spec.rb
@@ -5,7 +5,22 @@ class Calculator < ActiveRecord::Base
# This method must be overriden in concrete calculator.
#
# It should return amount computed based on #calculable and/or optional parameter
def compute(something = nil)
def compute(computable = nil)
raise NotImplementedError, 'please use concrete calculator' unless computable

This comment has been minimized.

Show comment
Hide comment
@huoxito

huoxito Nov 27, 2013

I don't get why we would still need the raise here? The idea looks good btw

@huoxito

huoxito Nov 27, 2013

I don't get why we would still need the raise here? The idea looks good btw

This comment has been minimized.

Show comment
Hide comment
@peterberkenbosch

peterberkenbosch Nov 27, 2013

Owner
@peterberkenbosch

peterberkenbosch via email Nov 27, 2013

Owner
# Spree::LineItem -> :compute_line_item
method = "compute_#{computable.class.name.demodulize.underscore}".to_sym
self.send(method, computable)
end
def compute_shipment(shipment)
raise NotImplementedError, 'please use concrete calculator'
end
def compute_line_item(line_item)
raise NotImplementedError, 'please use concrete calculator'
end
def compute_order(order)
raise NotImplementedError, 'please use concrete calculator'
end
@@ -0,0 +1,75 @@
require 'spec_helper'
describe Spree::Calculator do
let(:order) { create(:order) }
let!(:line_item) { create(:line_item, :order => order) }
let(:shipment) { create(:shipment, :order => order, :stock_location => create(:stock_location_with_items)) }
context "with computable" do
context "and compute methods stubbed out" do
context "with a Spree::LineItem" do
it "calls compute_line_item" do
subject.should_receive(:compute_line_item).with(line_item)
subject.compute(line_item)
end
end
context "with a Spree::Order" do
it "calls compute_order" do
subject.should_receive(:compute_order).with(order)
subject.compute(order)
end
end
context "with a Spree::Shipment" do
it "calls compute_shipment" do
subject.should_receive(:compute_shipment).with(shipment)
subject.compute(shipment)
end
end
context "with a arbitray object" do
it "calls the correct compute" do
s = "Calculator can all"
subject.should_receive(:compute_string).with(s)
subject.compute(s)
end
end
end
context "with no stubbing" do
context "with a Spree::LineItem" do
it "raises NotImplementedError" do
expect{subject.compute(line_item)}.to raise_error NotImplementedError
end
end
context "with a Spree::Order" do
it "raises NotImplementedError" do
expect{subject.compute(order)}.to raise_error NotImplementedError
end
end
context "with a Spree::Shipment" do
it "raises NotImplementedError" do
expect{subject.compute(shipment)}.to raise_error NotImplementedError
end
end
context "with a arbitray object" do
it "raises NoMethodError" do
s = "Calculator can all"
expect{subject.compute(s)}.to raise_error NoMethodError
end
end
end
end
context "without computable" do
it "raises NotImplementedError" do
expect{subject.compute}.to raise_error NotImplementedError
end
end
end

0 comments on commit 38654dd

Please sign in to comment.