Class: Acfs::Resource::Attributes::Dict

Inherits:
Base
  • Object
show all
Defined in:
lib/acfs/resource/attributes/dict.rb

Overview

Dict attribute type. Use it in your model as an attribute type:

Examples:

class User
  include Acfs::Model
  attribute :opts, :dict
end

Instance Attribute Summary

Attributes inherited from Base

#default

Instance Method Summary collapse

Methods inherited from Base

#cast, #default_value, #initialize

Constructor Details

This class inherits a constructor from Acfs::Resource::Attributes::Base

Instance Method Details

#cast_value(value) ⇒ Hash

Cast given object to a dict/hash.

Parameters:

  • value (Object)

    Object to cast.

Returns:

  • (Hash)

    Casted object as hash.

Raises:

  • (TypeError)

    If object cannot be casted to a hash.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/acfs/resource/attributes/dict.rb', line 23

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