Sha256: 53466f6893a17bba107dac14bad61a085f79f8848404220e307ce019daa50c9c

Contents?: true

Size: 866 Bytes

Versions: 7

Compression:

Stored size: 866 Bytes

Contents

# frozen_string_literal: true

module Acfs::Resource::Attributes
  # @api public
  #
  # Dict attribute type. Use it in your model as an attribute type:
  #
  # @example
  #   class User
  #     include Acfs::Model
  #     attribute :opts, :dict
  #   end
  #
  class Dict < Base
    # @api public
    #
    # Cast given object to a dict/hash.
    #
    # @param [Object] value Object to cast.
    # @return [Hash] Casted object as hash.
    # @raise [TypeError] If object cannot be casted to a hash.
    #
    def cast_value(value)
      return {} if value.blank?

      if value.is_a?(Hash)
        value
      elsif value.respond_to?(:serializable_hash)
        value.serializable_hash
      elsif value.respond_to?(:to_hash)
        value.to_hash
      elsif value.respond_to?(:to_h)
        value.to_h
      else
        Hash(value)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
acfs-2.0.0 lib/acfs/resource/attributes/dict.rb
acfs-1.7.0 lib/acfs/resource/attributes/dict.rb
acfs-1.6.0 lib/acfs/resource/attributes/dict.rb
acfs-1.5.1 lib/acfs/resource/attributes/dict.rb
acfs-1.5.0 lib/acfs/resource/attributes/dict.rb
acfs-1.4.0 lib/acfs/resource/attributes/dict.rb
acfs-1.3.4 lib/acfs/resource/attributes/dict.rb