Last active
August 29, 2015 14:12
-
-
Save kuboon/6b3084e1b87adb9f8fd3 to your computer and use it in GitHub Desktop.
omit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def omit(val = nil, &block) | |
unless block_given? | |
raise 'no parent block' if @_omit.empty? | |
@_omit[-1] = false unless val.nil? | |
return val | |
end | |
raise "Don't pass a value with block" if val | |
@_omit ||= [] | |
@_omit.push true | |
ret = capture(&block) | |
return nil if @_omit.pop | |
@_omit[-1] = false unless @_omit.empty? | |
ret | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe ApplicationHelper do | |
describe 'omit' do | |
subject { Haml::Engine.new(haml).def_method(self, :render); render } | |
context 'empty block' do | |
let(:haml) { <<HAML | |
= omit do | |
%p | |
HAML | |
} | |
it { is_expected.to eq "\n" } | |
end | |
context 'block with nil' do | |
let(:haml) { <<HAML | |
= omit do | |
%p= omit nil | |
HAML | |
} | |
it { is_expected.to eq "\n" } | |
end | |
context 'block with non-nil value' do | |
let(:haml) { <<HAML | |
= omit do | |
%p= omit 'hoge' | |
HAML | |
} | |
it { is_expected.to eq "<p>hoge</p>\n" } | |
end | |
context 'block with empty block' do | |
let(:haml) { <<HAML | |
= omit do | |
= omit do | |
%span= omit nil | |
%p= omit nil | |
HAML | |
} | |
it { is_expected.to eq "\n" } | |
end | |
context 'block with non-empty block' do | |
let(:haml) { <<HAML | |
= omit do | |
= omit do | |
%span= omit 'hoge' | |
%p= omit nil | |
HAML | |
} | |
it { is_expected.to eq "<span>hoge</span><p></p>\n" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment