Class: Applitools::FlexibleOpenStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/applitools/core/test_results.rb

Overview

First, extend OpenStruct with a custom method for case-insensitive hash access

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Also handle nested hashes by recursively converting them to FlexibleOpenStruct



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/applitools/core/test_results.rb', line 30

def method_missing(name, *args)
  value = super

  if value.is_a?(Hash)
    send("#{name}=", hash_to_flexible_open_struct(value))
    value = send(name)
  elsif value.is_a?(Array)
    send("#{name}=", array_to_flexible_open_struct(value))
    value = send(name)
  end

  value
end

Instance Method Details

#[](key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/applitools/core/test_results.rb', line 8

def [](key)
  # Convert the key to string in case it's a symbol or number
  key_str = key.to_s

  # Direct access first (most efficient)
  return super(key) if respond_to?(key)

  # If the key contains uppercase letters, try snake_case version
  if key_str =~ /[A-Z]/
    snake_case_key = key_str.gsub(/([A-Z])/, '_\1').downcase.gsub(/^_/, '')
    return send(snake_case_key) if respond_to?(snake_case_key)
  else
    # If the key is snake_case, try camelCase version
    camel_case_key = key_str.gsub(/_([a-z])/) { $1.upcase }
    return send(camel_case_key) if respond_to?(camel_case_key)
  end

  # Fall back to nil if no matching attribute is found
  nil
end