class Restspec::Schema::Types::EmbeddedSchemaType

An embedded schema is an attribute with all the options of a given schema.

Attributes

schema_name[RW]

Public Class Methods

new(schema_name, options = {}) click to toggle source
Calls superclass method Restspec::Schema::Types::BasicType::new
# File lib/restspec/schema/types/embedded_schema_type.rb, line 7
def initialize(schema_name, options = {})
  self.schema_name = schema_name
  super(options)
end

Public Instance Methods

example_for(attribute) click to toggle source

Generates examples by creating a {SchemaExample} using the passed schema.

@param attribute [Restspec::Schema::Attribute] the atribute of the schema to use.

# File lib/restspec/schema/types/embedded_schema_type.rb, line 16
def example_for(attribute)
  Restspec::Schema::SchemaExample.new(schema).value
end
valid?(attribute, value) click to toggle source

Checks if the value is an embedded valid example of the schema used to initialize.

@param attribute [Restspec::Schema::Attribute] the atribute of the schema. @param value [Object] the value of the attribute.

@example

# Let's assume this schema
schema :category do
  attribute :name, string
end

# We can do this anywhere
schema :product do
  attribute :name, string
  attribute :category, embedded_schema(:category)
end

{ name: 'Something', category: { name: 'Products' }  } # a valid product!
{ name: 'Something' } # an invalid product!

@return [true, false] If the value is an embedded valid example of the schema.

# File lib/restspec/schema/types/embedded_schema_type.rb, line 42
def valid?(attribute, value)
  begin
    Restspec::Schema::Checker.new(schema).check!(value)
    true
  rescue  Restspec::Schema::Checker::NoAttributeError,
          Restspec::Schema::Checker::InvalidationError,
          Restspec::Schema::Checker::NoObjectError
    false
  end
end

Private Instance Methods

schema() click to toggle source
# File lib/restspec/schema/types/embedded_schema_type.rb, line 55
def schema
  @schema ||= Restspec::SchemaStore.get(schema_name)
end