Sha256: fdb773b4e3f02b00c45b5de28941394cb18522e35396d537687895c32d7a9faf

Contents?: true

Size: 652 Bytes

Versions: 3

Compression:

Stored size: 652 Bytes

Contents

module TD::Types
  class Base < Dry::Struct
    # throw an error when unknown keys provided
    schema schema.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

3 entries across 3 versions & 2 rubygems

Version Path
tdlib-schema-1.7.0.2 lib/tdlib/types/base.rb
tdlib-schema-1.7.0.1 lib/tdlib/types/base.rb
tdlib-ruby-2.2.0 lib/tdlib/types/base.rb