Sha256: 75f5d20143fb78fb22eea0fc95c783d8b909073496e9d8f47e0ca9a70cb4d62e

Contents?: true

Size: 1.32 KB

Versions: 18

Compression:

Stored size: 1.32 KB

Contents

module JSONAPI

  # A ResourceFragment holds a ResourceIdentity and associated partial resource data.
  #
  # The following partial resource data may be stored
  # cache - the value of the cache field for the resource instance
  # related - a hash of arrays of related resource identities, grouped by relationship name
  # related_from - a set of related resource identities that loaded the fragment
  #
  # Todo: optionally use these for faster responses by bypassing model instantiation)
  # attributes - resource attributes

  class ResourceFragment
    attr_reader :identity, :attributes, :related_from, :related

    attr_accessor :primary, :cache

    alias :cache_field :cache #ToDo: Rename one or the other

    def initialize(identity)
      @identity = identity
      @cache = nil
      @attributes = {}
      @related = {}
      @primary = false
      @related_from = Set.new
    end

    def initialize_related(relationship_name)
      @related ||= {}
      @related[relationship_name.to_sym] ||= Set.new
    end

    def add_related_identity(relationship_name, identity)
      initialize_related(relationship_name)
      @related[relationship_name.to_sym] << identity
    end

    def add_related_from(identity)
      @related_from << identity
    end

    def add_attribute(name, value)
      @attributes[name] = value
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
jsonapi-resources-0.10.7 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.6 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.5 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.4 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.3 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.2 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.1 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta9 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta8 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta7 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta6 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta5 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta4 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta3 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta2.1 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta2 lib/jsonapi/resource_fragment.rb
jsonapi-resources-0.10.0.beta1 lib/jsonapi/resource_fragment.rb