Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assigns in filters defined with Filter.define #934

Merged
merged 1 commit into from Aug 22, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+32 −10
Diff settings

Always

Just for now

Fix assigns in filters defined with Filter.define

  • Loading branch information...
ddfreyne committed Aug 22, 2016
commit 025303781a3a2ef1784e7bbed35101e8a7587665
@@ -38,10 +38,10 @@ class Filter < Nanoc::Int::Context
extend Nanoc::Int::PluginRegistry::PluginMethods

class << self
def define(ident)
def define(ident, &block)
filter_class = Class.new(::Nanoc::Filter) { identifier(ident) }
filter_class.send(:define_method, :run) do |content, params|
yield(content, params)
instance_exec(content, params, &block)
end
end

Copy path View file
@@ -1,17 +1,39 @@
describe Nanoc::Filter do
describe '.define' do
before do
Nanoc::Filter.define(:nanoc_filter_define_sample) do |content, _params|
content.upcase
context 'simple filter' do
let(:filter_name) { 'b5355bbb4d772b9853d21be57da614dba521dbbb' }
let(:filter_class) { Nanoc::Filter.named(filter_name) }

before do
Nanoc::Filter.define(filter_name) do |content, _params|
content.upcase
end
end
end

it 'defines a filter' do
expect(Nanoc::Filter.named(:nanoc_filter_define_sample)).not_to be_nil
it 'defines a filter' do
expect(filter_class).not_to be_nil
end

it 'defines a callable filter' do
expect(filter_class.new.run('foo', {})).to eql('FOO')
end
end

it 'defines a callable filter' do
expect(Nanoc::Filter.named(:nanoc_filter_define_sample).new.run('foo', {})).to eql('FOO')
context 'filter that accesses assigns' do
let(:filter_name) { 'd7ed105d460e99a3d38f46af023d9490c140fdd9' }
let(:filter_class) { Nanoc::Filter.named(filter_name) }
let(:filter) { filter_class.new(assigns) }
let(:assigns) { { animal: 'Giraffe' } }

before do
Nanoc::Filter.define(filter_name) do |_content, _params|
@animal
end
end

it 'can access assigns' do
expect(filter.setup_and_run(:__irrelevant__, {})).to eq('Giraffe')
end
end
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.