Class: Restspec::Schema::SchemaExample

Inherits:
Object
  • Object
show all
Defined in:
lib/restspec/schema/schema_example.rb

Overview

A value object that generates a example from a schema using an optional set of extensions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, extensions = {}) ⇒ SchemaExample

Returns a new instance of SchemaExample.

Parameters:

  • schema (Restspec::Schema::Schema)

    the schema used to generate the example.

  • extensions (Hash) (defaults to: {})

    A set of extensions to merge with the example.



10
11
12
13
# File 'lib/restspec/schema/schema_example.rb', line 10

def initialize(schema, extensions = {})
  self.schema = schema
  self.extensions = extensions
end

Instance Attribute Details

#extensionsObject

Returns the value of attribute extensions.



6
7
8
# File 'lib/restspec/schema/schema_example.rb', line 6

def extensions
  @extensions
end

#schemaObject

Returns the value of attribute schema.



5
6
7
# File 'lib/restspec/schema/schema_example.rb', line 5

def schema
  @schema
end

Instance Method Details

#valueRestspec::Values::SuperHash

It returns the generated example.

Returns:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/restspec/schema/schema_example.rb', line 17

def value
  example_attributes = attributes.inject({}) do |sample, (_, attribute)|
    sample.merge(attribute.name => AttributeExample.new(attribute).value)
  end.merge(extensions)

  if schema.root?
    wrap_in_root(example_attributes)
  else
    example_attributes
  end
end