module Scrivito class ObjDataFromService < ObjData def initialize(data) @data = data end def raw_value_and_type_of(attribute_name) return [value_of_widget_pool, nil] if attribute_name == '_widget_pool' if value_and_type = @data[attribute_name] value, type = value_and_type value = convert_value(value, type) [value, type] else [nil, nil] end end def convert_value(value, type) case type when 'html' then convert_text_links(value) when 'widget' if value.is_a?(Hash) && value.size == 1 && value.has_key?('list') # Legacy data with obj classes. value['list'].map { |item| item['widget'] } else value end else value end end def attribute_names @data.keys end def to_h @data.to_h end private def convert_text_links(value) text_links_conversion.convert(value) end def text_links_conversion @text_links_conversion = TextLinkConversion.new(text_links_map) end def text_links_map text_links = @data["_text_links"] return nil unless text_links text_links.first.each_with_object({}) do |link, map| map[link["link_id"]] = TextLink.new(link) end end def value_of_widget_pool if widget_pool = @data['_widget_pool'] widget_pool.first.dup.tap do |hash| hash.each_pair do |widget_id, raw_widget_data| hash[widget_id] = self.class.new(raw_widget_data) end end end end end end