Module: Darstellung::Representable

Includes:
Definable
Defined in:
lib/darstellung/representable.rb

Overview

This module is included into resources that need a summary and detail view for display in APIs. Summary views are for display in lists, detail views are generally show actions.

Since:

  • 0.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Definable

#detail_attributes, #summary_attributes

Instance Attribute Details

#resourceObject (readonly)

Since:

  • 0.0.0



17
18
19
# File 'lib/darstellung/representable.rb', line 17

def resource
  @resource
end

#resource The resource being represented.(Theresourcebeingrepresented.) ⇒ Object (readonly)



17
# File 'lib/darstellung/representable.rb', line 17

attr_reader :resource

Class Method Details

.included(klass) ⇒ Object

Including the module will inject the necessary macros into the base class.

Parameters:

  • klass (Class)

    The class including the module.

Since:

  • 0.0.0



121
122
123
# File 'lib/darstellung/representable.rb', line 121

def included(klass)
  klass.extend(Macros)
end

Instance Method Details

#collection(version = nil) ⇒ Hash

Note:

The collection representation is a list of summary representations.

Gets the collection view for a specific version of the resource. If no version is provided then we assume from “0.0.0” which will render attributes available in all versions of the API.

Examples:

Get the collection representation.

user_resource.collection("1.0.1")

Parameters:

  • version (String) (defaults to: nil)

    The version to get of the resource.

Returns:

  • (Hash)

    The collection representation of the resource.

Since:

  • 0.0.0



33
34
35
# File 'lib/darstellung/representable.rb', line 33

def collection(version = nil)
  representation(version, multiple(summary_attributes, version))
end

#detail(version = nil) ⇒ Hash

Gets the detail view for a specific version of the resource. If no version is provided then we assume from “0.0.0” which will render attributes available in all versions of the API.

Examples:

Get the detail representation.

user_resource.detail("1.0.1")

Parameters:

  • version (String) (defaults to: nil)

    The version to get of the resource.

Returns:

  • (Hash)

    The detail representation of the resource.

Since:

  • 0.0.0



49
50
51
# File 'lib/darstellung/representable.rb', line 49

def detail(version = nil)
  representation(version, single(detail_attributes, resource, version))
end

#initialize(resource) ⇒ Object

Initialize the new representation with the provided resource.

Examples:

Initialize the representation.

class UserResource
  include Darstellung::Representable
end

UserResource.new(user)

Parameters:

  • resource (Object)

    The resource to be represented.

Since:

  • 0.0.0



65
66
67
# File 'lib/darstellung/representable.rb', line 65

def initialize(resource)
  @resource = resource
end

#summary(version = nil) ⇒ Hash

Gets the summary view for a specific version of the resource. If no version is provided then we assume from “0.0.0” which will render attributes available in all versions of the API.

Examples:

Get the summary representation.

user_resource.summary("1.0.1")

Parameters:

  • version (String) (defaults to: nil)

    The version to get of the resource.

Returns:

  • (Hash)

    The summary representation of the resource.

Since:

  • 0.0.0



81
82
83
# File 'lib/darstellung/representable.rb', line 81

def summary(version = nil)
  representation(version, single(summary_attributes, resource, version))
end