Sha256: f131a1d285d78c61cf33f7a4012d9dd1a6b62234ba5a507b851f560248e47b0c

Contents?: true

Size: 1.43 KB

Versions: 12

Compression:

Stored size: 1.43 KB

Contents

module OpenApi
  # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#header-object
  class Header
    prepend EquatableAsContent

    attr_accessor :description, :required, :deprecated, :allow_empty_value

    def initialize(description: nil, required: false, deprecated: nil, allow_empty_value: false, **other_fields_hash)
      self.description = description
      self.required = required
      self.deprecated = deprecated
      self.allow_empty_value = allow_empty_value
      self.other_fields_hash = other_fields_hash.with_indifferent_access

      other_fields_hash.keys.each do |field_name|
        define_singleton_method(field_name) do
          other_fields_hash[field_name]
        end
        define_singleton_method("#{field_name}=") do |value|
          other_fields_hash[field_name] = value
        end
      end
    end

    def self.load(hash)
      fixed_field_names = [:description, :required, :deprecated, :allowEmptyValue]
      other_fields_hash = hash.reject { |key| key.to_sym.in?(fixed_field_names) }

      new(
        description: hash["description"],
        required: hash["required"].nil? ? false : hash["required"],
        deprecated: hash["deprecated"].nil? ? false : hash["deprecated"],
        allow_empty_value: hash["allowEmptyValue"].nil? ? false : hash["allowEmptyValue"],
        **other_fields_hash.symbolize_keys,
      )
    end

    private

    attr_accessor :other_fields_hash
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
open_api-0.6.2 lib/open_api/header.rb
open_api-0.6.1 lib/open_api/header.rb
open_api-0.6.0 lib/open_api/header.rb
open_api-0.5.0 lib/open_api/header.rb
open_api-0.4.0 lib/open_api/header.rb
open_api-0.3.4 lib/open_api/header.rb
open_api-0.3.3 lib/open_api/header.rb
open_api-0.3.2 lib/open_api/header.rb
open_api-0.3.1 lib/open_api/header.rb
open_api-0.3.0 lib/open_api/header.rb
open_api-0.2.0 lib/open_api/header.rb
open_api-0.1.0 lib/open_api/header.rb