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.29.1 lib/decidim/attributes/hash.rb
decidim-core-0.28.4 lib/decidim/attributes/hash.rb
decidim-core-0.27.9 lib/decidim/attributes/hash.rb
decidim-core-0.29.0 lib/decidim/attributes/hash.rb
decidim-core-0.28.3 lib/decidim/attributes/hash.rb
decidim-core-0.27.8 lib/decidim/attributes/hash.rb
decidim-core-0.29.0.rc4 lib/decidim/attributes/hash.rb
decidim-core-0.29.0.rc3 lib/decidim/attributes/hash.rb
decidim-core-0.29.0.rc2 lib/decidim/attributes/hash.rb
decidim-core-0.29.0.rc1 lib/decidim/attributes/hash.rb
decidim-core-0.28.2 lib/decidim/attributes/hash.rb
decidim-core-0.27.7 lib/decidim/attributes/hash.rb
decidim-core-0.28.1 lib/decidim/attributes/hash.rb
decidim-core-0.27.6 lib/decidim/attributes/hash.rb
decidim-core-0.28.0 lib/decidim/attributes/hash.rb
decidim-core-0.27.5 lib/decidim/attributes/hash.rb
decidim-core-0.28.0.rc5 lib/decidim/attributes/hash.rb
decidim-core-0.28.0.rc4 lib/decidim/attributes/hash.rb
decidim-core-0.27.4 lib/decidim/attributes/hash.rb
decidim-core-0.27.3 lib/decidim/attributes/hash.rb