34 lines
565 B
Ruby
34 lines
565 B
Ruby
# frozen_string_literal: true
|
|
|
|
class OpenStruct
|
|
def self.deep_load(data)
|
|
if data.is_a?(Array)
|
|
data.map { deep_load(_1) }
|
|
elsif data.is_a?(Hash)
|
|
OpenStruct.new(data.to_h { [_1, deep_load(_2)] })
|
|
else
|
|
data
|
|
end
|
|
end
|
|
|
|
def self.deep_to_hash(v)
|
|
if v.is_a?(Array)
|
|
v.map { OpenStruct.deep_to_hash(_1) }
|
|
elsif v.is_a?(OpenStruct)
|
|
v.to_hash_deep
|
|
else
|
|
v
|
|
end
|
|
end
|
|
|
|
def to_hash_deep
|
|
table.to_h do |k, v|
|
|
[ k, OpenStruct.deep_to_hash(v) ]
|
|
end
|
|
end
|
|
|
|
def to_hash
|
|
table.to_h
|
|
end
|
|
end
|