Class: Mongory::Utils::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/mongory/utils/context.rb

Overview

Context is a utility class that provides a stable but mutatable shared context for the Mongory query builder. It holds the configuration and the current record being matcher tree processed.

Examples:

context = Mongory::Utils::Context.new(config)
context.current_record = record
context.config = new_config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Context

Initializes a new Context instance with the given configuration.

Parameters:

  • config (Config) (defaults to: {})

    The configuration object for the context.



24
25
26
27
28
# File 'lib/mongory/utils/context.rb', line 24

def initialize(config = {})
  @config = config
  @current_record = nil
  @need_convert = true
end

Instance Attribute Details

#configConfig

The configuration object for the context

Returns:

  • (Config)

    the current value of config



17
18
19
# File 'lib/mongory/utils/context.rb', line 17

def config
  @config
end

#current_recordRecord

The current record being processed in the matcher tree

Returns:

  • (Record)

    the current value of current_record



17
18
19
# File 'lib/mongory/utils/context.rb', line 17

def current_record
  @current_record
end

#need_convertBoolean

Whether the record needs to be converted before matching

Returns:

  • (Boolean)

    the current value of need_convert



17
18
19
# File 'lib/mongory/utils/context.rb', line 17

def need_convert
  @need_convert
end

Instance Method Details

#dupContext

Note:

The new context shares the same configuration object but has its own state

Creates a duplicate of the context with its own configuration.

Returns:

  • (Context)

    A new context instance with duplicated configuration



34
35
36
37
38
# File 'lib/mongory/utils/context.rb', line 34

def dup
  new_context = super
  new_context.config = @config.dup
  new_context
end