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 313 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 317 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 287 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
Private Instance Methods
ant_transform(content, path = nil) { |str| ... }
click to toggle source
# File lib/buildr/core/filter.rb, line 341 def ant_transform(content, path = nil) content.gsub(/@.*?@/) { |str| yield(str[1..-2]) || str } end
callback_transform(content, path = nil)
click to toggle source
# File lib/buildr/core/filter.rb, line 353 def callback_transform(content, path = nil) config.call(path, content) end
configure(mapper_type, *args, &block)
click to toggle source
# File lib/buildr/core/filter.rb, line 325 def configure(mapper_type, *args, &block) configurer = method("#{mapper_type}_config") rescue nil if configurer @config = configurer.call(*args, &block) else raise ArgumentError, "Missing hash argument after :#{mapper_type}" unless args.size == 1 && Hash === args[0] @config = {} unless Hash === @config args.first.each_pair { |k, v| @config[k] = v.to_s } end @mapper_type = mapper_type end
erb_config(*args, &block)
click to toggle source
# File lib/buildr/core/filter.rb, line 372 def erb_config(*args, &block) if block_given? raise ArgumentError, "Expected block or single argument, but both given." unless args.empty? block elsif args.size > 1 raise ArgumentError, "Expected block or single argument." else args.first end end
erb_transform(content, path = nil)
click to toggle source
# File lib/buildr/core/filter.rb, line 357 def erb_transform(content, path = nil) case config when Binding bnd = config when Hash bnd = OpenStruct.new table = config.inject({}) { |h, e| h[e.first.to_sym] = e.last; h } bnd.instance_variable_set(:@table, table) bnd = bnd.instance_eval { binding } else bnd = config.instance_eval { binding } end ERB.new(content).result(bnd) end
maven_transform(content, path = nil) { |str| ... }
click to toggle source
# File lib/buildr/core/filter.rb, line 337 def maven_transform(content, path = nil) content.gsub(/\$\{.*?\}/) { |str| yield(str[2..-2]) || str } end
regexp_transform(content, path = nil) { |scan.join| ... }
click to toggle source
# File lib/buildr/core/filter.rb, line 349 def regexp_transform(content, path = nil) content.gsub(mapper_type) { |str| yield(str.scan(mapper_type).join) || str } end
ruby_transform(content, path = nil) { |str| ... }
click to toggle source
# File lib/buildr/core/filter.rb, line 345 def ruby_transform(content, path = nil) content.gsub(/#\{.*?\}/) { |str| yield(str[2..-2]) || str } end