Class: Restspec::Schema::Schema

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

Overview

A schema is a collection of attributes that defines how the data passed through the API should be formed. In REST, they are the representation of the resources the REST API returns.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Object

Returns a new Schema object.

Parameters:

  • name (Symbol)

    The name of the schema


19
20
21
22
# File 'lib/restspec/schema/schema.rb', line 19

def initialize(name)
  self.name = name
  self.attributes = {}
end

Instance Attribute Details

#attributesObject

The set of attributes that conforms the schema.


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

def attributes
  @attributes
end

#intentionObject

TODO: Document


14
15
16
# File 'lib/restspec/schema/schema.rb', line 14

def intention
  @intention
end

#nameObject

The schema identifier.


8
9
10
# File 'lib/restspec/schema/schema.rb', line 8

def name
  @name
end

#original_schemaObject

Returns the value of attribute original_schema


15
16
17
# File 'lib/restspec/schema/schema.rb', line 15

def original_schema
  @original_schema
end

Instance Method Details

#attributes_for_intentionObject


32
33
34
35
36
37
38
# File 'lib/restspec/schema/schema.rb', line 32

def attributes_for_intention
  return attributes if intention.blank?

  attributes.inject({}) do |hash, (name, attribute)|
    attribute.can?(intention) ? hash.merge(name => attribute) : hash
  end
end

#extend_with(without: []) ⇒ Object

Parameters:

  • without (Array) (defaults to: [])

    An array of attributes that should be removed from the schema. This shouldn't be used without cloning first, to avoid modifying a schema used elsewhere.


27
28
29
30
# File 'lib/restspec/schema/schema.rb', line 27

def extend_with(without: [])
  without.each { |attribute_name| attributes.delete(attribute_name.to_s) }
  self
end