Class: Restspec::Schema::Attribute

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

Overview

An attribute is a part of a schema. All attributes have a name and a type at least. A type is an instance of a subclass of Types::BasicType that keeps information about what are valid instances of the attribute and can generate valid instances of the attribute.

Examples:


string_type = Types::StringType.new
name_attr = Attribute.new(:name, type)

string_type.example_for(name_attr) # A random word
string_type.valid?(name_attr, 1000) # false
string_type.valid?(name_attr, 'John') # true

With the :example option


string_type = Types::StringType.new
name_attr = Attribute.new(:name, type, example: 'Example!')

string_type.example_for(name_attr) # Example!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Object

Creates an attribute. It uses an identifier (name), an instance of a subclass of Types::BasicType and a set of options.

Parameters:

  • name

    the name of the attribute

  • type

    an instance of a subclass of Types::BasicType that works like the type of this attribute, allowing the type to generate examples and run validations based on this attribute.

  • options (defaults to: {})

    that can be the following:

    • example: A callable object (eg: a lambda) that returns something.
    • for: Defines what abilities this attributes has. This is an array that can contains none, some or all the symbols :response and :payload. This option defaults to [:response, :payload], allowing the attribute to be used for run validations from Checker#check! (:response) and for generating payload from SchemaExample#value (:payload).

43
44
45
46
47
48
# File 'lib/restspec/schema/attribute.rb', line 43

def initialize(name, type, options = {})
  self.name = name
  self.type = type
  self.example_override = options.fetch(:example, nil)
  self.allowed_abilities = options.fetch(:for, [:response, :payload])
end

Instance Attribute Details

#nameObject

Returns the value of attribute name


25
26
27
# File 'lib/restspec/schema/attribute.rb', line 25

def name
  @name
end

#typeObject

Returns the value of attribute type


25
26
27
# File 'lib/restspec/schema/attribute.rb', line 25

def type
  @type
end

Instance Method Details

#can?(ability) ⇒ true, false

Returns if the attribute has the ability being passed

Returns:

  • (true, false)

    if the attribute has the ability being passed


64
65
66
# File 'lib/restspec/schema/attribute.rb', line 64

def can?(ability)
  allowed_abilities.include?(ability)
end

#can_be_checked?true, false

Returns if the attribute has the ability to be used in payloads.

Returns:

  • (true, false)

    if the attribute has the ability to be used in payloads.


59
60
61
# File 'lib/restspec/schema/attribute.rb', line 59

def can_be_checked?
  allowed_abilities.include?(:payload)
end

#exampleObject

The inner example in the attribute created calling the :example option when generating examples.

Returns:

  • The inner example created using the :example option.


54
55
56
# File 'lib/restspec/schema/attribute.rb', line 54

def example
  @example ||= example_override
end