Sha256: 6e084c4ccced72206fc840bd32db9548a7b933536677af4a8f8daec62781b31f

Contents?: true

Size: 1.78 KB

Versions: 11

Compression:

Stored size: 1.78 KB

Contents

module Scrivito
  class ObjParamsParser < Struct.new(:host, :port, :context)
    class UnknownWidgetAction < ScrivitoError
    end

    def parse(params)
      params = params.deep_dup
      convert_params(params)
      params
    end

    private

    def convert_field_params(params, attribute_definitions)
      params.each_pair do |key, value|
        params[key] = case type = attribute_definitions[key.to_s].try(:type)
          when 'binary'     then parse_binary_params(value)
          when 'html'       then ContentConversion.convert_html_links(value, host, port)
          when 'link'       then ContentConversion.convert_link(value, host, port)
          when 'linklist'   then ContentConversion.convert_linklist_urls(value, host, port)
          when 'widgetlist' then parse_widgetlist_params(value)
          when 'date'       then DateAttribute.deserialize_from_client(value)
          else value
          end
      end
    end

    def parse_widgetlist_params(params)
      params.map do |widget_id_or_params|
        if widget_id_or_params.is_a?(Hash)
          action, widget_params = widget_id_or_params.flatten
          case action
          when 'create' then Widget.new(widget_params, context.slice(:scrivito_user))
          when 'copy'
            widget_id = widget_params['widget_id']
            widget = Workspace.find(widget_params['workspace_id'])
              .objs.find(widget_params['obj_id'])
              .widgets[widget_id]
            raise ResourceNotFound, "Could not find Widget with id #{widget_id}" unless widget
            widget.copy
          else raise UnknownWidgetAction
          end
        else
          obj.widgets[widget_id_or_params]
        end
      end
    end

    def parse_binary_params(params)
      UploadedBinary.new(params) if params
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
scrivito_sdk-1.0.0 lib/scrivito/obj_params_parser.rb
scrivito_sdk-1.0.0.rc4 lib/scrivito/obj_params_parser.rb
scrivito_sdk-1.0.0.rc3 lib/scrivito/obj_params_parser.rb
scrivito_sdk-1.0.0.rc2 lib/scrivito/obj_params_parser.rb
scrivito_sdk-1.0.0.rc1 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.90.0 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.90.0.rc3 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.90.0.rc2 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.90.0.rc1 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.70.2 lib/scrivito/obj_params_parser.rb
scrivito_sdk-0.71.2 lib/scrivito/obj_params_parser.rb