Skip to content

Commit

Permalink
Add Nanoc::Filter.define
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 10, 2016
1 parent ae6df04 commit 04e35d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/nanoc/base/compilation/filter.rb
Expand Up @@ -38,6 +38,13 @@ class Filter < Nanoc::Int::Context
extend Nanoc::Int::PluginRegistry::PluginMethods

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

# Sets the new type for the filter. The type can be `:binary` (default)
# or `:text`. The given argument can either be a symbol indicating both
# “from” and “to” types, or a hash where the only key is the “from” type
Expand Down
17 changes: 17 additions & 0 deletions spec/nanoc/base/filter_spec.rb
@@ -0,0 +1,17 @@
describe Nanoc::Filter do
describe '.define' do
before do
Nanoc::Filter.define(:nanoc_filter_define_sample) do |content, _params|
content.upcase
end
end

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

it 'defines a callable filter' do
expect(Nanoc::Filter.named(:nanoc_filter_define_sample).new.run('foo', {})).to eql('FOO')
end
end
end

0 comments on commit 04e35d0

Please sign in to comment.