Class: RSpec::Rails::Api::FieldConfig
- Inherits:
-
Object
- Object
- RSpec::Rails::Api::FieldConfig
- 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
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#description ⇒ Object
Returns the value of attribute description.
-
#required ⇒ Object
Returns the value of attribute required.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(type:, description:, required: true, attributes: nil, of: nil) ⇒ FieldConfig
constructor
rubocop:disable Metrics/CyclomaticComplexity.
-
#to_h ⇒ Hash
Field configuration.
Constructor Details
#initialize(type:, description:, required: true, attributes: nil, of: nil) ⇒ FieldConfig
rubocop:disable Metrics/CyclomaticComplexity
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rspec/rails/api/field_config.rb', line 14 def initialize(type:, description:, required: true, attributes: nil, of: nil) # rubocop:disable Metrics/CyclomaticComplexity @required = required @description = description raise "Field type not allowed: '#{type}'" unless Validator.valid_type?(type) raise "Don't use 'of' on non-arrays" if of && type != :array raise "Don't use 'attributes' on non-objects" if attributes && type != :object define_attributes attributes if type == :object define_attributes of if type == :array @type = type end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
12 13 14 |
# File 'lib/rspec/rails/api/field_config.rb', line 12 def attributes @attributes end |
#description ⇒ Object
Returns the value of attribute description.
12 13 14 |
# File 'lib/rspec/rails/api/field_config.rb', line 12 def description @description end |
#required ⇒ Object
Returns the value of attribute required.
12 13 14 |
# File 'lib/rspec/rails/api/field_config.rb', line 12 def required @required end |
#type ⇒ Object
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_h ⇒ Hash
Returns Field configuration.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rspec/rails/api/field_config.rb', line 30 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 |