Sha256: e32ec99996107ac8b76b1370fdcf9ba09a908202b8c9320cc9d50c346c3a94fe

Contents?: true

Size: 1.42 KB

Versions: 7

Compression:

Stored size: 1.42 KB

Contents

module JSONAPIonify::Structure
  module Objects
    class Attributes < Base

      # Attributes may contain any valid JSON value.
      # Complex data structures involving JSON objects and arrays are allowed as
      # attribute values. However, any object that constitutes or is contained in an
      # attribute **MUST NOT** contain a `relationships` or `links` member, as those
      # members are reserved by this specification for future use.
      must_not_contain! :relationships, :links, deep: true

      # A resource object's AttributesObject and its RelationshipsObject are collectively called its fields.
      # Fields for a ResourceObject **MUST** share a common namespace with each
      # other and with `type` and `id`. In other words, a resource can not have an
      # attribute and relationship with the same name, nor can it have an attribute
      # or relationship named `type` or `id`.
      must_not_contain! :type, :id
      validate_each! message: 'conflicts with a relationship key' do |obj, key, _|
        !obj.parent || !obj.parent.relationship_keys.include?(key)
      end

      # Although has-one foreign keys (e.g. `author_id`) are often stored internally
      # alongside other information to be represented in a resource object, these keys
      # **SHOULD NOT** appear as attributes.
      should_not_contain! { |key| key.to_s.end_with? '_id' }

      def to_hash
        super.sort.to_h
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
jsonapionify-0.11.0 lib/jsonapionify/structure/objects/attributes.rb
jsonapionify-0.10.2 lib/jsonapionify/structure/objects/attributes.rb
jsonapionify-0.10.1 lib/jsonapionify/structure/objects/attributes.rb
jsonapionify-0.10.0 lib/jsonapionify/structure/objects/attributes.rb
jsonapionify-0.9.3 lib/jsonapionify/structure/objects/attributes.rb
jsonapionify-0.9.2 lib/jsonapionify/structure/objects/attributes.rb
jsonapionify-0.9.1 lib/jsonapionify/structure/objects/attributes.rb