Sha256: f30365ccbbd189d927d556dfd1b78ced83133f52cbc1e0a7b083c1e3d89327a2

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

require 'active_support/concern'
module Camunda
  # The VariableSerialization module adds information to variables so Camunda can parse them. It adds types annotations and
  # serializes hashes and array to JSON. Camunda engine cannot search on snake_case variables so it changes variable names
  # to camelCase.
  # @see Camunda::ProcessDefinition
  module VariableSerialization
    extend ActiveSupport::Concern
    # Wrapper for class level method
    def serialize_variables(variables)
      self.class.serialize_variables(variables)
    end

    class_methods do
      # rubocop:disable Metrics/MethodLength
      # @param variables [Hash]
      # @return {String,Symbol => {String,Symbol => Object}}
      # @raise [ArgumentError] if a class instance is passed
      def serialize_variables(variables)
        hash = variables.transform_values do |value|
          case value
          when String
            { value: value, type: 'String' }
          when Array, Hash
            { value: transform_json(value).to_json, type: 'Json' }
          when TrueClass, FalseClass
            { value: value, type: 'Boolean' }
          when Integer
            { value: value, type: 'Integer' }
          when Float
            { value: value, type: 'Double' }
          else
            { value: value.to_s, type: 'String' }
          end
        end
        camelcase_keys(hash)
      end
      # rubocop:enable Metrics/MethodLength

      # Transforms keys of a JSON like object (Array,Hash) from snake_case to CamelCase
      # @param json [Array,Hash]
      # @return [Hash] returns hash with camelCase keys
      def transform_json(json)
        case json
        when Array
          json.map { |element| transform_json(element) }
        when Hash
          camelcase_keys(json)
        else
          json
        end
      end

      def camelcase_keys(hash)
        hash.deep_transform_keys { |key| key.to_s.camelcase(:lower) }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
camunda-workflow-1.0.0 lib/camunda/variable_serialization.rb
camunda-workflow-0.6.0 lib/camunda/variable_serialization.rb
camunda-workflow-0.5.0 lib/camunda/variable_serialization.rb
camunda-workflow-0.4.1 lib/camunda/variable_serialization.rb
camunda-workflow-0.4.0 lib/camunda/variable_serialization.rb
camunda-workflow-0.3.3 lib/camunda/variable_serialization.rb