Sha256: 05d1d7ac8c0ec2437dacaeabbee02555ac0e3b4dbadb03e7a3ce071151aff192

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

module Scrivito
module Backend

class ObjDataFromRest < 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 attribute_data = @data[attribute_name]
      if attribute_name.starts_with?('_')
        [attribute_data, nil]
      else
        type = attribute_data[0]
        [legacy_compatible(attribute_data[1], type), type]
      end
    else
      [nil, nil]
    end
  end

  def attribute_names_for_comparison
    @data.keys
  end

  def to_h
    @data
  end

  private

  def legacy_compatible(value, type)
    if type == "linklist"
      value.map(&method(:legacy_link))
    elsif type == "link"
      legacy_link(value)
    else
      value
    end
  end

  def legacy_link(link)
    return link unless link["obj_id"]

    transformed = link.dup
    transformed["destination"] = transformed.delete("obj_id")

    transformed
  end

  def value_of_widget_pool
    if widget_pool = @data['_widget_pool']
      # using Hash#merge to "map" to a new Hash
      widget_pool.merge(widget_pool) do |id, raw_widget_data|
        self.class.new(raw_widget_data)
      end
    end
  end
end

end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
scrivito_sdk-0.65.1 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.65.0 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.65.0.rc2 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.65.0.rc1 lib/scrivito/backend/obj_data_from_rest.rb