class Cannonbol::ParameterizedPattern

Public Class Methods

new(opts = nil, &block) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 311
def initialize(opts = nil, &block)
  if opts.class == Hash
    if opts.first
      @param_name = opts.first.first
      @initial_param_value = opts.first.last
    end
  else
    @initial_param_value = opts
  end
  @block = block
end
parameter(name, &post_processor) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 323
def self.parameter(name, &post_processor)
  @post_processor = post_processor
  define_method(name) do |needle|
    value = (@param_name && needle.captures.has_key?(@param_name)) ? needle.captures[@param_name] : @initial_param_value
    value = @block.call(value) if @block
    needle.capture(@param_name, value)
    value = post_processor.call(value) if @post_processor
    value
  end
end