Sha256: 71bf9e5d2c122f46b9de42f69d02c685f5729b163657d86728920cb85ec93003

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'set'

module SqliteExt

  module DbTracksCreatedFunctions

    # Adds recording of names of created functions to
    # `create_function`.
    if RUBY_VERSION.split('.').first.to_i >= 2

      def create_function(name, arity, *other_args, &block)
        super
        created_function_names << name_key_from(name)
      end

    else

      def self.included(other)
        orig_create_function = other.instance_method(:create_function)
        other.send :define_method, :create_function, proc{ |name, arity, *other_args, &block|
          orig_create_function.bind(self).call name, arity, *other_args, &block
          created_function_names << name_key_from(name)
        }
      end

    end

    # Given a name, returns true if a function of that hane has
    # been created on the target instance. The name lookup is
    # case-insensitive, and either a string or a symbol may be
    # supplied.
    def function_created?(name)
      name_key = name_key_from(name)
      created_function_names.include?(name_key)
    end

    private

    def name_key_from(name)
      "#{name}".upcase
    end

    def created_function_names
      @created_function_names ||= Set.new
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sqlite_ext-1.5.0 lib/sqlite_ext/db_tracks_created_functions.rb