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

Defined in:
lib/jsonapi/serializable/resource/attributes.rb

Overview

DSL methods for declaring attributes.

Instance Method Summary collapse

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.



53
54
55
56
# File 'lib/jsonapi/serializable/resource/attributes.rb', line 53

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.



64
65
66
67
68
# File 'lib/jsonapi/serializable/resource/attributes.rb', line 64

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

#inherited(klass) ⇒ Object



41
42
43
44
# File 'lib/jsonapi/serializable/resource/attributes.rb', line 41

def inherited(klass)
  super
  klass.attribute_blocks = attribute_blocks.dup
end