Sha256: 9d48be1b65581c537f54f136b9c56ea3ff8c1f26017bdd749ef35f5f3ad14359

Contents?: true

Size: 981 Bytes

Versions: 25

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

module Decidim
  module Attributes
    # Custom attributes value to represent a Hash.
    class Hash < Decidim::Attributes::Array
      attr_reader :key_type

      def initialize(key_type: ::Symbol, value_type: ::Object, default: {})
        @key_type = key_type
        @value_type = value_type
        @default = default
      end

      def type # :nodoc:
        :hash
      end

      private

      def cast_value(value)
        value ||= default.dup
        value = value.to_h if !value.is_a?(::Hash) && value.respond_to?(:to_h)

        # This preserves backwards compatibility for non-hash values e.g. with
        # the admin hero content block which expects the images container object
        # as the default value when building the admin form.
        return value unless value.is_a?(::Hash)

        value.to_h do |k, v|
          [primitive_value(k, key_type), primitive_value(v, value_type)]
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.27.2 lib/decidim/attributes/hash.rb
decidim-core-0.27.1 lib/decidim/attributes/hash.rb
decidim-core-0.27.0 lib/decidim/attributes/hash.rb
decidim-core-0.27.0.rc2 lib/decidim/attributes/hash.rb
decidim-core-0.27.0.rc1 lib/decidim/attributes/hash.rb