Sha256: c03f218a3eb0f10bceaeafb7c98e3430ba563e799490c10670641ec7754da6e8

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 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 'link'
          ContentConversion.convert_link(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.30.0 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.30.0.rc1 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.18.1 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.18.0 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.17.0 lib/scrivito/obj_params_parser.rb