Sha256: 40f04b6812524e89c0c3273a79a5db2b3cf9802d94961412e5ae18b21a7b0483

Contents?: true

Size: 650 Bytes

Versions: 2

Compression:

Stored size: 650 Bytes

Contents

module TD::Types
  class Base < Dry::Struct
    # throw an error when unknown keys provided
    input input.strict
    
    # convert string keys to symbols
    transform_keys(&:to_sym)
    
    # resolve default types on nil
    transform_types do |type|
      if type.default?
        type.constructor do |value|
          value.nil? ? Dry::Types::Undefined : value
        end
      else
        type
      end
    end
    
    def to_hash
      { '@type' => LOOKUP_TABLE.key(self.class.name.sub('TD::Types::', '')) }.merge(super)
    end
    alias_method :to_h, :to_hash
    
    def to_json(*args)
      to_hash.to_json(*args)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tdlib-ruby-2.1.0 lib/tdlib/types/base.rb
tdlib-ruby-2.0.0 lib/tdlib/types/base.rb