In Files

Parent

PLRuby::Description::Singleton_method

plruby_singleton_methods

Sometime it can be usefull to define methods (in pure Ruby) which can be called from a PLRuby function or a PLRuby trigger.

In this case, you have 2 possibilities

just close the current definition of the function (or trigger) with a end and define your singleton method without the final end

Here a small and useless example

plruby_test=# CREATE FUNCTION tutu() RETURNS int4 AS '
plruby_test'#     toto(1, 3) + toto(4, 4)
plruby_test'# end
plruby_test'# 
plruby_test'# def PLtemp.toto(a, b)
plruby_test'#     a + b
plruby_test'# ' LANGUAGE 'plruby';
CREATE
plruby_test=# select tutu();
tutu
----
  12
(1 row)

plruby_test=#

At load time, PLRuby look if it exist a table plruby_singleton_methods and if found try, for each row, to define singleton methods with the template :

def PLtemp.#{name}(#{args})
    #{body}
end

The previous example can be written (you have a more complete example in test/plp/test_setup.sql)

plruby_test=# SELECT * FROM plruby_singleton_methods;
name|args|body 
----+----+-----
toto|a, b|a + b
(1 row)

plruby_test=# CREATE FUNCTION tutu() RETURNS int4 AS '
plruby_test'#     toto(1, 3) + toto(4, 4)
plruby_test'# ' LANGUAGE 'plruby';
CREATE
plruby_test=# select tutu();
tutu
----
  12
(1 row)

plruby_test=#

[Validate]

Generated with the Darkfish Rdoc Generator 2.