Sha256: 52a0aea1edb2b1c429cece70151afc62ed90d4cce16a583efec9c032aa4e548a

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require 'rspec/rails/api/entity_config'
require 'rspec/rails/api/utils'

module RSpec
  module Rails
    module Api
      # Represents an entity field configuration.
      # A field have some options and a method to serialize itself.
      class FieldConfig
        attr_accessor :required, :type, :attributes, :description

        def initialize(type:, description:, required: true, attributes: nil, of: nil)
          @required    = required
          @description = description
          raise "Field type not allowed: '#{type}'" unless Utils.check_attribute_type(type)

          define_attributes attributes if type == :object
          define_attributes of if type == :array

          @type = type
        end

        def to_h
          out               = { required: @required, type: @type }
          out[:description] = @description unless @description.nil?

          if %i[object array].include?(@type) && @attributes
            out[:attributes] = if @attributes.is_a? EntityConfig
                                 @attributes.to_h
                               elsif attributes.is_a? Symbol
                                 @attributes
                               end
          end

          out
        end

        private

        def define_attributes(attributes)
          @attributes = case attributes
                        when Hash
                          @attributes = EntityConfig.new attributes
                        when Symbol
                          attributes
                        end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-rails-api-0.3.4 lib/rspec/rails/api/field_config.rb
rspec-rails-api-0.3.3 lib/rspec/rails/api/field_config.rb
rspec-rails-api-0.3.2 lib/rspec/rails/api/field_config.rb