Skip to content

Commit

Permalink
Merge 19de4ae into cd32058
Browse files Browse the repository at this point in the history
  • Loading branch information
sorah committed Oct 11, 2012
2 parents cd32058 + 19de4ae commit 2255bd9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/chanko/active_if.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module Chanko
class ActiveIf

mattr_accessor :blocks, :files, :loaded, :definitions, :expanded_judgements
self.files = []
self.expanded_judgements = []
self.definitions = {}

def initialize(*names, &block)
@options = names.last.is_a?(Hash) ? names.pop : {}
@blocks = names.map do |name|
case name
when Chanko::ActiveIf::Any
name.block
else
self.class.fetch(name)
self.class.fetch(name, @options[:raise])
end
end
@blocks << block if block
Expand Down Expand Up @@ -56,11 +56,11 @@ def load_definitions!
end
end

def fetch(name)
def fetch(name, raise_error = false)
load_definitions!
result = self.definitions[name.to_sym]
return result if result
Chanko::ExceptionNotifier.notify("missing Activeif definition #{name}", false, :exception_klass => Chanko::Exception::MissingActiveIfDefinition)
Chanko::ExceptionNotifier.notify("missing Activeif definition #{name}", raise_error, :exception_klass => Chanko::Exception::MissingActiveIfDefinition)
Chanko::ActiveIf.default
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chanko/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def invoke!(scope, options={})
end
rescue ::Exception => e
Chanko::Loader.aborted(unit.unit_name)
Chanko::ExceptionNotifier.notify("raise exception #{unit.name}##{@label} => #{e.message}", self.unit.default?,
Chanko::ExceptionNotifier.notify("raise exception #{unit.name}##{@label} => #{e.message}", @unit.propagates_errors?,
:exception => e, :backtrace => e.backtrace[0..20], :key => "#{unit.name}_exception", :context => scope)
return Chanko::Aborted
ensure
Expand Down
5 changes: 3 additions & 2 deletions lib/chanko/method_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ def unit(_unit_label=nil, &block)
begin
Chanko::Loader.push_scope(unit_label) if _unit_label
begin
yield Chanko::Loader.fetch(unit_label)
unit = Chanko::Loader.fetch(unit_label)
yield unit
rescue ::Exception => e
Chanko::ExceptionNotifier.notify("unknown error #{_unit_label}", false,
Chanko::ExceptionNotifier.notify("unknown error #{_unit_label}", unit.propagates_errors?,
:exception => e,
:key => "method_proxy unknown error #{_unit_label}",
:context => self,
Expand Down
20 changes: 15 additions & 5 deletions lib/chanko/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def expand(klass_name, &block)
begin
klass = klass_name.constantize
rescue NameError => e
Chanko::ExceptionNotifier.notify("expand name error #{self.name} #{klass_name}", false,
Chanko::ExceptionNotifier.notify("expand name error #{self.name} #{klass_name}", self.expand_owner.propagates_errors?,
:key => "#{self.name} expand error", :exception => e,
:context => self, :backtrace => e.backtrace[0..20]
)
Expand All @@ -72,16 +72,23 @@ module ClassMethods
attr_reader :shared_methods

def active_if(*symbols, &block)
@active_if = Chanko::ActiveIf.new(*symbols, &block)
@active_if = Chanko::ActiveIf.new(*(symbols + [:raise => @propagates_errors]), &block)
end
alias_method :judge, :active_if

def propagates_errors(o=true)
@propagates_errors = o
end
alias_method :propagates_errors=, :propagates_errors

def propagates_errors?; @propagates_errors ||= false; end

def active?(context=nil, options={})
begin
options = options.merge(:unit => self)
@active_if.enabled?(context, options)
rescue ::Exception => e
Chanko::ExceptionNotifier.notify("Activeif definition #{self.name} raised", false,
Chanko::ExceptionNotifier.notify("Activeif definition #{self.name} raised", @propagates_errors,
:key => "#{self.name}_active?",
:context => context,
:backtrace => e.backtrace[0..20],
Expand Down Expand Up @@ -109,10 +116,13 @@ def expand_prefix
def models_module
return self.const_get("Models") if self.constants.map(&:to_s).include?("Models")
expand_prefix = self.expand_prefix
owner = self
models_module = self.const_set("Models", Module.new do
extend ModelsClassMethods
mattr_accessor :expand_prefix
mattr_accessor :expand_owner
self.expand_prefix = expand_prefix
self.expand_owner = owner
end)
return models_module
end
Expand All @@ -135,7 +145,7 @@ def scope(scope, &block)
begin
scope_klass = scope_klass_string.constantize
rescue NameError => e
Chanko::ExceptionNotifier.notify("scope '#{scope_klass_string}' is unable to constantize", false,
Chanko::ExceptionNotifier.notify("scope '#{scope_klass_string}' is unable to constantize", @propagates_errors,
:key => "#{self.name} expand error", :exception => e,
:backtrace => e.backtrace[0..20])
end
Expand Down Expand Up @@ -292,7 +302,7 @@ def functions(context, label, active_if_options={}, options = {})

return cbks.dup unless cbks.blank?

Chanko::ExceptionNotifier.notify("missing functions #{self.name}##{label}", false,
Chanko::ExceptionNotifier.notify("missing functions #{self.name}##{label}", @propagates_errors,
:exception_klass => Chanko::Exception::MissingFunction,
:key => "missing function #{self.name}",
:context => self
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/active_if_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
Chanko::ActiveIf.fetch(:missing).call.should == false
end

context 'with option :raise' do
it 'raises error when tried to fetch missing stuff' do
expect { Chanko::ActiveIf.fetch(:missing, true) }.should raise_error(Chanko::Exception::MissingActiveIfDefinition)
expect { Chanko::ActiveIf.fetch(:return_true, true) }.should_not raise_error(Chanko::Exception::MissingActiveIfDefinition)
end

it 'raises error when missing stuff included' do
expect { Chanko::ActiveIf.new(:missing, :raise => true) }.should raise_error(Chanko::Exception::MissingActiveIfDefinition)
expect { Chanko::ActiveIf.new(:return_true, :raise => true) }.should_not raise_error(Chanko::Exception::MissingActiveIfDefinition)
end
end

it 'should false if one of definitions is false' do
Chanko::ActiveIf.new(:return_true, :return_false).enabled?(self).should == false
Chanko::ActiveIf.new(:return_false, :return_true).enabled?(self).should == false
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ class ExceptionToNotPassThroughed < Exception; end
after(:all) { Chanko.config.propagated_errors = @config_save }
end

describe 'with unit.propagates_errors' do
before do
no_raise_chanko_exception
mock_unit("RaiseErrorTest")
end

it "doesn't raise the exception when propagates_errors == false" do
RaiseErrorTest.propagates_errors = false
function = Chanko::Function.new(:hello, RaiseErrorTest) do
raise Exception
end

expect { function.invoke!(controller) }.to_not raise_error
end

it "raises the exception if propagates_errors == true" do
RaiseErrorTest.propagates_errors = true
function = Chanko::Function.new(:hello, RaiseErrorTest) do
raise Exception
end

expect { function.invoke!(controller) }.to raise_error
end
end


context 'controller' do
before do
mock_unit("RenderTest")
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/method_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ def hello; 'hello'; end
}.to_not raise_error(StandardError, 'error')
end

it 'raise error when unit.propagates_errors is true' do
no_raise_chanko_exception
begin
ProxyTest.propagates_errors = true
expect {
receiver.unit(:proxy_test) do |unit|
raise StandardError, 'error'
end
}.to raise_error(StandardError, 'error')
ensure
ProxyTest.propagates_errors = false
end
end


end

context 'null proxy' do
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,25 @@
before { Chanko.config.cache_classes = false }
it_should_behave_like 'unit'
end

describe 'raise_error' do
before do
no_raise_chanko_exception
mock_unit("RaiseErrorTest", Chanko::Test::Invoker)
RaiseErrorTest.propagates_errors = true
end

it 'raises missingfunction' do
expect { RaiseErrorTest.functions(self, :missing) }.to raise_error(Chanko::Exception::MissingFunction)
end

it 'raises missingactiveifdefinition' do
expect { RaiseErrorTest.active_if(:missing) }.to raise_error(Chanko::Exception::MissingActiveIfDefinition)
end

it 'raises nameerror' do
expect { RaiseErrorTest.scope("MissingScopeName") }.to raise_error(NameError)
expect { RaiseErrorTest.models { expand("MissingScopeName") {} } }.to raise_error(NameError)
end
end
end

0 comments on commit 2255bd9

Please sign in to comment.