Sha256: 18857c3abc229aae7b1dce603d2d6dafe052d3ccf52d4540575c4cb23e5397bb

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

module Scrivito
  class ObjParamsParser
    def initialize(host, port)
      @host = host
      @port = port
    end

    def parse(obj, orig_params)
      raise "Required parameter 'obj' is missing." unless orig_params.present?
      raise "Parameter 'obj' is not a hash." unless orig_params.is_a?(Hash)

      params = orig_params.dup

      if obj
        widget_field_params = WidgetFieldParams.new

        convert_params(params, obj, widget_field_params)

        if widget_pool_params = params['_widget_pool']
          convert_widget_pool_params(widget_pool_params, obj, widget_field_params)
        end

        additional_widget_pool_params = widget_field_params.pool_params
        if additional_widget_pool_params.any?
          params['_widget_pool'] ||= {}
          params['_widget_pool'].merge!(additional_widget_pool_params)
        end
      end

      params
    end

    private

    def convert_widget_pool_params(widget_pool_params, obj, widget_field_params_parser)
      widget_pool_params.each_pair do |widget_id, widget_params|
        widget = obj.widget_from_pool(widget_id)
        if widget_params.present?
          convert_params(widget_params, widget, widget_field_params_parser)
        end
      end
    end

    def convert_params(params, obj, widget_field_params)
      params.each do |key, value|
        type = obj.type_of_attribute(key.to_s)

        params[key] = case type
        when 'html'
          ContentConversion.convert_html_links(value, @host, @port)
        when 'linklist'
          ContentConversion.convert_linklist_urls(value, @host, @port)
        when 'widget'
          widget_field_params.convert(value)
        else
          value
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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