Sha256: 3f92583c50557893edbaba6101e6f8aae0c87104ef1edb7d30739d7fdd2bd251

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

module Scrivito

  class ObjDataFromService < ObjData
    def initialize(data)
      @data = data
    end

    def raw_value_and_type_of(attribute_name)
      if attribute_name == '_widget_pool'
        [value_of_widget_pool, nil]
      else
        type_and_value = @data[attribute_name]

        if type_and_value.present?
          type = if type_and_value.length == 1
            type_of_internal(attribute_name)
          else
            type_and_value.last
          end

          [convert_value(type_and_value.first, type), type]
        end
      end
    end

    def has_custom_attribute?(attribute_name)
      is_custom_attribute?(attribute_name) && !!@data[attribute_name]
    end

    def all_attributes
      @all_attributes ||= (@data.keys | INTERNAL_KEYS.to_a | SPECIAL_KEYS.to_a)
    end

    private

    def convert_value(value, type)
      case type
      when "html"
        text_links_conversion.convert(value)
      when "widget"
        if !value
          []
        else
          (value['list'] || []).map do |list_item|
            list_item['widget']
          end
        end
      else
        value
      end
    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
      raw_value_of_widget_pool.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

    def raw_value_of_widget_pool
      @data['_widget_pool'].first
    end

    def is_custom_attribute?(attribute_name)
      !attribute_name.starts_with?('_') && !SPECIAL_KEYS.include?(attribute_name)
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
scrivito_sdk-0.17.0 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.16.0 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.15.0 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.14.0 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.13.0 lib/scrivito/obj_data_from_service.rb
scrivito_sdk-0.12.0 lib/scrivito/obj_data_from_service.rb