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.



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

def initialize(context)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegate all methods to the wrapped context



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

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

Instance Method Details

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

Override get method to return JSON-serialized objects



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

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)


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

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