lib/sqlite_ext.rb in sqlite_ext-1.1.0 vs lib/sqlite_ext.rb in sqlite_ext-1.2.0

- old
+ new

@@ -78,18 +78,35 @@ # array, and there is no way to return an array from a SQL # function in SQLite. # # `NULL`s are propagated as described in the documentation # for `register_function`. + # + # Note that calling `register_ruby_math` more than once + # without calling `purge_function_registrations` in between + # has no effect. Ruby math functions remain registered and + # are not re-registered in that case. def register_ruby_math + return if ruby_math_is_registered + register_ruby_math! + end + + # Registers or re-registers Ruby math. You may want to call + # this method if one or more of the functions defined by a + # previous call to `register_ruby_math` or + # `register_ruby_math!` may have been subsequently replaced. + # + # See `register_ruby_math`. + def register_ruby_math! fn_methods = Math.public_methods - (Module.instance_methods << :frexp) fn_methods.each do |m| register_function m, Math.method(m) end [:floor, :ceil].each do |m| register_function m, m.to_proc end + self.ruby_math_is_registered = true end # Registers a #create_function call to be invoked on every # new instance of `SQLite3::Database` immidately after it is # instantiated and before it is returned from the call to @@ -135,10 +152,11 @@ # Removes all function registrations. Has no effect on # existing instances of `SQLite3::Database`. def purge_function_registrations registered_function_creations.clear + self.ruby_math_is_registered = false end # Creates all of the registered functions on an instance of # `SQLite3::Database`. # @@ -152,10 +170,13 @@ end end private + attr_accessor :ruby_math_is_registered + def registered_function_creations @registered_function_creations ||= {} end + end end