module JSONAPI::Serializable::Resource::DSL

Public Class Methods

extended(klass) click to toggle source
# 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

Public Instance Methods

attribute(name, _options = {}, &block) click to toggle source

Declare an attribute for this resource.

@param [Symbol] name The key of the attribute. @yieldreturn [Hash, String, nil] The block to compute the value.

@example

attribute(:name) { @object.name }
# 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) click to toggle source

Declare a list of attributes for this resource.

@param [Array] *args The attributes keys.

@example

attributes :title, :body, :date
# File lib/jsonapi/serializable/resource/dsl.rb, line 78
def attributes(*args)
  args.each do |attr|
    attribute(attr)
  end
end
belongs_to(name, options = {}, &block)
Alias for: relationship
has_many(name, options = {}, &block)
Alias for: relationship
has_one(name, options = {}, &block)
Alias for: relationship
id(&block) click to toggle source

Declare the JSON API id of this resource.

@yieldreturn [String] The id of the resource.

@example

id { @object.id.to_s }
# File lib/jsonapi/serializable/resource/dsl.rb, line 38
def id(&block)
  self.id_block = block
end
inherited(klass) click to toggle source

rubocop:disable Metrics/AbcSize

# 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
meta(value = nil, &block) click to toggle source

@overload meta(value)

Declare the meta information for this resource.
@param [Hash] value The meta information hash.

@example
  meta key: value

@overload meta(&block)

Declare the meta information for this resource.
@yieldreturn [String] The meta information hash.
@example
  meta do
    { key: value }
  end
# 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) click to toggle source

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.

@see JSONAPI::Serializable::Relationship

@param [Symbol] name The key of the relationship. @param [Hash] options The options for the relationship.

@example

relationship :author do
  data do
    @object.author
  end
  linkage do
    { type: 'users', id: @object.author_id }
  end
  link(:self) do
    "http://api.example.com/posts/#{@object.id}/relationships/author"
  end
  link(:related) do
    "http://api.example.com/posts/#{@object.id}/author"
  end
  meta do
    { author_online: @object.author.online? }
  end
end
# File lib/jsonapi/serializable/resource/dsl.rb, line 153
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
Also aliased as: has_many, has_one, belongs_to
type(value = nil, &block) click to toggle source

@overload type(value)

Declare the JSON API type of this resource.
@param [String] value The value of the type.

@example
  type 'users'

@overload type(&block)

Declare the JSON API type of this resource.
@yieldreturn [String] The value of the type.

@example
  type { @object.type }
# 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