From 6e0a12c06ad3eb315a4f1599364b4ac3fa2384a0 Mon Sep 17 00:00:00 2001 From: Denis Defreyne Date: Sat, 16 Apr 2016 21:27:32 +0200 Subject: [PATCH] Add Filter.define --- lib/nanoc/base/compilation/filter.rb | 7 +++++++ spec/nanoc/base/filter_spec.rb | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 spec/nanoc/base/filter_spec.rb diff --git a/lib/nanoc/base/compilation/filter.rb b/lib/nanoc/base/compilation/filter.rb index c42ef0d08a..cbbc9c17f9 100644 --- a/lib/nanoc/base/compilation/filter.rb +++ b/lib/nanoc/base/compilation/filter.rb @@ -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 diff --git a/spec/nanoc/base/filter_spec.rb b/spec/nanoc/base/filter_spec.rb new file mode 100644 index 0000000000..824c77b669 --- /dev/null +++ b/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