class Lucid::InterfaceRb::RbTransform

A Ruby Transform holds a Regexp and a Proc, and is created by calling Transform in the <tt>support ruby files. See also RbLucid.

Example:

Transform /^(\d+) lucid tests$/ do |lucid_string|
  lucid_string.to_i
end

Public Class Methods

new(rb_language, pattern, proc) click to toggle source
# File lib/lucid/interface_rb/rb_transform.rb, line 20
def initialize(rb_language, pattern, proc)
  raise MissingProc if proc.nil? || proc.arity < 1
  @rb_language, @regexp, @proc = rb_language, Regexp.new(pattern), proc
end

Public Instance Methods

invoke(arg) click to toggle source
# File lib/lucid/interface_rb/rb_transform.rb, line 29
def invoke(arg)
  if matched = match(arg)
    args = matched.captures.empty? ? [arg] : matched.captures
    @rb_language.current_domain.lucid_instance_exec(true, @regexp.inspect, *args, &@proc)
  end
end
match(arg) click to toggle source
# File lib/lucid/interface_rb/rb_transform.rb, line 25
def match(arg)
  arg ? arg.match(@regexp) : nil
end
to_s() click to toggle source
# File lib/lucid/interface_rb/rb_transform.rb, line 36
def to_s
   convert_captures(strip_anchors(@regexp.source))
end

Private Instance Methods

convert_captures(regexp_source) click to toggle source
# File lib/lucid/interface_rb/rb_transform.rb, line 41
def convert_captures(regexp_source)
  regexp_source.gsub(/(\()(?!\?:)/,'(?:')
end
strip_anchors(regexp_source) click to toggle source
# File lib/lucid/interface_rb/rb_transform.rb, line 51
def strip_anchors(regexp_source)
  regexp_source.
    gsub(/(^\^|\$$)/, '')
end
strip_captures(regexp_source) click to toggle source
# File lib/lucid/interface_rb/rb_transform.rb, line 45
def strip_captures(regexp_source)
  regexp_source.
    gsub(/(\()/, '').
    gsub(/(\))/, '')
end