Class: Onsi::Model::ModelRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/onsi/model.rb

Overview

Note:

You shouldn’t ever have to directly interact with one of these classes.

The class that holds attributes and relationships for a model’s version.

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Constant Summary collapse

DATE_FORMAT =

The default date format for a rendered Date. (ISO-8601)

Since:

  • 1.0.0

'%Y-%m-%d'.freeze
DATETIME_FORMAT =

The default date-time format for a rendered Date and Time. (ISO-8601)

Since:

  • 1.0.0

'%Y-%m-%dT%H:%M:%SZ'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#id_attrObject (readonly)

The name of the id attribute on the model

Since:

  • 1.0.0



96
97
98
# File 'lib/onsi/model.rb', line 96

def id_attr
  @id_attr
end

Instance Method Details

#attribute(name, &block) ⇒ Object

Add an attribute to the rendered attributes.

Examples:

api_render(:v1) do
  attribute(:first_name)
  attribute(:last_name)
  attribute(:full_name) { "#{first_name} #{last_name}" }

  # ...

end

Parameters:

  • name (String, Symbol, #to_sym)

    The name of the attribute. If no block is passed the name will be called on the Resource#object

  • block (Block)

    The block used to fetch a dynamic attribute. It will be executed in the context of the Resource#object

Since:

  • 1.0.0



140
141
142
# File 'lib/onsi/model.rb', line 140

def attribute(name, &block)
  @attributes[name.to_sym] = block || name
end

#meta(name, &block) ⇒ Object

Add a metadata value to the rendered object’s meta.

Parameters:

  • name (#to_sym)

    The name for the meta value.

  • block (Block)

    The block used to fetch the meta value. It will be executed in the context of the Resource#object

Since:

  • 1.0.0



172
173
174
# File 'lib/onsi/model.rb', line 172

def meta(name, &block)
  @metadata[name.to_sym] = block
end

#relationship(name, type, &block) ⇒ Object

Add a relationship to the rendered relationships.

Examples:

api_render(:v1) do
  relationship(:team, :team)

  # ...

end

Parameters:

  • name (Symbol, #to_sym)

    The relationship name.

  • type (String, #to_s)

    The relationship type.

  • block (Block)

    The block used to fetch a dynamic attribute. It will be executed in the context of the Resource#object

Since:

  • 1.0.0



161
162
163
# File 'lib/onsi/model.rb', line 161

def relationship(name, type, &block)
  @relationships[name.to_sym] = { type: type, attr: block || name }
end

#type(name = nil) ⇒ Object

Note:

Not required. If there is no type, the class name will be used when rendering the object. (Name is underscored)

The type name.

Parameters:

  • name (String, nil) (defaults to: nil)

    The resource object type name.

Since:

  • 1.0.0



116
117
118
119
# File 'lib/onsi/model.rb', line 116

def type(name = nil)
  @type = name if name
  @type
end