Sha256: c6e9c3eadf5fdc76c1ea90c09b140ca100edb9ce618e1b9aa6db0dd8eea5e37d

Contents?: true

Size: 1.97 KB

Versions: 24

Compression:

Stored size: 1.97 KB

Contents

module JSONAPIonify::Structure
  module Objects
    # ResourceObjects appear in a JSON API document to represent resources.
    class Resource < ResourceIdentifier
      define_order *%i{type id attributes relationships meta links}

      # The `id` member is not required when the resource object originates at the
      # client and represents a new resource to be created on the server.
      must_contain! :id, if: ->(obj) { obj.server? } # an id representing the resource

      # In addition, a resource object **MAY** contain any of these top-level members:
      may_contain! :attributes, # An AttributesObject representing some of the resource's data.
                   :relationships, # A RelationshipsObject describing relationships between the resource and other JSON API resources.
                   :links, # A LinksObject containing links related to the resource.
                   :meta # A MetaObject containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.

      implements :attributes, as: Attributes
      implements :relationships, as: Maps::Relationships
      implements :links, as: Maps::Links
      implements :meta, as: Meta

      # Note: This spec is agnostic about inflection rules, so the value of `type`
      # can be either plural or singular. However, the same value should be used
      # consistently throughout an implementation.
      def attribute_keys
        return [] unless self[:attributes]
        self[:attributes].keys
      end

      def relationship_keys
        return [] unless self[:relationships]
        self[:relationships].keys
      end

      def relates_to?(other)
        relationships = self[:relationships]
        return false unless relationships
        relationships.any? do |_, resource_identifier|
          Array.wrap(resource_identifier[:data]).any? do |rel|
            rel[:id] == other[:id] && rel[:type] == other[:type]
          end
        end
      end

    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.9 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.8 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.7 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.5 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.4 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.3 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.2 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.1 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.12.0 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.11 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.10 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.9 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.8 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.7 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.6 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.5 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.11.0 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.10.2 lib/jsonapionify/structure/objects/resource.rb
jsonapionify-0.10.1 lib/jsonapionify/structure/objects/resource.rb