Module: MonkeySupport::CProxy

Included in:
ActiveSupport::Inflector
Defined in:
lib/monkeysupport/c_proxy.rb

Defined Under Namespace

Modules: Util

Instance Method Summary collapse

Instance Method Details

#monkey_c_proxy(ruby_name, c_name, args) ⇒ Object

Generates a proxy-to-C method.

Parameters:

- ruby_name -- the original name of the function to override
- c_name    -- the name of the C function in MonkeySupport::C to use
- args      -- list of arguments to funtion, by type.

args example: [:string, :fixnum, [:string, '-'], [:bool true]]

  • takes a string, a fixnum, a string with default value '-', then a boolean with default value true.

EXAMPLE:

alias_method :__unproxy_demodulize, :demodulize MS_C = MonkeySupport::C unless defined? MS_C def demodulize(arg0) begin MS_C.activesupport_inflector_demodulize(arg0) rescue TypeError __unproxy_demodulize(arg0) end end



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/monkeysupport/c_proxy.rb', line 26

def monkey_c_proxy(ruby_name, c_name, args)
  arglist_with_defaults    = Util::arglist(args, true)
  arglist_without_defaults = Util::arglist(args, false)

  # Note: About MS_C: That little extra lookup to MonkeySupport::C
  # can be a near-15% performance hit on some functions. Brutal.
  function = <<-EOS
    alias_method :__unproxy_#{ruby_name}, :#{ruby_name}
    MS_C = MonkeySupport::C unless defined? MS_C
    def #{ruby_name}(#{arglist_with_defaults})
      begin
        MS_C.#{c_name}(#{arglist_without_defaults})
      rescue TypeError
        __unproxy_#{ruby_name}(#{arglist_without_defaults})
      end
    end
  EOS

  class_eval(function)
end