Sha256: 1bfe6c6b26bcaf4b86f70565e76e6c06b2936b9280ef02963302ce6a53275429

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'dry/struct'

module Telegram
  module Bot
    module Types
      include Dry.Types()

      # Collection of default types. They are also available without
      # `Types::` prefix in nested classes in opposite to other built-in types.
      Boolean = self::Bool
      Integer = Coercible::Integer
      Float = Coercible::Float
      String = Coercible::String
      Array = self::Array

      class Base < Dry::Struct
        transform_keys(&:to_sym)

        class << self
          # Make all attributes optional.
          # Copied from #attribute?
          def attribute(name, *args, &block)
            super(:"#{name}?", build_type(name, *args, &block))
          end
        end

        # Access fields by string values for backward compatibility.
        # Don't raise errors when accessing missing attributes.
        # https://github.com/dry-rb/dry-struct/pull/177
        def [](name)
          name = name.to_sym
          super(name)
        rescue MissingAttributeError
          raise unless self.class.attribute_names.include?(name)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
telegram-bot-types-0.7.0 lib/telegram/bot/types/base.rb