Class: RSpec::Rails::Api::EntityConfig

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

Overview

Represents an entity configuration. Basically, entities configuration only have a collection of fields and a method to serialize them for comparison with actual content

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ EntityConfig



14
15
16
17
18
19
# File 'lib/rspec/rails/api/entity_config.rb', line 14

def initialize(fields)
  @fields = {}
  fields.each_pair do |name, definition|
    @fields[name] = FieldConfig.new(**definition)
  end
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



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

def fields
  @fields
end

Instance Method Details

#expand_with(entities) ⇒ Hash

Replaces the arrays ‘of’ and objects ‘attributes’ with the corresponding entities, recursively



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rspec/rails/api/entity_config.rb', line 38

def expand_with(entities)
  hash = to_h
  hash.each_pair do |field, config|
    next unless i[array object].include? config[:type]

    attribute = config[:attributes]
    hash[field][:attributes] = if attribute.is_a? Symbol
                                 expand_attribute attribute, entities
                               elsif @fields[field].attributes
                                 @fields[field].attributes.expand_with entities
                               end
  end
end

#to_hHash



23
24
25
26
27
28
29
# File 'lib/rspec/rails/api/entity_config.rb', line 23

def to_h
  out = {}
  @fields.each_key do |key|
    out[key] = @fields[key].to_h
  end
  out
end