Sha256: 6fe2f6592e2ac1c6b9572d02090408535fb3b6d762aeffe1f705695227686ccb

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

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_for_comparison
      @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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
scrivito_sdk-0.65.1 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.65.0 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.65.0.rc2 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.65.0.rc1 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.60.0 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.60.0.rc2 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.60.0.rc1 lib/scrivito/obj_data_from_service.rb