class Buildr::Filter::Mapper

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'

Constants

BINARY_FILES

Attributes

config[R]
mapper_type[R]

Public Instance Methods

is_binary?(content, path) click to toggle source
# File lib/buildr/core/filter.rb, line 314
def is_binary?(content, path)
  !!path && BINARY_FILES.any? { |glob| File.fnmatch(glob, path) }
end
transform(content, path = nil) click to toggle source
# File lib/buildr/core/filter.rb, line 318
def transform(content, path = nil)
  return content if is_binary?(content, path)
  type = Regexp === mapper_type ? :regexp : mapper_type
  raise ArgumentError, "Invalid mapper type: #{type.inspect}" unless respond_to?("#{type}_transform", true)
  self.__send__("#{type}_transform", content, path) { |key| config[key] || config[key.to_s.to_sym] }
end
using(*args, &block) click to toggle source
# File lib/buildr/core/filter.rb, line 288
def using(*args, &block)
  case args.first
  when Hash # Maven hash mapping
    using :maven, *args
  when Binding # Erb binding
    using :erb, *args
  when Symbol # Mapping from a method
    raise ArgumentError, "Unknown mapping type: #{args.first}" unless respond_to?("#{args.first}_transform", true)
    configure(*args, &block)
  when Regexp # Mapping using a regular expression
    raise ArgumentError, 'Expected regular expression followed by mapping hash' unless args.size == 2 && Hash === args[1]
    @mapper_type, @config = *args
  else
    unless args.empty? && block.nil?
      raise ArgumentError, 'Expected proc, method or a block' if args.size > 1 || (args.first && block)
      @mapper_type = :callback
      config = args.first || block
      raise ArgumentError, 'Expected proc, method or callable' unless config.respond_to?(:call)
      @config = config
    end
  end
  self
end