Sha256: dada7087c2acd744b5c628edc479e1f65637eeac3a5cc3474e2280f3027eff74

Contents?: true

Size: 1.33 KB

Versions: 17

Compression:

Stored size: 1.33 KB

Contents

module Attributor
  module Container
    # Module for types that can contain subtypes. Collection.of(?) or Hash.of(?)
    def self.included(klass)
      klass.module_eval do
        include Attributor::Type
      end
      klass.extend(ClassMethods)
    end

    module ClassMethods

      def decode_string(value, context=Attributor::DEFAULT_ROOT_CONTEXT)
        raise "#{self.name}.decode_string is not implemented. (when decoding #{Attributor.humanize_context(context)})"
      end

      # Decode JSON string that encapsulates an array
      #
      # @param value [String] JSON string
      # @return [Array] a normal Ruby Array
      #
      def decode_json(value, context=Attributor::DEFAULT_ROOT_CONTEXT)
        raise Attributor::DeserializationError, context: context, from: value.class, encoding: "JSON" , value: value unless value.kind_of? ::String

        # attempt to parse as JSON
        parsed_value = JSON.parse(value)

        if self.valid_type?(parsed_value)
          value = parsed_value
        else
          raise Attributor::CoercionError, context: context, from: parsed_value.class, to: self.name, value: parsed_value
        end
        return value

      rescue JSON::JSONError => e
        raise Attributor::DeserializationError, context: context, from: value.class, encoding: "JSON" , value: value
      end

    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
attributor-5.0.2 lib/attributor/types/container.rb
attributor-5.0.1 lib/attributor/types/container.rb
attributor-5.0 lib/attributor/types/container.rb
attributor-4.2.0 lib/attributor/types/container.rb
attributor-4.1.0 lib/attributor/types/container.rb
attributor-4.0.1 lib/attributor/types/container.rb
attributor-4.0.0 lib/attributor/types/container.rb
attributor-3.0.1 lib/attributor/types/container.rb
attributor-3.0 lib/attributor/types/container.rb
attributor-2.6.1 lib/attributor/types/container.rb
attributor-2.6.0 lib/attributor/types/container.rb
attributor-2.5.0 lib/attributor/types/container.rb
attributor-2.4.0 lib/attributor/types/container.rb
attributor-2.3.0 lib/attributor/types/container.rb
attributor-2.2.1 lib/attributor/types/container.rb
attributor-2.2.0 lib/attributor/types/container.rb
attributor-2.1.0 lib/attributor/types/container.rb