class SimpleJsonapi::Node::Attributes

Represents a resource's attributes object.

@!attribute [r] resource

@return [Object]

@!attribute [r] resource_type

@return [String]

@!attribute [r] attribute_definitions

@return [Hash{Symbol => Definition::Attribute}]

Attributes

attribute_definitions[R]
resource[R]
resource_type[R]

Public Class Methods

new(resource:, resource_type:, attribute_definitions:, **options) click to toggle source

@param resource [Object] @param resource_type [String] @param attribute_definitions [Hash{Symbol => Definition::Attribute}] @param options see {Node::Base#initialize} for additional parameters

Calls superclass method
# File lib/simple_jsonapi/node/attributes.rb, line 17
def initialize(resource:, resource_type:, attribute_definitions:, **options)
  super(options)

  @resource = resource
  @resource_type = resource_type
  @attribute_definitions = attribute_definitions
end

Public Instance Methods

as_jsonapi() click to toggle source

@return [Hash{Symbol => Hash}]

# File lib/simple_jsonapi/node/attributes.rb, line 26
def as_jsonapi
  if attribute_definitions_to_render.any?
    json = {}
    attribute_definitions_to_render.each do |name, defn|
      json[name] = evaluate(defn.value_proc, resource)
    end
    { attributes: json }
  else
    {}
  end
end

Private Instance Methods

attribute_definitions_to_render() click to toggle source
# File lib/simple_jsonapi/node/attributes.rb, line 40
def attribute_definitions_to_render
  @attribute_definitions_to_render ||= begin
    include_all_fields = fields_spec.all_fields?(resource_type)
    explicit_fields = fields_spec[resource_type]

    attribute_definitions
      .select { |name, _| include_all_fields || explicit_fields.include?(name) }
      .select { |_, defn| render?(defn, resource) }
  end
end