| Class | PLRuby::Description::Singleton_method |
| In: |
plruby.rb
|
| Parent: | Object |
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=#
plruby_test=# select * from plruby_singleton_methods;
name | args | body
------+------+------
*** | |
(1 row)
plruby_test=# create function add_value(int, int) returns int as '
plruby_test'# args[0] + args[1]
plruby_test'# ' language 'plruby';
CREATE FUNCTION
plruby_test=#
plruby_test=# select add_value(10, 2);
add_value
-----------
12
(1 row)
plruby_test=#
plruby_test=# create function add_one(int) returns int as '
plruby_test'# add_value(args[0], 1)
plruby_test'# ' language 'plruby';
CREATE FUNCTION
plruby_test=#
plruby_test=# select add_one(11);
add_one
---------
12
(1 row)
plruby_test=#