Sha256: e840c5d34d3bfaaf7c7d6302c4300e1ae9aa0335aa1b551c9d20bc844467b6be

Contents?: true

Size: 1.22 KB

Versions: 23

Compression:

Stored size: 1.22 KB

Contents

module Scrivito
module Backend

class ObjDataFromRest < ObjData
  def initialize(data)
    raise InternalError unless Hash === 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
    @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

23 entries across 23 versions & 1 rubygems

Version Path
scrivito_sdk-1.0.0 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-1.0.0.rc4 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-1.0.0.rc3 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-1.0.0.rc2 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-1.0.0.rc1 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.90.0 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.90.0.rc3 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.90.0.rc2 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.90.0.rc1 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.70.2 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.71.2 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.71.1 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.70.1 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.71.0 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.71.0.rc2 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.71.0.rc1 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.70.0 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.70.0.rc3 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.70.0.rc2 lib/scrivito/backend/obj_data_from_rest.rb
scrivito_sdk-0.70.0.rc1 lib/scrivito/backend/obj_data_from_rest.rb