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.


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


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

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

#can_be_checked?true, false


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.


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

def example
  @example ||= example_override
end