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, options = {}) ⇒ Object

Returns a new Schema object.

Parameters:

  • name (Symbol)

    The name of the schema

  • options (Hash) (defaults to: {})

    Some options:

    • root: If the schema should have a root { schema: } around the object. If this attribute is a symbol or string, that will be the schema root to use.


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

def initialize(name, options = {})
  self.name = name
  self.attributes = {}
  self.root = options[:root]
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



17
18
19
# File 'lib/restspec/schema/schema.rb', line 17

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.



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

def original_schema
  @original_schema
end

#rootObject

The root raw value



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

def root
  @root
end

Instance Method Details

#attributes_for_intentionObject



39
40
41
42
43
44
45
# File 'lib/restspec/schema/schema.rb', line 39

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.



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

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

#root?true, false

Returns if the schema must include a root.

Returns:

  • (true, false)

    if the schema must include a root.



49
50
51
# File 'lib/restspec/schema/schema.rb', line 49

def root?
  !!root
end

#root_nameObject



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

def root_name
  root == true ? name.to_sym : root.to_sym
end