Class | Buildr::Filter::Mapper |
In: |
lib/buildr/core/filter.rb
(CVS)
|
Parent: | Object |
This class implements content replacement logic for Filter.
To register a new template engine @:foo@, extend this class with a method like:
def foo_transform(content, path = nil) # if this method yields a key, the value comes from the mapping hash content.gsub(/world/) { |str| yield :bar } end
Then you can use :foo mapping type on a Filter
filter.using :foo, :bar => :baz
Or all by your own, simply
Mapper.new(:foo, :bar => :baz).transform("Hello world") # => "Hello baz"
You can handle configuration arguments by providing a @*_config@ method like:
# The return value of this method is available with the :config accessor. def moo_config(*args, &block) raise ArgumentError, "Expected moo block" unless block_given? { :moos => args, :callback => block } end def moo_transform(content, path = nil) content.gsub(/moo+/i) do |str| moos = yield :moos # same than config[:moos] moo = moos[str.size - 3] || str config[:callback].call(moo) end end
Usage for the @:moo@ mapper would be something like:
mapper = Mapper.new(:moo, 'ooone', 'twoo') do |str| i = nil; str.capitalize.gsub(/\w/) { |s| s.send( (i = !i) ? 'upcase' : 'downcase' ) } end mapper.transform('Moo cow, mooo cows singing mooooo') # => 'OoOnE cow, TwOo cows singing MoOoOo'
config | [R] | |
mapper_type | [R] |