Class: Buildr::Filter::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/buildr/core/filter.rb

Overview

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'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Mapper

:nodoc:



249
250
251
# File 'lib/buildr/core/filter.rb', line 249

def initialize(*args, &block) #:nodoc:
  using(*args, &block)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



247
248
249
# File 'lib/buildr/core/filter.rb', line 247

def config
  @config
end

#mapper_typeObject (readonly)

Returns the value of attribute mapper_type.



247
248
249
# File 'lib/buildr/core/filter.rb', line 247

def mapper_type
  @mapper_type
end

Instance Method Details

#transform(content, path = nil) ⇒ Object

Raises:

  • (ArgumentError)


277
278
279
280
281
# File 'lib/buildr/core/filter.rb', line 277

def transform(content, path = nil)
  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) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/buildr/core/filter.rb', line 253

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