Module: PgAnyWhere

Defined in:
lib/pg_any_where.rb,
lib/pg_any_where/railtie.rb,
lib/pg_any_where/version.rb,
lib/pg_any_where/configuration.rb

Overview

PgAnyWhere rewrites ActiveRecord array-where conditions to use PostgreSQL’s ANY / ALL operators instead of IN / NOT IN.

Quick start (non-Rails)

require "pg_any_where"
PgAnyWhere.patch!

Configuration

PgAnyWhere.configure do |config|
  config.enabled        = true   # or set ENV["PG_ANY_WHERE_ENABLED"]
  config.min_array_size = 2      # or set ENV["PG_ANY_WHERE_MIN_ARRAY_SIZE"]
end

Rails

No manual setup needed — the Railtie handles everything automatically.

Defined Under Namespace

Classes: Configuration, Error, Railtie

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.configurationPgAnyWhere::Configuration

Returns the global Configuration instance.



34
35
36
# File 'lib/pg_any_where.rb', line 34

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields the global Configuration for block-style setup.

Yield Parameters:



41
42
43
# File 'lib/pg_any_where.rb', line 41

def configure
  yield configuration
end

.enabled?Boolean

Shorthand for PgAnyWhere.configuration.enabled?.

Returns:

  • (Boolean)


72
73
74
# File 'lib/pg_any_where.rb', line 72

def enabled?
  configuration.enabled?
end

.patch!Object

Patches Arel::Visitors::PostgreSQL with the ANY / ALL rewrite logic.

Idempotent — safe to call multiple times.

Raises:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pg_any_where.rb', line 50

def patch!
  return if @patched

  unless defined?(Arel::Visitors::PostgreSQL)
    raise Error, "Arel::Visitors::PostgreSQL is not defined. " \
                 "Ensure activerecord-postgresql-adapter is loaded before calling PgAnyWhere.patch!"
  end

  Arel::Visitors::PostgreSQL.prepend(Arel::Visitors::PgAnyWhereVisitor)
  @patched = true
end

.patched?Boolean

Returns true after patch! has been called successfully.

Returns:

  • (Boolean)


65
66
67
# File 'lib/pg_any_where.rb', line 65

def patched?
  @patched == true
end

.reset!void

This method returns an undefined value.

Resets all configuration and the patched flag. Primarily used in tests.



80
81
82
83
84
85
# File 'lib/pg_any_where.rb', line 80

def reset!
  @configuration = nil
  # NOTE: we cannot un-prepend a module, so @patched is intentionally NOT
  # reset here — calling patch! again would attempt a second prepend.
  self
end