Class: Rhales::JsonAwareContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rhales/hydration_data_aggregator.rb

Overview

Context wrapper that automatically converts Ruby objects to JSON in data sections

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ JsonAwareContext

Returns a new instance of JsonAwareContext.



185
186
187
# File 'lib/rhales/hydration_data_aggregator.rb', line 185

def initialize(context)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Delegate all methods to the wrapped context



190
191
192
# File 'lib/rhales/hydration_data_aggregator.rb', line 190

def method_missing(method, *, &)
  @context.send(method, *, &)
end

Instance Method Details

#get(variable_path) ⇒ Object Also known as: resolve_variable

Override get method to return JSON-serialized objects



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rhales/hydration_data_aggregator.rb', line 199

def get(variable_path)
  value = @context.get(variable_path)

  # Convert Ruby objects to JSON for data sections
  case value
  when Hash, Array
    begin
      value.to_json
    rescue JSON::GeneratorError, SystemStackError => ex
      # Handle serialization errors (circular references, unsupported types, etc.)
      raise JSONSerializationError,
        "Failed to serialize Ruby object to JSON: #{ex.message}. " \
        "Object type: #{value.class}, var path: #{variable_path}..."
    end
  else
    value
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/rhales/hydration_data_aggregator.rb', line 194

def respond_to_missing?(method, include_private = false)
  @context.respond_to?(method, include_private)
end