Module: Roar::JSON::JSONAPI::Declarative

Defined in:
lib/roar/json/json_api/declarative.rb

Overview

Declarative API for JSON API Representers.

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#attributes(&block) ⇒ Object

Define attributes for this resource.

Examples:

attributes do
  property :name
end

Parameters:

  • block (#call)

See Also:

Since:

  • 0.1.0



36
37
38
# File 'lib/roar/json/json_api/declarative.rb', line 36

def attributes(&block)
  nested(:attributes, inherit: true, &block)
end

#has_many(name, options = {}, &block) ⇒ Object

Define a to-many relationship for this resource.

Parameters:

  • name (String)

    name of the relationship

  • block (#call)

    Stuff

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

    a customizable set of options

Options Hash (options):

  • :extend (Class, Module, Proc)

    Representer to use for parsing or rendering

  • :prepare (Proc)

    Decorate the represented object

  • :class (Class, Proc)

    Class to instantiate when parsing nested fragment

  • :instance (Proc)

    Instantiate object directly when parsing nested fragment

Since:

  • 0.1.0



129
130
131
# File 'lib/roar/json/json_api/declarative.rb', line 129

def has_many(name, options = {}, &block)
  has_relationship(name, options.merge(collection: true), &block)
end

#has_one(name, options = {}, &block) ⇒ Object

Define a to-one relationship for this resource.

Parameters:

  • name (String)

    name of the relationship

  • block (#call)

    Stuff

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

    a customizable set of options

Options Hash (options):

  • :extend (Class, Module, Proc)

    Representer to use for parsing or rendering

  • :prepare (Proc)

    Decorate the represented object

  • :class (Class, Proc)

    Class to instantiate when parsing nested fragment

  • :instance (Proc)

    Instantiate object directly when parsing nested fragment

See Also:

Since:

  • 0.1.0



119
120
121
# File 'lib/roar/json/json_api/declarative.rb', line 119

def has_one(name, options = {}, &block)
  has_relationship(name, options.merge(collection: false), &block)
end

Define a link.

Examples:

Link for a resource

link(:self) { "http://authors/#{represented.id}" }

Top-level link

link(:self, toplevel: true) { "http://authors/#{represented.id}" }

Link with options

link(:self) do |opts|
  "http://articles?locale=#{opts[:user_options][:locale]}"
end

representer.to_json(user_options: { locale: 'de' })

Parameters:

  • name (Symbol, String)

    name of the link.

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

    a customizable set of options

Options Hash (options):

  • :toplevel (Boolean)

    place link at top-level of document.

Yield Parameters:

  • opts (Hash)

    Options passed to render method

See Also:

Since:

  • 0.1.0



61
62
63
64
# File 'lib/roar/json/json_api/declarative.rb', line 61

def link(name, options = {}, &block)
  return super(name, &block) unless options[:toplevel]
  for_collection.link(name, &block)
end

#meta(options = {}, &block) ⇒ Object

Define meta information.

Examples:

Meta information for a resource

meta do
  collection :reviewers
end

Top-level meta information

meta toplevel: true do
  property :copyright
end

Parameters:

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

    a customizable set of options

  • block (#call)

Options Hash (options):

  • :toplevel (Boolean)

    place meta information at top-level of document.

See Also:

Since:

  • 0.1.0



83
84
85
86
# File 'lib/roar/json/json_api/declarative.rb', line 83

def meta(options = {}, &block)
  return super(&block) unless options[:toplevel]
  for_collection.meta(&block)
end

#relationship(&block) ⇒ Object

Define links and meta information for a given relationship.

Examples:

has_one :author, extend: AuthorDecorator do
  relationship do
    link(:self)     { "/articles/#{represented.id}/relationships/author" }
    link(:related)  { "/articles/#{represented.id}/author" }
  end
end

Parameters:

  • block (#call)

Since:

  • 0.1.0



101
102
103
104
105
106
# File 'lib/roar/json/json_api/declarative.rb', line 101

def relationship(&block)
  return (@relationship ||= -> {}) unless block

  heritage.record(:relationship, &block)
  @relationship = block
end

#type(name = nil) ⇒ String

Defjne a type for this resource.

Examples:

type :articles

Parameters:

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

    type name of this resource

Returns:

  • (String)

    type name of this resource

See Also:

Since:

  • 0.1.0



18
19
20
21
22
23
# File 'lib/roar/json/json_api/declarative.rb', line 18

def type(name = nil)
  return @type unless name # original name.

  heritage.record(:type, name)
  @type = name.to_s
end