Sha256: b78e1267442da9d42b0e28667ad610bbb7581efc3a7a6511b3ed77bf42710c45

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Restspec
  module Schema
    # A value object that generates a example from a schema using an optional set of extensions.
    class SchemaExample
      attr_accessor :schema
      attr_accessor :extensions

      # @param schema [Restspec::Schema::Schema] the schema used to generate the example.
      # @param extensions [Hash] A set of extensions to merge with the example.
      def initialize(schema, extensions = {})
        self.schema = schema
        self.extensions = extensions
      end

      # It returns the generated example.
      # @return [Restspec::Values::SuperHash] generated example.
      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

      private

      def attributes
        schema.attributes_for_intention
      end

      def wrap_in_root(hash)
        { schema.root_name => hash }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
restspec-0.3.2 lib/restspec/schema/schema_example.rb
restspec-0.3.1 lib/restspec/schema/schema_example.rb
restspec-0.3.0 lib/restspec/schema/schema_example.rb