Sha256: c29181044c21372b199ef08bfa3cbfc5626d22c8b26a4bd369082856e6157d0f

Contents?: true

Size: 835 Bytes

Versions: 16

Compression:

Stored size: 835 Bytes

Contents

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

16 entries across 16 versions & 1 rubygems

Version Path
acfs-1.3.2 lib/acfs/resource/attributes/dict.rb
acfs-1.3.1 lib/acfs/resource/attributes/dict.rb
acfs-1.3.0 lib/acfs/resource/attributes/dict.rb
acfs-1.2.1 lib/acfs/resource/attributes/dict.rb
acfs-0.50.0 lib/acfs/resource/attributes/dict.rb
acfs-1.2.0 lib/acfs/resource/attributes/dict.rb
acfs-0.49.1 lib/acfs/resource/attributes/dict.rb
acfs-1.1.1 lib/acfs/resource/attributes/dict.rb
acfs-0.49.0 lib/acfs/resource/attributes/dict.rb
acfs-1.1.0 lib/acfs/resource/attributes/dict.rb
acfs-0.48.2 lib/acfs/resource/attributes/dict.rb
acfs-1.0.1 lib/acfs/resource/attributes/dict.rb
acfs-0.48.1 lib/acfs/resource/attributes/dict.rb
acfs-1.0.0 lib/acfs/resource/attributes/dict.rb
acfs-0.48.0 lib/acfs/resource/attributes/dict.rb
acfs-0.47.0 lib/acfs/resource/attributes/dict.rb