Sha256: 67674d92fb2f9d57e160c9bb235e1e82df6d2d7795b1aab2247d4984d0976eb0

Contents?: true

Size: 539 Bytes

Versions: 2

Compression:

Stored size: 539 Bytes

Contents

# frozen_string_literal: true

module Appydave
  module Tools
    module Types
      # Used by the ActiveModel attributes API to cast values to hashes
      class HashType < ActiveModel::Type::Value
        def cast(value)
          case value
          when String
            JSON.parse(value)
          when Hash
            value
          else
            raise ArgumentError, "Cannot cast #{value.class} to Hash"
          end
        end

        def serialize(value)
          value.to_json
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
appydave-tools-0.9.5 lib/appydave/tools/types/hash_type.rb
appydave-tools-0.9.4 lib/appydave/tools/types/hash_type.rb