Class: RSpec::Rails::Api::FieldConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rails/api/field_config.rb

Overview

Represents an entity field configuration. A field have some options and a method to serialize itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, required: true, description:, attributes: nil, of: nil) ⇒ FieldConfig

Returns a new instance of FieldConfig.



14
15
16
17
18
19
20
21
22
23
# File 'lib/rspec/rails/api/field_config.rb', line 14

def initialize(type:, required: true, description:, 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



12
13
14
# File 'lib/rspec/rails/api/field_config.rb', line 12

def attributes
  @attributes
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/rspec/rails/api/field_config.rb', line 12

def description
  @description
end

#requiredObject

Returns the value of attribute required.



12
13
14
# File 'lib/rspec/rails/api/field_config.rb', line 12

def required
  @required
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/rspec/rails/api/field_config.rb', line 12

def type
  @type
end

Instance Method Details

#to_hObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rspec/rails/api/field_config.rb', line 25

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