Module: JSONAPI::Serializable::Resource::DSL

Included in:
JSONAPI::Serializable::Resource
Defined in:
lib/jsonapi/serializable/resource/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 5

def self.extended(klass)
  class << klass
    attr_accessor :id_block, :type_val, :type_block, :attribute_blocks,
                  :relationship_blocks, :relationship_options,
                  :link_blocks, :meta_val, :meta_block
  end

  klass.attribute_blocks = {}
  klass.relationship_blocks  = {}
  klass.relationship_options = {}
  klass.link_blocks = {}
end

Instance Method Details

#attribute(name, _options = {}, &block) ⇒ Object

Declare an attribute for this resource.

Examples:

attribute(:name) { @object.name }

Parameters:

  • name (Symbol)

    The key of the attribute.

Yield Returns:

  • (Hash, String, nil)

    The block to compute the value.


67
68
69
70
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 67

def attribute(name, _options = {}, &block)
  block ||= proc { @object.public_send(name) }
  attribute_blocks[name.to_sym] = block
end

#attributes(*args) ⇒ Object

Declare a list of attributes for this resource.

Examples:

attributes :title, :body, :date

Parameters:

  • *args (Array)

    The attributes keys.


78
79
80
81
82
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 78

def attributes(*args)
  args.each do |attr|
    attribute(attr)
  end
end

#id(&block) ⇒ Object

Declare the JSON API id of this resource.

Examples:

id { @user.id.to_s }

Yield Returns:

  • (String)

    The id of the resource.


38
39
40
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 38

def id(&block)
  self.id_block = block
end

#inherited(klass) ⇒ Object

rubocop:disable Metrics/AbcSize


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 19

def inherited(klass)
  klass.id_block   = id_block
  klass.type_val   = type_val
  klass.type_block = type_block
  klass.attribute_blocks     = attribute_blocks.dup
  klass.relationship_blocks  = relationship_blocks.dup
  klass.relationship_options = relationship_options.dup
  klass.link_blocks = link_blocks.dup
  klass.meta_val    = meta_val
  klass.meta_block  = meta_block
end

Declare a link for this resource. The properties of the link are set

by providing a block in which the DSL methods of
+JSONAPI::Serializable::Link+ are called, or the value of the link
is returned directly.

Examples:

link(:self) do
  "http://api.example.com/users/#{@user.id}"
end
link(:self) do
  href "http://api.example.com/users/#{@user.id}"
  meta is_self: true
end

Parameters:

  • name (Symbol)

    The key of the link.

Yield Returns:

  • (Hash, String, nil)

    The block to compute the value, if any.

See Also:

  • JSONAPI::Serialiable::Link

104
105
106
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 104

def link(name, &block)
  link_blocks[name] = block
end

#meta(value) ⇒ Object #meta(&block) ⇒ Object

Overloads:

  • #meta(value) ⇒ Object

    Declare the meta information for this resource.

    Examples:

    meta key: value

    Parameters:

    • value (Hash)

      The meta information hash.

  • #meta(&block) ⇒ Object

    Declare the meta information for this resource.

    Examples:

    meta do
      { key: value }
    end

    Yield Returns:

    • (String)

      The meta information hash.


122
123
124
125
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 122

def meta(value = nil, &block)
  self.meta_val = value
  self.meta_block = block
end

#relationship(name, options = {}, &block) ⇒ Object Also known as: has_many, has_one, belongs_to

Declare a relationship for this resource. The properties of the

relationship are set by providing a block in which the DSL methods
of +JSONAPI::Serializable::Relationship+ are called.

Examples:

relationship :posts do
  resources { @user.posts.map { |p| PostResource.new(post: p) } }
end
relationship :author do
  resources do
    @post.author && UserResource.new(user: @post.author)
  end
  data do
    { type: 'users', id: @post.author_id }
  end
  link(:self) do
    "http://api.example.com/posts/#{@post.id}/relationships/author"
  end
  link(:related) do
    "http://api.example.com/posts/#{@post.id}/author"
  end
  meta do
    { author_online: @post.author.online? }
  end
end

Parameters:

  • name (Symbol)

    The key of the relationship.

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

    The options for the relationship.

See Also:


158
159
160
161
162
163
164
165
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 158

def relationship(name, options = {}, &block)
  rel_block = proc do
    data { @object.public_send(name) }
    instance_eval(&block) unless block.nil?
  end
  relationship_blocks[name.to_sym]  = rel_block
  relationship_options[name.to_sym] = options
end

#type(value) ⇒ Object #type(&block) ⇒ Object

Overloads:

  • #type(value) ⇒ Object

    Declare the JSON API type of this resource.

    Examples:

    type 'users'

    Parameters:

    • value (String)

      The value of the type.

  • #type(&block) ⇒ Object

    Declare the JSON API type of this resource.

    Examples:

    type { @object.type }

    Yield Returns:

    • (String)

      The value of the type.


55
56
57
58
# File 'lib/jsonapi/serializable/resource/dsl.rb', line 55

def type(value = nil, &block)
  self.type_val   = value.to_sym if value
  self.type_block = block
end