Module: Darstellung::Macros

Defined in:
lib/darstellung/macros.rb

Overview

This module provides all the class level macros for defining representations.

Since:

  • 0.0.0

Instance Method Summary collapse

Instance Method Details

#detail(name, options = {}, &block) ⇒ Attribute

Defines an attribute to be displayed in the detail representation of the resource.

Examples:

Display the name field in the detail display.

class UserResource
  include Darstellung::Representable
  detail :name
end

Display the name field only from version 1.0.0

class UserResource
  include Darstellung::Representable
  detail :name, from: "1.0.0"
end

Display the name field only from version 1.0.0 - 1.1.0

class UserResource
  include Darstellung::Representable
  detail :name, from: "1.0.0", to: "1.1.0"
end

Display the name field as a custom representation.

class UserResource
  include Darstellung::Representable

  detail :name do |user|
    user.full_name
  end
end

Parameters:

  • name (Symbol)

    The name of the attribute.

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

    The attribute options.

Options Hash (options):

  • :from (String)

    The version the field is available from.

  • :to (String)

    The version the field is available to.

Returns:

  • (Attribute)

    The attribute object for the field.

Since:

  • 0.0.0



51
52
53
# File 'lib/darstellung/macros.rb', line 51

def detail(name, options = {}, &block)
  create_attribute(name, detail_attributes, options, &block)
end

#detail_attributesHash

Get all the attributes that are used in the detail representation.

Examples:

Get all the detail attributes fields.

class UserResource
  include Darstellung::Representable
end

UserResource.detail_attributes

Returns:

  • (Hash)

    The name/attribute pairs.

Since:

  • 0.0.0



67
68
69
# File 'lib/darstellung/macros.rb', line 67

def detail_attributes
  @detail_attributes ||= {}
end

#summary(name, options = {}, &block) ⇒ Attribute

Defines an attribute to be displayed in the summary representation of the resource.

Examples:

Display the name field in the summary display.

class UserResource
  include Darstellung::Representable
  summary :name
end

Display the name field only from version 1.0.0

class UserResource
  include Darstellung::Representable
  summary :name, from: "1.0.0"
end

Display the name field only from version 1.0.0 - 1.1.0

class UserResource
  include Darstellung::Representable
  summary :name, from: "1.0.0", to: "1.1.0"
end

Display the name field as a custom representation.

class UserResource
  include Darstellung::Representable

  summary :name do |user|
    user.full_name
  end
end

Parameters:

  • name (Symbol)

    The name of the attribute.

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

    The attribute options.

Options Hash (options):

  • :from (String)

    The version the field is available from.

  • :to (String)

    The version the field is available to.

Returns:

  • (Attribute)

    The attribute object for the field.

Since:

  • 0.0.0



110
111
112
# File 'lib/darstellung/macros.rb', line 110

def summary(name, options = {}, &block)
  create_attribute(name, summary_attributes, options, &block)
end

#summary_attributesHash

Get all the attributes that are used in the summary representation.

Examples:

Get all the summary attributes fields.

class UserResource
  include Darstellung::Representable
end

UserResource.summary_attributes

Returns:

  • (Hash)

    The name/attribute pairs.

Since:

  • 0.0.0



126
127
128
# File 'lib/darstellung/macros.rb', line 126

def summary_attributes
  @summary_attributes ||= {}
end