Sha256: c97f8a40ad52abd0e89b3ad8096ad0dba08cb15f1104956308b14b1c3a744add

Contents?: true

Size: 1.03 KB

Versions: 11

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Content
    module Types
      # Data serialization/deserialization for Katalyst::Content structural data
      class NodesType < ActiveRecord::Type::Json
        def serialize(value)
          super(value.as_json)
        end

        def deserialize(value)
          case value
          when nil
            nil
          when String
            deserialize(super)
          when Hash
            deserialize_params(value)
          when Array
            deserialize_array(value)
          end
        end

        private

        # Deserialize a params-style array, e.g. "0" => { ... }
        def deserialize_params(value)
          value.map do |index, attributes|
            Node.new(index: index, **attributes)
          end.select(&:id).sort_by(&:index)
        end

        def deserialize_array(value)
          value.map.with_index do |attributes, index|
            Node.new(index: index, **attributes)
          end.select(&:id).sort_by(&:index)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
katalyst-content-1.1.1 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-1.1.0 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-1.0.2 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-1.0.1 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-1.0.0 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-0.2.2 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-0.2.1 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-0.2.0 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-0.1.2 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-0.1.1 app/models/katalyst/content/types/nodes_type.rb
katalyst-content-0.1.0 app/models/katalyst/content/types/nodes_type.rb